Skip to content

Commit da2fd52

Browse files
committed
Merge pull request #38 from dbalduini/break-client-auth
Break client auth
2 parents e9204fa + 0d1137d commit da2fd52

File tree

11 files changed

+247
-222
lines changed

11 files changed

+247
-222
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ trait OAuth2BaseProvider extends Results {
110110
trait OAuth2Provider extends OAuth2BaseProvider {
111111

112112
/**
113-
* Issue access token in DataHandler process and return the response to client.
113+
* Issue access token in AuthorizationHandler process and return the response to client.
114114
*
115-
* @param dataHandler Implemented DataHander for register access token to your system.
115+
* @param handler Implemented AuthorizationHandler for register access token to your system.
116116
* @param request Playframework is provided HTTP request interface.
117117
* @tparam A play.api.mvc.Request has type.
118118
* @return Request is successful then return JSON to client in OAuth 2.0 format.
119119
* Request is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
120120
*/
121-
def issueAccessToken[A, U](dataHandler: DataHandler[U], timeout: Duration = 60.seconds)(implicit request: Request[A]): Result = {
122-
val f = tokenEndpoint.handleRequest(request, dataHandler).map {
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 {
123123
case Left(e) if e.statusCode == 400 => BadRequest(responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
124124
case Left(e) if e.statusCode == 401 => Unauthorized(responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
125125
case Right(r) => Ok(Json.toJson(responseAccessToken(r))).withHeaders("Cache-Control" -> "no-store", "Pragma" -> "no-cache")
@@ -129,17 +129,17 @@ trait OAuth2Provider extends OAuth2BaseProvider {
129129
}
130130

131131
/**
132-
* Authorize to already created access token in DataHandler process and return the response to client.
132+
* Authorize to already created access token in ProtectedResourceHandler process and return the response to client.
133133
*
134-
* @param dataHandler Implemented DataHander for authenticate to your system.
134+
* @param handler Implemented ProtectedResourceHandler for authenticate to your system.
135135
* @param callback Callback is called when authentication is successful.
136136
* @param request Playframework is provided HTTP request interface.
137137
* @tparam A play.api.mvc.Request has type.
138138
* @return Authentication is successful then the response use your API result.
139139
* Authentication is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
140140
*/
141-
def authorize[A, U](dataHandler: DataHandler[U], timeout: Duration = 60.seconds)(callback: AuthInfo[U] => Result)(implicit request: Request[A]): Result = {
142-
val f = protectedResource.handleRequest(request, dataHandler).map {
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 {
143143
case Left(e) if e.statusCode == 400 => BadRequest.withHeaders(responseOAuthErrorHeader(e))
144144
case Left(e) if e.statusCode == 401 => Unauthorized.withHeaders(responseOAuthErrorHeader(e))
145145
case Right(authInfo) => callback(authInfo)
@@ -183,34 +183,34 @@ trait OAuth2Provider extends OAuth2BaseProvider {
183183
trait OAuth2AsyncProvider extends OAuth2BaseProvider {
184184

185185
/**
186-
* Issue access token in DataHandler process and return the response to client.
186+
* Issue access token in AuthorizationHandler process and return the response to client.
187187
*
188-
* @param dataHandler Implemented DataHander for register access token to your system.
188+
* @param handler Implemented AuthorizationHandler for register access token to your system.
189189
* @param request Playframework is provided HTTP request interface.
190190
* @tparam A play.api.mvc.Request has type.
191191
* @return Request is successful then return JSON to client in OAuth 2.0 format.
192192
* Request is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
193193
*/
194-
def issueAccessToken[A, U](dataHandler: DataHandler[U])(implicit request: Request[A]): Future[Result] = {
195-
tokenEndpoint.handleRequest(request, dataHandler).map {
194+
def issueAccessToken[A, U](handler: AuthorizationHandler[U])(implicit request: Request[A]): Future[Result] = {
195+
tokenEndpoint.handleRequest(request, handler).map {
196196
case Left(e) if e.statusCode == 400 => BadRequest(responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
197197
case Left(e) if e.statusCode == 401 => Unauthorized(responseOAuthErrorJson(e)).withHeaders(responseOAuthErrorHeader(e))
198198
case Right(r) => Ok(Json.toJson(responseAccessToken(r))).withHeaders("Cache-Control" -> "no-store", "Pragma" -> "no-cache")
199199
}
200200
}
201201

202202
/**
203-
* Authorize to already created access token in DataHandler process and return the response to client.
203+
* Authorize to already created access token in ProtectedResourceHandler process and return the response to client.
204204
*
205-
* @param dataHandler Implemented DataHander for authenticate to your system.
205+
* @param handler Implemented ProtectedResourceHandler for authenticate to your system.
206206
* @param callback Callback is called when authentication is successful.
207207
* @param request Playframework is provided HTTP request interface.
208208
* @tparam A play.api.mvc.Request has type.
209209
* @return Authentication is successful then the response use your API result.
210210
* Authentication is failed then return BadRequest or Unauthorized status to client with cause into the JSON.
211211
*/
212-
def authorize[A, U](dataHandler: DataHandler[U])(callback: AuthInfo[U] => Future[Result])(implicit request: Request[A]): Future[Result] = {
213-
protectedResource.handleRequest(request, dataHandler).flatMap {
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 {
214214
case Left(e) if e.statusCode == 400 => Future.successful(BadRequest.withHeaders(responseOAuthErrorHeader(e)))
215215
case Left(e) if e.statusCode == 401 => Future.successful(Unauthorized.withHeaders(responseOAuthErrorHeader(e)))
216216
case Right(authInfo) => callback(authInfo)

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Keys._
44
object ScalaOAuth2Build extends Build {
55

66
lazy val _organization = "com.nulab-inc"
7-
lazy val _version = "0.10.0"
7+
lazy val _version = "0.11.0-SNAPSHOT"
88
lazy val _playVersion = "2.3.4"
99

1010
val _scalaVersion = "2.10.4"
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package scalaoauth2.provider
2+
3+
import scala.concurrent.Future
4+
5+
/**
6+
* Provide <b>Authorization</b> phases support for using OAuth 2.0.
7+
*
8+
* <h3>[Authorization phases]</h3>
9+
*
10+
* <h4>Authorization Code Grant</h4>
11+
* <ul>
12+
* <li>validateClient(clientCredential, grantType)</li>
13+
* <li>findAuthInfoByCode(code)</li>
14+
* <li>getStoredAccessToken(authInfo)</li>
15+
* <li>isAccessTokenExpired(token)</li>
16+
* <li>refreshAccessToken(authInfo, token)
17+
* <li>createAccessToken(authInfo)</li>
18+
* </ul>
19+
*
20+
* <h4>Refresh Token Grant</h4>
21+
* <ul>
22+
* <li>validateClient(clientCredential, grantType)</li>
23+
* <li>findAuthInfoByRefreshToken(refreshToken)</li>
24+
* <li>refreshAccessToken(authInfo, refreshToken)</li>
25+
* </ul>
26+
*
27+
* <h4>Resource Owner Password Credentials Grant</h4>
28+
* <ul>
29+
* <li>validateClient(clientCredential, grantType)</li>
30+
* <li>findUser(username, password)</li>
31+
* <li>getStoredAccessToken(authInfo)</li>
32+
* <li>isAccessTokenExpired(token)</li>
33+
* <li>refreshAccessToken(authInfo, token)
34+
* <li>createAccessToken(authInfo)</li>
35+
* </ul>
36+
*
37+
* <h4>Client Credentials Grant</h4>
38+
* <ul>
39+
* <li>validateClient(clientCredential, grantType)</li>
40+
* <li>findClientUser(clientCredential)</li>
41+
* <li>getStoredAccessToken(authInfo)</li>
42+
* <li>isAccessTokenExpired(token)</li>
43+
* <li>refreshAccessToken(authInfo, token)
44+
* <li>createAccessToken(authInfo)</li>
45+
* </ul>
46+
*
47+
*/
48+
trait AuthorizationHandler[U] {
49+
50+
/**
51+
* Verify proper client with parameters for issue an access token.
52+
*
53+
* @param clientCredential Client sends clientId and clientSecret which are registered by application.
54+
* @param grantType Client sends this value which is registered by application.
55+
* @return true if request is a regular client, false if request is a illegal client.
56+
*/
57+
def validateClient(clientCredential: ClientCredential, grantType: String): Future[Boolean]
58+
59+
/**
60+
* Find userId with username and password these are used on your system.
61+
* If you don't support Resource Owner Password Credentials Grant then doesn't need implementing.
62+
*
63+
* @param username Client sends this value which is used on your system.
64+
* @param password Client sends this value which is used on your system.
65+
* @return Including UserId to Option if could find the user, None if couldn't find.
66+
*/
67+
def findUser(username: String, password: String): Future[Option[U]]
68+
69+
/**
70+
* Creates a new access token by authorized information.
71+
*
72+
* @param authInfo This value is already authorized by system.
73+
* @return Access token returns to client.
74+
*/
75+
def createAccessToken(authInfo: AuthInfo[U]): Future[AccessToken]
76+
77+
/**
78+
* Returns stored access token by authorized information.
79+
*
80+
* If want to create new access token then have to return None
81+
*
82+
* @param authInfo This value is already authorized by system.
83+
* @return Access token returns to client.
84+
*/
85+
def getStoredAccessToken(authInfo: AuthInfo[U]): Future[Option[AccessToken]]
86+
87+
/**
88+
* Creates a new access token by refreshToken.
89+
*
90+
* @param authInfo This value is already authorized by system.
91+
* @return Access token returns to client.
92+
*/
93+
def refreshAccessToken(authInfo: AuthInfo[U], refreshToken: String): Future[AccessToken]
94+
95+
/**
96+
* Find authorized information by authorization code.
97+
*
98+
* If you don't support Authorization Code Grant then doesn't need implementing.
99+
*
100+
* @param code Client sends authorization code which is registered by system.
101+
* @return Return authorized information that matched the code.
102+
*/
103+
def findAuthInfoByCode(code: String): Future[Option[AuthInfo[U]]]
104+
105+
/**
106+
* Find authorized information by refresh token.
107+
*
108+
* If you don't support Refresh Token Grant then doesn't need implementing.
109+
*
110+
* @param refreshToken Client sends refresh token which is created by system.
111+
* @return Return authorized information that matched the refresh token.
112+
*/
113+
def findAuthInfoByRefreshToken(refreshToken: String): Future[Option[AuthInfo[U]]]
114+
115+
/**
116+
* Find user by clientId and clientSecret.
117+
*
118+
* If you don't support Client Credentials Grant then doesn't need implementing.
119+
*
120+
* @param clientCredential Client sends clientId and clientSecret which are registered by application.
121+
* @return Return user that matched both values.
122+
*/
123+
def findClientUser(clientCredential: ClientCredential, scope: Option[String]): Future[Option[U]]
124+
125+
}

0 commit comments

Comments
 (0)