Skip to content

Commit 56ed28a

Browse files
authored
split OAuth2Provider to protected resource and issue token (#102)
1 parent 63ab59f commit 56ed28a

File tree

1 file changed

+82
-76
lines changed

1 file changed

+82
-76
lines changed

play2-oauth2-provider/src/main/scala/scalaoauth2/provider/OAuth2Provider.scala

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,6 @@ import scala.language.implicitConversions
1111
*/
1212
trait OAuth2BaseProvider extends Results {
1313

14-
val protectedResource: ProtectedResource = ProtectedResource
15-
16-
val tokenEndpoint: TokenEndpoint = TokenEndpoint
17-
18-
implicit def play2oauthRequest(request: RequestHeader): AuthorizationRequest = {
19-
new AuthorizationRequest(request.headers.toMap, request.queryString)
20-
}
21-
22-
implicit def play2oauthRequest[A](request: Request[A]): AuthorizationRequest = {
23-
val param: Map[String, Seq[String]] = getParam(request)
24-
new AuthorizationRequest(request.headers.toMap, param)
25-
}
26-
27-
implicit def play2protectedResourceRequest(request: RequestHeader): ProtectedResourceRequest = {
28-
new ProtectedResourceRequest(request.headers.toMap, request.queryString)
29-
}
30-
31-
implicit def play2protectedResourceRequest[A](request: Request[A]): ProtectedResourceRequest = {
32-
val param: Map[String, Seq[String]] = getParam(request)
33-
new ProtectedResourceRequest(request.headers.toMap, param)
34-
}
35-
3614
private[provider] def getParam[A](request: Request[A]): Map[String, Seq[String]] = {
3715
(request.body match {
3816
case body: play.api.mvc.AnyContent if body.asFormUrlEncoded.isDefined => body.asFormUrlEncoded.get
@@ -63,6 +41,86 @@ trait OAuth2BaseProvider extends Results {
6341

6442
}
6543

44+
protected[scalaoauth2] def responseOAuthErrorHeader(e: OAuthError): (String, String) = "WWW-Authenticate" -> ("Bearer " + toOAuthErrorString(e))
45+
46+
protected def toOAuthErrorString(e: OAuthError): String = {
47+
val params = Seq("error=\"" + e.errorType + "\"") ++
48+
(if (!e.description.isEmpty) { Seq("error_description=\"" + e.description + "\"") } else { Nil })
49+
params.mkString(", ")
50+
}
51+
52+
}
53+
54+
trait OAuth2ProtectedResourceProvider extends OAuth2BaseProvider {
55+
56+
val protectedResource: ProtectedResource = ProtectedResource
57+
58+
implicit def play2protectedResourceRequest(request: RequestHeader): ProtectedResourceRequest = {
59+
new ProtectedResourceRequest(request.headers.toMap, request.queryString)
60+
}
61+
62+
implicit def play2protectedResourceRequest[A](request: Request[A]): ProtectedResourceRequest = {
63+
val param: Map[String, Seq[String]] = getParam(request)
64+
new ProtectedResourceRequest(request.headers.toMap, param)
65+
}
66+
67+
/**
68+
* Authorize to already created access token in ProtectedResourceHandler process and return the response to client.
69+
*
70+
* @param handler Implemented ProtectedResourceHandler for authenticate to your system.
71+
* @param callback Callback is called when authentication is successful.
72+
* @param request Play Framework is provided HTTP request interface.
73+
* @param ctx This contxt is used by ProtectedResource.
74+
* @tparam A play.api.mvc.Request has type.
75+
* @tparam U set the type in AuthorizationHandler.
76+
* @return Authentication is successful then the response use your API result.
77+
* Authentication is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
78+
*/
79+
def authorize[A, U](handler: ProtectedResourceHandler[U])(callback: AuthInfo[U] => Future[Result])(implicit request: Request[A], ctx: ExecutionContext): Future[Result] = {
80+
protectedResource.handleRequest(request, handler).flatMap {
81+
case Left(e) => Future.successful(new Status(e.statusCode).withHeaders(responseOAuthErrorHeader(e)))
82+
case Right(authInfo) => callback(authInfo)
83+
}
84+
}
85+
86+
}
87+
88+
trait OAuth2TokenEndpointProvider extends OAuth2BaseProvider {
89+
90+
val tokenEndpoint: TokenEndpoint = TokenEndpoint
91+
92+
implicit def play2oauthRequest(request: RequestHeader): AuthorizationRequest = {
93+
new AuthorizationRequest(request.headers.toMap, request.queryString)
94+
}
95+
96+
implicit def play2oauthRequest[A](request: Request[A]): AuthorizationRequest = {
97+
val param: Map[String, Seq[String]] = getParam(request)
98+
new AuthorizationRequest(request.headers.toMap, param)
99+
}
100+
101+
/**
102+
* Issue access token in AuthorizationHandler process and return the response to client.
103+
*
104+
* @param handler Implemented AuthorizationHandler for register access token to your system.
105+
* @param request Play Framework is provided HTTP request interface.
106+
* @param ctx This context is used by TokenEndPoint.
107+
* @tparam A play.api.mvc.Request has type.
108+
* @tparam U set the type in AuthorizationHandler.
109+
* @return Request is successful then return JSON to client in OAuth 2.0 format.
110+
* Request is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
111+
*/
112+
def issueAccessToken[A, U](handler: AuthorizationHandler[U])(implicit request: Request[A], ctx: ExecutionContext): Future[Result] = {
113+
tokenEndpoint.handleRequest(request, handler).map {
114+
case Left(e) => new Status(e.statusCode)(responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
115+
case Right(r) => Ok(Json.toJson(responseAccessToken(r))).withHeaders("Cache-Control" -> "no-store", "Pragma" -> "no-cache")
116+
}
117+
}
118+
119+
protected[scalaoauth2] def responseOAuthErrorJson(e: OAuthError): JsValue = Json.obj(
120+
"error" -> e.errorType,
121+
"error_description" -> e.description
122+
)
123+
66124
protected[scalaoauth2] def responseAccessToken[U](r: GrantHandlerResult[U]) = {
67125
Map[String, JsValue](
68126
"token_type" -> JsString(r.tokenType),
@@ -76,19 +134,6 @@ trait OAuth2BaseProvider extends Results {
76134
}
77135
}
78136

79-
protected[scalaoauth2] def responseOAuthErrorJson(e: OAuthError): JsValue = Json.obj(
80-
"error" -> e.errorType,
81-
"error_description" -> e.description
82-
)
83-
84-
protected[scalaoauth2] def responseOAuthErrorHeader(e: OAuthError): (String, String) = "WWW-Authenticate" -> ("Bearer " + toOAuthErrorString(e))
85-
86-
protected def toOAuthErrorString(e: OAuthError): String = {
87-
val params = Seq("error=\"" + e.errorType + "\"") ++
88-
(if (!e.description.isEmpty) { Seq("error_description=\"" + e.description + "\"") } else { Nil })
89-
params.mkString(", ")
90-
}
91-
92137
}
93138

94139
/**
@@ -111,7 +156,7 @@ trait OAuth2BaseProvider extends Results {
111156
* <h3>Authorized</h3>
112157
* @example {{{
113158
* import scalaoauth2.provider._
114-
* object BookController extends Controller with OAuthProvider {
159+
* object BookController extends Controller with OAuth2Provider {
115160
* def list = Action.async { implicit request =>
116161
* authorize(new MyDataHandler()) { authInfo =>
117162
* val user = authInfo.user // User is defined on your system
@@ -121,46 +166,7 @@ trait OAuth2BaseProvider extends Results {
121166
* }
122167
* }}}
123168
*/
124-
trait OAuth2Provider extends OAuth2BaseProvider {
125-
126-
/**
127-
* Issue access token in AuthorizationHandler process and return the response to client.
128-
*
129-
* @param handler Implemented AuthorizationHandler for register access token to your system.
130-
* @param request Playframework is provided HTTP request interface.
131-
* @param ctx This context is used by TokenEndPoint.
132-
* @tparam A play.api.mvc.Request has type.
133-
* @tparam U set the type in AuthorizationHandler.
134-
* @return Request is successful then return JSON to client in OAuth 2.0 format.
135-
* Request is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
136-
*/
137-
def issueAccessToken[A, U](handler: AuthorizationHandler[U])(implicit request: Request[A], ctx: ExecutionContext): Future[Result] = {
138-
tokenEndpoint.handleRequest(request, handler).map {
139-
case Left(e) => new Status(e.statusCode)(responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
140-
case Right(r) => Ok(Json.toJson(responseAccessToken(r))).withHeaders("Cache-Control" -> "no-store", "Pragma" -> "no-cache")
141-
}
142-
}
143-
144-
/**
145-
* Authorize to already created access token in ProtectedResourceHandler process and return the response to client.
146-
*
147-
* @param handler Implemented ProtectedResourceHandler for authenticate to your system.
148-
* @param callback Callback is called when authentication is successful.
149-
* @param request Playframework is provided HTTP request interface.
150-
* @param ctx This contxt is used by ProtectedResource.
151-
* @tparam A play.api.mvc.Request has type.
152-
* @tparam U set the type in AuthorizationHandler.
153-
* @return Authentication is successful then the response use your API result.
154-
* Authentication is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
155-
*/
156-
def authorize[A, U](handler: ProtectedResourceHandler[U])(callback: AuthInfo[U] => Future[Result])(implicit request: Request[A], ctx: ExecutionContext): Future[Result] = {
157-
protectedResource.handleRequest(request, handler).flatMap {
158-
case Left(e) => Future.successful(new Status(e.statusCode).withHeaders(responseOAuthErrorHeader(e)))
159-
case Right(authInfo) => callback(authInfo)
160-
}
161-
}
162-
163-
}
169+
trait OAuth2Provider extends OAuth2ProtectedResourceProvider with OAuth2TokenEndpointProvider
164170

165171
/**
166172
* OAuth2AsyncProvider supports issue access token and authorize in asynchronous.

0 commit comments

Comments
 (0)