Skip to content

Commit 8a679af

Browse files
committed
improve error messages
1 parent 1f31f33 commit 8a679af

File tree

7 files changed

+21
-22
lines changed

7 files changed

+21
-22
lines changed

scala-oauth2-core/src/main/scala/scalaoauth2/provider/AccessTokenFetcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object AuthHeader extends AccessTokenFetcher {
3939
override def fetch(request: ProtectedResourceRequest): FetchResult = {
4040
val header = request.requireHeader("Authorization")
4141
val matcher = REGEXP_AUTHORIZATION.findFirstMatchIn(header).getOrElse {
42-
throw new InvalidRequest("parse() method was called when match() result was false.")
42+
throw new InvalidRequest("Authorization is invalid")
4343
}
4444

4545
val token = matcher.group(2)

scala-oauth2-core/src/main/scala/scalaoauth2/provider/DataHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ trait DataHandler[U] {
144144
def findAuthInfoByRefreshToken(refreshToken: String): Future[Option[AuthInfo[U]]]
145145

146146
/**
147-
* Find userId by clientId and clientSecret.
147+
* Find user by clientId and clientSecret.
148148
*
149149
* If you don't support Client Credentials Grant then doesn't need implementing.
150150
*

scala-oauth2-core/src/main/scala/scalaoauth2/provider/GrantHandler.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ trait GrantHandler {
99

1010
def handleRequest[U](request: AuthorizationRequest, dataHandler: DataHandler[U]): Future[GrantHandlerResult]
1111

12-
1312
/**
1413
* Returns valid access token.
1514
*
@@ -41,11 +40,11 @@ trait GrantHandler {
4140
class RefreshToken(clientCredentialFetcher: ClientCredentialFetcher) extends GrantHandler {
4241

4342
override def handleRequest[U](request: AuthorizationRequest, dataHandler: DataHandler[U]): Future[GrantHandlerResult] = {
44-
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("BadRequest"))
43+
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("Authorization header is invalid"))
4544
val refreshToken = request.requireRefreshToken
4645

4746
dataHandler.findAuthInfoByRefreshToken(refreshToken).flatMap { authInfoOption =>
48-
val authInfo = authInfoOption.getOrElse(throw new InvalidGrant("NotFound"))
47+
val authInfo = authInfoOption.getOrElse(throw new InvalidGrant("Authorized information is not found by the refresh token"))
4948
if (authInfo.clientId != clientCredential.clientId) {
5049
throw new InvalidClient
5150
}
@@ -66,7 +65,7 @@ class RefreshToken(clientCredentialFetcher: ClientCredentialFetcher) extends Gra
6665
class Password(clientCredentialFetcher: ClientCredentialFetcher) extends GrantHandler {
6766

6867
override def handleRequest[U](request: AuthorizationRequest, dataHandler: DataHandler[U]): Future[GrantHandlerResult] = {
69-
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("BadRequest"))
68+
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("Authorization header is invalid"))
7069
val username = request.requireUsername
7170
val password = request.requirePassword
7271

@@ -84,13 +83,13 @@ class Password(clientCredentialFetcher: ClientCredentialFetcher) extends GrantHa
8483
class ClientCredentials(clientCredentialFetcher: ClientCredentialFetcher) extends GrantHandler {
8584

8685
override def handleRequest[U](request: AuthorizationRequest, dataHandler: DataHandler[U]): Future[GrantHandlerResult] = {
87-
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("BadRequest"))
86+
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("Authorization header is invalid"))
8887
val clientSecret = clientCredential.clientSecret
8988
val clientId = clientCredential.clientId
9089
val scope = request.scope
9190

9291
dataHandler.findClientUser(clientId, clientSecret, scope).flatMap { userOption =>
93-
val user = userOption.getOrElse(throw new InvalidGrant())
92+
val user = userOption.getOrElse(throw new InvalidGrant("client_id or client_secret or scope is incorrect"))
9493
val authInfo = AuthInfo(user, clientId, scope, None)
9594

9695
issueAccessToken(dataHandler, authInfo)
@@ -102,13 +101,13 @@ class ClientCredentials(clientCredentialFetcher: ClientCredentialFetcher) extend
102101
class AuthorizationCode(clientCredentialFetcher: ClientCredentialFetcher) extends GrantHandler {
103102

104103
override def handleRequest[U](request: AuthorizationRequest, dataHandler: DataHandler[U]): Future[GrantHandlerResult] = {
105-
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("BadRequest"))
104+
val clientCredential = clientCredentialFetcher.fetch(request).getOrElse(throw new InvalidRequest("Authorization header is invalid"))
106105
val clientId = clientCredential.clientId
107106
val code = request.requireCode
108107
val redirectUri = request.redirectUri
109108

110109
dataHandler.findAuthInfoByCode(code).flatMap { authInfoOption =>
111-
val authInfo = authInfoOption.getOrElse(throw new InvalidGrant())
110+
val authInfo = authInfoOption.getOrElse(throw new InvalidGrant("Authorized information is not found by the code"))
112111
if (authInfo.clientId != clientId) {
113112
throw new InvalidClient
114113
}

scala-oauth2-core/src/main/scala/scalaoauth2/provider/ProtectedResource.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ trait ProtectedResource {
1313
}.map { fetcher =>
1414
val result = fetcher.fetch(request)
1515
dataHandler.findAccessToken(result.token).flatMap { optionalToken =>
16-
val token = optionalToken.getOrElse(throw new InvalidToken("Not found the access token"))
16+
val token = optionalToken.getOrElse(throw new InvalidToken("The access token is not found"))
1717
if (dataHandler.isAccessTokenExpired(token)) {
1818
throw new ExpiredToken()
1919
}
2020

21-
dataHandler.findAuthInfoByAccessToken(token).map(_.map(Right(_)).getOrElse(Left(new InvalidToken("Invalid the access token"))))
21+
dataHandler.findAuthInfoByAccessToken(token).map(_.map(Right(_)).getOrElse(Left(new InvalidToken("The access token is invalid"))))
2222
}.recover {
2323
case e: OAuthError => Left(e)
2424
}
25-
}.getOrElse(throw new InvalidRequest("Access token was not specified"))
25+
}.getOrElse(throw new InvalidRequest("Access token is not found"))
2626
} catch {
2727
case e: OAuthError => Future.successful(Left(e))
2828
}

scala-oauth2-core/src/main/scala/scalaoauth2/provider/TokenEndpoint.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ trait TokenEndpoint {
1515
)
1616

1717
def handleRequest[U](request: AuthorizationRequest, dataHandler: DataHandler[U]): Future[Either[OAuthError, GrantHandlerResult]] = try {
18-
val grantType = request.grantType.getOrElse(throw new InvalidRequest("the grant_type is not found"))
19-
val handler = handlers.get(grantType).getOrElse(throw new UnsupportedGrantType("the grant_type isn't supported"))
20-
val clientCredential = fetcher.fetch(request).getOrElse(throw new InvalidRequest("client credential is not found"))
18+
val grantType = request.grantType.getOrElse(throw new InvalidRequest("grant_type is not found"))
19+
val handler = handlers.get(grantType).getOrElse(throw new UnsupportedGrantType("The grant_type is not supported"))
20+
val clientCredential = fetcher.fetch(request).getOrElse(throw new InvalidRequest("Client credential is not found"))
2121

2222
dataHandler.validateClient(clientCredential.clientId, clientCredential.clientSecret, grantType).flatMap { validClient =>
2323
if (!validClient) {

scala-oauth2-core/src/test/scala/scalaoauth2/provider/ProtectedResourceSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ProtectedResourceSpec extends FlatSpec with ScalaFutures {
8585
case _ =>
8686
}
8787
}
88-
e.description should be ("Access token was not specified")
88+
e.description should be ("Access token is not found")
8989
}
9090
}
9191

@@ -112,7 +112,7 @@ class ProtectedResourceSpec extends FlatSpec with ScalaFutures {
112112
case _ =>
113113
}
114114
}
115-
e.description should be ("Not found the access token")
115+
e.description should be ("The access token is not found")
116116
}
117117
}
118118

@@ -139,7 +139,7 @@ class ProtectedResourceSpec extends FlatSpec with ScalaFutures {
139139
case _ =>
140140
}
141141
}
142-
e.description should be ("Invalid the access token")
142+
e.description should be ("The access token is invalid")
143143
}
144144
}
145145
}

scala-oauth2-core/src/test/scala/scalaoauth2/provider/TokenEndPointSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TokenEndPointSpec extends FlatSpec with ScalaFutures {
4949
case _ =>
5050
}
5151
}
52-
e.description should be ("the grant_type is not found")
52+
e.description should be ("grant_type is not found")
5353
}
5454
}
5555

@@ -69,7 +69,7 @@ class TokenEndPointSpec extends FlatSpec with ScalaFutures {
6969
case _ =>
7070
}
7171
}
72-
e.description should be ("the grant_type isn't supported")
72+
e.description should be ("The grant_type is not supported")
7373
}
7474
}
7575

@@ -89,7 +89,7 @@ class TokenEndPointSpec extends FlatSpec with ScalaFutures {
8989
case _ =>
9090
}
9191
}
92-
e.description should be ("client credential is not found")
92+
e.description should be ("Client credential is not found")
9393
}
9494
}
9595

0 commit comments

Comments
 (0)