Skip to content

Commit 119f8e6

Browse files
authored
Fix non-null assumptions (#21)
1 parent 2479b1f commit 119f8e6

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

oauth2-server-core/src/main/java/nl/myndocs/oauth2/Oauth2TokenService.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ class Oauth2TokenService(
4444
throw InvalidRequestException(INVALID_REQUEST_FIELD_MESSAGE.format("password"))
4545
}
4646

47-
val requestedClient = clientService.clientOf(
48-
passwordGrantRequest.clientId!!
49-
)!!
47+
val requestedClient = clientService.clientOf(passwordGrantRequest.clientId!!) ?: throw InvalidClientException()
5048

5149
val authorizedGrantType = AuthorizedGrantType.PASSWORD
5250
if (!requestedClient.authorizedGrantTypes.contains(authorizedGrantType)) {
@@ -134,7 +132,7 @@ class Oauth2TokenService(
134132
throw InvalidGrantException()
135133
}
136134

137-
val client = clientService.clientOf(refreshToken.clientId)!!
135+
val client = clientService.clientOf(refreshToken.clientId) ?: throw InvalidClientException()
138136

139137
val authorizedGrantType = AuthorizedGrantType.REFRESH_TOKEN
140138
if (!client.authorizedGrantTypes.contains(authorizedGrantType)) {
@@ -293,9 +291,10 @@ class Oauth2TokenService(
293291
}
294292

295293
override fun userInfo(accessToken: String): UserInfo {
296-
val storedAccessToken = tokenStore.accessToken(accessToken)!!
297-
val client = clientService.clientOf(storedAccessToken.clientId)!!
298-
val identity = identityService.identityOf(client, storedAccessToken.username)!!
294+
val storedAccessToken = tokenStore.accessToken(accessToken) ?: throw InvalidGrantException()
295+
val client = clientService.clientOf(storedAccessToken.clientId) ?: throw InvalidClientException()
296+
val identity = identityService.identityOf(client, storedAccessToken.username)
297+
?: throw InvalidIdentityException()
299298

300299
return UserInfo(
301300
identity,
@@ -305,17 +304,15 @@ class Oauth2TokenService(
305304
}
306305

307306
private fun throwExceptionIfUnverifiedClient(clientRequest: ClientRequest) {
308-
if (clientRequest.clientId == null) {
309-
throw InvalidRequestException(INVALID_REQUEST_FIELD_MESSAGE.format("client_id"))
310-
}
307+
val clientId = clientRequest.clientId
308+
?: throw InvalidRequestException(INVALID_REQUEST_FIELD_MESSAGE.format("client_id"))
311309

312-
if (clientRequest.clientSecret == null) {
313-
throw InvalidRequestException(INVALID_REQUEST_FIELD_MESSAGE.format("client_secret"))
314-
}
310+
val clientSecret = clientRequest.clientSecret
311+
?: throw InvalidRequestException(INVALID_REQUEST_FIELD_MESSAGE.format("client_secret"))
315312

316-
val client = clientService.clientOf(clientRequest.clientId!!) ?: throw InvalidClientException()
313+
val client = clientService.clientOf(clientId) ?: throw InvalidClientException()
317314

318-
if (!clientService.validClient(client, clientRequest.clientSecret!!)) {
315+
if (!clientService.validClient(client, clientSecret)) {
319316
throw InvalidClientException()
320317
}
321318
}

0 commit comments

Comments
 (0)