Skip to content

Commit

Permalink
chore(authorization): deduplicate constant strings
Browse files Browse the repository at this point in the history
  • Loading branch information
slisson committed Dec 11, 2024
1 parent 0c04276 commit 028c396
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object ModelixAuthorization : BaseRouteScopedPlugin<IModelixAuthorizationConfig,
val token = JWT.create()
.withIssuer("modelix")
.withAudience("modelix")
.withClaim("email", "[email protected]")
.withClaim(KeycloakTokenConstants.EMAIL, "[email protected]")
.sign(Algorithm.HMAC256("unit-tests"))
// The signing algorithm and key isn't relevant because the token is already considered valid
// and the signature is never checked.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.modelix.authorization

object KeycloakTokenConstants {
val EMAIL = "email"
val PREFERRED_USERNAME = "preferred_username"
val REALM_ACCESS = "realm_access"
val REALM_ACCESS_ROLES = "roles"
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ fun createModelixAccessToken(hmac512key: String, user: String, grantedPermission
}
}

private fun Map<String, Any>?.readRolesArray(): List<String> {
return this?.get("roles") as? List<String> ?: emptyList()
}

fun ApplicationCall.getBearerToken(): String? {
val authHeader = request.parseAuthorizationHeader()
if (authHeader == null || authHeader.authScheme != AuthScheme.Bearer) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class ModelixJWTUtil {
}

val payload = JWTClaimsSet.Builder()
.claim("preferred_username", user)
.claim("permissions", grantedPermissions)
.claim(KeycloakTokenConstants.PREFERRED_USERNAME, user)
.claim(ModelixTokenConstants.PERMISSIONS, grantedPermissions)
.expirationTime(Date(Instant.now().plus(12, ChronoUnit.HOURS).toEpochMilli()))
.also { additionalTokenContent(TokenBuilder(it)) }
.build()
Expand All @@ -171,7 +171,7 @@ class ModelixJWTUtil {
}

fun extractPermissions(token: DecodedJWT): List<String>? {
return token.claims["permissions"]?.asList(String::class.java)
return token.claims[ModelixTokenConstants.PERMISSIONS]?.asList(String::class.java)
}

fun loadGrantedPermissions(token: DecodedJWT, evaluator: PermissionEvaluator) {
Expand All @@ -197,14 +197,14 @@ class ModelixJWTUtil {
}

fun extractUserId(jwt: DecodedJWT): String? {
return jwt.getClaim("email")?.asString()
?: jwt.getClaim("preferred_username")?.asString()
return jwt.getClaim(KeycloakTokenConstants.EMAIL)?.asString()
?: jwt.getClaim(KeycloakTokenConstants.PREFERRED_USERNAME)?.asString()
}

fun extractUserRoles(jwt: DecodedJWT): List<String> {
val keycloakRoles = jwt
.getClaim("realm_access")?.asMap()
?.get("roles")
.getClaim(KeycloakTokenConstants.REALM_ACCESS)?.asMap()
?.get(KeycloakTokenConstants.REALM_ACCESS_ROLES)
?.let { it as? List<*> }
?.mapNotNull { it as? String }
?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.modelix.authorization

object ModelixTokenConstants {
val PERMISSIONS = "permissions"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AccessControlDataTest {
@Test
fun `can grant permissions to identity tokens`() {
val token = JWT.create()
.withClaim("email", email)
.withClaim(KeycloakTokenConstants.EMAIL, email)
.sign(Algorithm.HMAC256("unit-tests"))
.let { JWT.decode(it) }
val data = AccessControlData().withGrantToUser(email, PermissionParts("r1", "write").fullId)
Expand Down

0 comments on commit 028c396

Please sign in to comment.