Skip to content

Commit 3c960f4

Browse files
committed
Fix JwtAuthenticationToken cannot be cast to class BearerTokenAuthentication error
1 parent 6c6aee9 commit 3c960f4

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.nimbusds.jwt.JWTParser
99
import org.springframework.security.core.Authentication
1010
import org.springframework.security.core.context.SecurityContextHolder
1111
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication
12+
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken
1213

1314
fun getCurrentAuthentication(): Authentication? = SecurityContextHolder.getContext().authentication
1415

@@ -23,17 +24,31 @@ fun getCurrentAuthenticatedIssuer(): String {
2324
throw IllegalStateException("User Authentication not found in Security Context")
2425
}
2526

26-
val authentication = getCurrentAuthentication() as BearerTokenAuthentication
27-
return authentication.token.tokenValue.let { JWTParser.parse(it).jwtClaimsSet.issuer }
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+
}
2836
}
2937

3038
fun getCurrentAuthenticatedMail(configuration: CsmPlatformProperties): String {
3139
if (getCurrentAuthentication() == null) {
3240
throw IllegalStateException("User Authentication not found in Security Context")
3341
}
3442

35-
val authentication = getCurrentAuthentication() as BearerTokenAuthentication
36-
return authentication.token.tokenValue.let {
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 {
3752
JWTParser.parse(it).jwtClaimsSet.getStringClaim(configuration.authorization.mailJwtClaim)
3853
}
3954
}
@@ -43,8 +58,15 @@ fun getCurrentAuthenticatedRoles(configuration: CsmPlatformProperties): List<Str
4358
throw IllegalStateException("User Authentication not found in Security Context")
4459
}
4560

46-
val authentication = getCurrentAuthentication() as BearerTokenAuthentication
47-
return authentication.token.tokenValue.let {
61+
val authentication = getCurrentAuthentication()
62+
63+
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 {
4870
JWTParser.parse(it).jwtClaimsSet.getStringListClaim(configuration.authorization.rolesJwtClaim)
4971
}
5072
}

0 commit comments

Comments
 (0)