Skip to content

Commit 1bdcf56

Browse files
icreatedicreated
authored andcommitted
Token lambda implementation
1 parent de8c75f commit 1bdcf56

File tree

1 file changed

+10
-35
lines changed

1 file changed

+10
-35
lines changed

src/main/kotlin/com/cosmotech/api/utils/SecurityUtils.kt

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,28 @@ fun getCurrentAuthenticatedUserName() =
2020
?: throw IllegalStateException("User Authentication not found in Security Context")
2121

2222
fun getCurrentAuthenticatedIssuer(): String {
23-
if (getCurrentAuthentication() == null) {
24-
throw IllegalStateException("User Authentication not found in Security Context")
25-
}
26-
27-
val authentication = getCurrentAuthentication()
28-
29-
if (authentication is JwtAuthenticationToken) {
30-
return authentication.token.tokenValue.let { JWTParser.parse(it).jwtClaimsSet.issuer }
31-
}
32-
33-
return (authentication as BearerTokenAuthentication).token.tokenValue.let {
34-
JWTParser.parse(it).jwtClaimsSet.issuer
35-
}
23+
return getValueFromAuthenticatedToken() { JWTParser.parse(it).jwtClaimsSet.issuer }
3624
}
3725

3826
fun getCurrentAuthenticatedMail(configuration: CsmPlatformProperties): String {
39-
if (getCurrentAuthentication() == null) {
40-
throw IllegalStateException("User Authentication not found in Security Context")
41-
}
42-
43-
val authentication = getCurrentAuthentication()
44-
45-
if (authentication is JwtAuthenticationToken) {
46-
return authentication.token.tokenValue.let {
47-
JWTParser.parse(it).jwtClaimsSet.getStringClaim(configuration.authorization.mailJwtClaim)
48-
}
49-
}
50-
51-
return (authentication as BearerTokenAuthentication).token.tokenValue.let {
27+
return getValueFromAuthenticatedToken() {
5228
JWTParser.parse(it).jwtClaimsSet.getStringClaim(configuration.authorization.mailJwtClaim)
5329
}
5430
}
5531

5632
fun getCurrentAuthenticatedRoles(configuration: CsmPlatformProperties): List<String> {
33+
return getValueFromAuthenticatedToken() {
34+
JWTParser.parse(it).jwtClaimsSet.getStringListClaim(configuration.authorization.rolesJwtClaim)
35+
}
36+
}
37+
38+
fun <T> getValueFromAuthenticatedToken(actionLambda: (String) -> T): T {
5739
if (getCurrentAuthentication() == null) {
5840
throw IllegalStateException("User Authentication not found in Security Context")
5941
}
60-
6142
val authentication = getCurrentAuthentication()
62-
6343
if (authentication is JwtAuthenticationToken) {
64-
return authentication.token.tokenValue.let {
65-
JWTParser.parse(it).jwtClaimsSet.getStringListClaim(configuration.authorization.rolesJwtClaim)
66-
}
67-
}
68-
69-
return (authentication as BearerTokenAuthentication).token.tokenValue.let {
70-
JWTParser.parse(it).jwtClaimsSet.getStringListClaim(configuration.authorization.rolesJwtClaim)
44+
return authentication.token.tokenValue.let { actionLambda(it) }
7145
}
46+
return (authentication as BearerTokenAuthentication).token.tokenValue.let { actionLambda(it) }
7247
}

0 commit comments

Comments
 (0)