Skip to content

Commit 11c6d46

Browse files
committed
chore(authorization): move extractUserId to companion object
1 parent 64053df commit 11c6d46

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

authorization/src/main/kotlin/org/modelix/authorization/AccessTokenPrincipal.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.auth0.jwt.interfaces.DecodedJWT
44
import io.ktor.server.auth.Principal
55

66
class AccessTokenPrincipal(val jwt: DecodedJWT) : Principal {
7-
fun getUserName(): String? = ModelixJWTUtil().extractUserId(jwt)
7+
fun getUserName(): String? = ModelixJWTUtil.extractUserId(jwt)
88

99
override fun equals(other: Any?): Boolean {
1010
if (other !is AccessTokenPrincipal) return false

authorization/src/main/kotlin/org/modelix/authorization/ModelixJWTUtil.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ class ModelixJWTUtil {
253253
}
254254

255255
fun extractUserId(jwt: DecodedJWT): String? {
256-
return jwt.getClaim(KeycloakTokenConstants.EMAIL)?.asString()
257-
?: jwt.getClaim(KeycloakTokenConstants.PREFERRED_USERNAME)?.asString()
256+
return Companion.extractUserId(jwt)
258257
}
259258

260259
fun extractUserRoles(jwt: DecodedJWT): List<String> {
@@ -327,6 +326,13 @@ class ModelixJWTUtil {
327326
builder.claim(name, value)
328327
}
329328
}
329+
330+
companion object {
331+
fun extractUserId(jwt: DecodedJWT): String? {
332+
return jwt.getClaim(KeycloakTokenConstants.EMAIL)?.asString()
333+
?: jwt.getClaim(KeycloakTokenConstants.PREFERRED_USERNAME)?.asString()
334+
}
335+
}
330336
}
331337

332338
class KtorResourceRetriever(val client: HttpClient) : AbstractRestrictedResourceRetriever(1000, 1000, 0) {

0 commit comments

Comments
 (0)