-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifies structure and adds support for refresh and id tokens
- Loading branch information
1 parent
5ec7e51
commit a7b872f
Showing
29 changed files
with
653 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/de/solugo/oidc/config/SerializationConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package de.solugo.oidc.config | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies | ||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class SerializationConfiguration { | ||
|
||
@Bean | ||
fun jacksonCustomizer() = Jackson2ObjectMapperBuilderCustomizer { | ||
it.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 101 additions & 12 deletions
113
src/main/kotlin/de/solugo/oidc/controller/TokenController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,129 @@ | ||
package de.solugo.oidc.controller | ||
|
||
|
||
import de.solugo.oidc.ConfigurationProvider | ||
import de.solugo.oidc.grant.Grant | ||
import de.solugo.oidc.service.TokenService | ||
import de.solugo.oidc.util.badRequest | ||
import de.solugo.oidc.token.* | ||
import de.solugo.oidc.util.plus | ||
import de.solugo.oidc.util.scopes | ||
import de.solugo.oidc.util.sessionId | ||
import de.solugo.oidc.util.uuid | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import org.jose4j.jwt.JwtClaims | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.MediaType | ||
import org.springframework.util.MultiValueMap | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.server.ServerWebExchange | ||
import org.springframework.web.util.UriComponentsBuilder | ||
import java.time.Instant | ||
|
||
@RestController | ||
class TokenController( | ||
private val grants: List<Grant>, | ||
private val grants: List<TokenGrant>, | ||
private val tokenService: TokenService, | ||
private val tokenProcessors: List<TokenProcessor>, | ||
) : ConfigurationProvider { | ||
|
||
private val logger = LoggerFactory.getLogger(javaClass) | ||
|
||
override fun provide(builder: UriComponentsBuilder) = mapOf( | ||
"token_endpoint" to builder.replacePath("/token").toUriString(), | ||
) | ||
|
||
@PostMapping("/token") | ||
@PostMapping("/token", consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE]) | ||
suspend fun createToken( | ||
exchange: ServerWebExchange, | ||
uriBuilder: UriComponentsBuilder, | ||
) = run { | ||
val params = exchange.formData.awaitSingle() | ||
val type = params.getFirst("grant_type") ?: badRequest("Missing grant_type") | ||
val provider = grants.firstOrNull { it.type == type } ?: badRequest("Grant type '$type' is not supported") | ||
val claims = JwtClaims() | ||
val parameters = exchange.formData.awaitSingle() | ||
|
||
try { | ||
process( | ||
issuer = uriBuilder.replacePath("/").toUriString(), | ||
parameters = parameters, | ||
) | ||
} catch (ex: TokenException) { | ||
mapOf( | ||
"error" to ex.error, | ||
"errorDescription" to ex.description, | ||
"errorUri" to ex.uri, | ||
) | ||
} | ||
} | ||
|
||
private suspend fun process( | ||
issuer: String, | ||
parameters: MultiValueMap<String, String>, | ||
): Map<String, Any> = try { | ||
val type = parameters.getFirst("grant_type") ?: throw TokenException( | ||
error = TokenError.InvalidRequest, | ||
description = "Request is missing grant_type parameter" | ||
) | ||
val grant = grants.firstOrNull { it.type == type } ?: throw TokenException( | ||
error = TokenError.InvalidRequest, | ||
description = "Grant type '$type' is not supported" | ||
) | ||
|
||
val context = TokenContext( | ||
issuer = issuer, | ||
parameters = parameters, | ||
scopes = parameters.getFirst("scope")?.split(" ")?.toSet(), | ||
) | ||
|
||
tokenProcessors.process(TokenProcessor.Step.PRE, context) | ||
|
||
grant.process(context) | ||
|
||
tokenProcessors.process(TokenProcessor.Step.POST, context) | ||
tokenProcessors.process(TokenProcessor.Step.VALIDATE, context) | ||
tokenProcessors.process(TokenProcessor.Step.CLAIMS, context) | ||
|
||
val commonClaims = context.commonClaims | ||
val idClaims = commonClaims + context.idClaims | ||
val accessClaims = commonClaims + context.accessClaims | ||
val refreshClaims = commonClaims + context.refreshClaims | ||
|
||
provider.process(params, claims) | ||
listOf(idClaims, accessClaims, refreshClaims).forEach { claims -> | ||
claims.issuer = issuer | ||
claims.jwtId = uuid() | ||
claims.sessionId = claims.sessionId ?: uuid() | ||
} | ||
|
||
mapOf( | ||
"access_token" to tokenService.createToken(uriBuilder.replacePath("/"), claims) | ||
buildMap { | ||
put("token_type", "Bearer") | ||
|
||
accessClaims.takeIf { it.claimsMap.isNotEmpty() }?.also { claims -> | ||
put("access_token", tokenService.encodeJwt(context.issuer, claims)) | ||
|
||
claims.expirationTime?.also { | ||
put("expires_in", it.value - (claims.issuedAt?.value ?: Instant.now().epochSecond)) | ||
} | ||
} | ||
refreshClaims.takeIf { it.claimsMap.isNotEmpty() }?.also { claims -> | ||
if (claims.scopes?.contains("offline_access") != true) return@also | ||
put("refresh_token", tokenService.encodeJwt(context.issuer, claims)) | ||
} | ||
idClaims.takeIf { it.claimsMap.isNotEmpty() }?.also { claims -> | ||
if (claims.scopes?.contains("openid") != true) return@also | ||
put("id_token", tokenService.encodeJwt(context.issuer, claims)) | ||
} | ||
} | ||
} catch (ex: Exception) { | ||
logger.error("Error processing token request", ex) | ||
throw TokenException( | ||
error = TokenError.ServerError, | ||
description = ex.message, | ||
cause = ex, | ||
) | ||
} | ||
|
||
private suspend fun List<TokenProcessor>.process(step: TokenProcessor.Step, context: TokenContext) { | ||
this.forEach { | ||
if (it.step == step) { | ||
it.process(context) | ||
} | ||
} | ||
} | ||
|
||
} |
22 changes: 0 additions & 22 deletions
22
src/main/kotlin/de/solugo/oidc/grant/ClientCredentialsGrant.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.solugo.oidc.model | ||
|
||
import java.time.Duration | ||
|
||
|
||
interface Client { | ||
val id: String | ||
val allowedGrants: Set<String> | ||
val accessTokenLifetime: Duration? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.solugo.oidc.model | ||
|
||
interface User { | ||
val id: String | ||
val username: String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.