11package scalaoauth2 .provider
22
3- import play .api .mvc ._
43import play .api .libs .json ._
5- import scala . concurrent . Await
6- import scala . concurrent . Future
4+ import play . api . mvc . _
5+
76import scala .concurrent .ExecutionContext .Implicits .global
7+ import scala .concurrent .Future
88import scala .language .implicitConversions
9- import scala .concurrent .duration ._
109
1110/**
1211 * Basic OAuth2 provider trait.
@@ -83,7 +82,7 @@ trait OAuth2BaseProvider extends Results {
8382 * <h3>Create controller for issue access token</h3>
8483 * <code>
8584 * object OAuth2Controller extends Controller with OAuth2Provider {
86- * def accessToken = Action { implicit request =>
85+ * def accessToken = Action.async { implicit request =>
8786 * issueAccessToken(new MyDataHandler())
8887 * }
8988 * }
@@ -98,7 +97,7 @@ trait OAuth2BaseProvider extends Results {
9897 * <code>
9998 * import scalaoauth2.provider._
10099 * object BookController extends Controller with OAuthProvider {
101- * def list = Action { implicit request =>
100+ * def list = Action.async { implicit request =>
102101 * authorize(new MyDataHandler()) { authInfo =>
103102 * val user = authInfo.user // User is defined on your system
104103 * // access resource for the user
@@ -118,14 +117,12 @@ trait OAuth2Provider extends OAuth2BaseProvider {
118117 * @return Request is successful then return JSON to client in OAuth 2.0 format.
119118 * Request is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
120119 */
121- def issueAccessToken [A , U ](handler : AuthorizationHandler [U ], timeout : Duration = 60 .seconds )(implicit request : Request [A ]): Result = {
122- val f = tokenEndpoint.handleRequest(request, handler).map {
120+ def issueAccessToken [A , U ](handler : AuthorizationHandler [U ])(implicit request : Request [A ]): Future [ Result ] = {
121+ tokenEndpoint.handleRequest(request, handler).map {
123122 case Left (e) if e.statusCode == 400 => BadRequest (responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
124123 case Left (e) if e.statusCode == 401 => Unauthorized (responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
125124 case Right (r) => Ok (Json .toJson(responseAccessToken(r))).withHeaders(" Cache-Control" -> " no-store" , " Pragma" -> " no-cache" )
126125 }
127-
128- Await .result(f, timeout)
129126 }
130127
131128 /**
@@ -138,14 +135,12 @@ trait OAuth2Provider extends OAuth2BaseProvider {
138135 * @return Authentication is successful then the response use your API result.
139136 * Authentication is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
140137 */
141- def authorize [A , U ](handler : ProtectedResourceHandler [U ], timeout : Duration = 60 .seconds )(callback : AuthInfo [U ] => Result )(implicit request : Request [A ]): Result = {
142- val f = protectedResource.handleRequest(request, handler).map {
143- case Left (e) if e.statusCode == 400 => BadRequest .withHeaders(responseOAuthErrorHeader(e))
144- case Left (e) if e.statusCode == 401 => Unauthorized .withHeaders(responseOAuthErrorHeader(e))
138+ def authorize [A , U ](handler : ProtectedResourceHandler [U ])(callback : AuthInfo [U ] => Future [ Result ] )(implicit request : Request [A ]): Future [ Result ] = {
139+ protectedResource.handleRequest(request, handler).flatMap {
140+ case Left (e) if e.statusCode == 400 => Future .successful( BadRequest .withHeaders(responseOAuthErrorHeader(e) ))
141+ case Left (e) if e.statusCode == 401 => Future .successful( Unauthorized .withHeaders(responseOAuthErrorHeader(e) ))
145142 case Right (authInfo) => callback(authInfo)
146143 }
147-
148- Await .result(f, timeout)
149144 }
150145
151146}
@@ -180,41 +175,5 @@ trait OAuth2Provider extends OAuth2BaseProvider {
180175 * }
181176 * </code>
182177 */
183- trait OAuth2AsyncProvider extends OAuth2BaseProvider {
184-
185- /**
186- * Issue access token in AuthorizationHandler process and return the response to client.
187- *
188- * @param handler Implemented AuthorizationHandler for register access token to your system.
189- * @param request Playframework is provided HTTP request interface.
190- * @tparam A play.api.mvc.Request has type.
191- * @return Request is successful then return JSON to client in OAuth 2.0 format.
192- * Request is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
193- */
194- def issueAccessToken [A , U ](handler : AuthorizationHandler [U ])(implicit request : Request [A ]): Future [Result ] = {
195- tokenEndpoint.handleRequest(request, handler).map {
196- case Left (e) if e.statusCode == 400 => BadRequest (responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
197- case Left (e) if e.statusCode == 401 => Unauthorized (responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
198- case Right (r) => Ok (Json .toJson(responseAccessToken(r))).withHeaders(" Cache-Control" -> " no-store" , " Pragma" -> " no-cache" )
199- }
200- }
201-
202- /**
203- * Authorize to already created access token in ProtectedResourceHandler process and return the response to client.
204- *
205- * @param handler Implemented ProtectedResourceHandler for authenticate to your system.
206- * @param callback Callback is called when authentication is successful.
207- * @param request Playframework is provided HTTP request interface.
208- * @tparam A play.api.mvc.Request has type.
209- * @return Authentication is successful then the response use your API result.
210- * Authentication is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
211- */
212- def authorize [A , U ](handler : ProtectedResourceHandler [U ])(callback : AuthInfo [U ] => Future [Result ])(implicit request : Request [A ]): Future [Result ] = {
213- protectedResource.handleRequest(request, handler).flatMap {
214- case Left (e) if e.statusCode == 400 => Future .successful(BadRequest .withHeaders(responseOAuthErrorHeader(e)))
215- case Left (e) if e.statusCode == 401 => Future .successful(Unauthorized .withHeaders(responseOAuthErrorHeader(e)))
216- case Right (authInfo) => callback(authInfo)
217- }
218- }
219-
220- }
178+ @ deprecated(" Use OAuth2Provider" , " 0.12.0" )
179+ trait OAuth2AsyncProvider extends OAuth2Provider
0 commit comments