@@ -13,60 +13,40 @@ import org.springframework.security.oauth2.server.resource.authentication.JwtAut
1313
1414fun getCurrentAuthentication (): Authentication ? = SecurityContextHolder .getContext().authentication
1515
16- fun getCurrentUserName (): String? = getCurrentAuthentication()?.name
17-
18- fun getCurrentAuthenticatedUserName () =
19- getCurrentUserName()
16+ fun getCurrentAuthenticatedUserName (configuration : CsmPlatformProperties ): String {
17+ return getValueFromAuthenticatedToken {
18+ val jwtClaimsSet = JWTParser .parse(it).jwtClaimsSet
19+ jwtClaimsSet.getStringClaim(" name" )
20+ ? : jwtClaimsSet.getStringClaim(configuration.authorization.applicationIdJwtClaim)
2021 ? : throw IllegalStateException (" User Authentication not found in Security Context" )
21-
22- 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
3522 }
3623}
3724
38- 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()
25+ fun getCurrentAuthenticatedIssuer (): String {
26+ return getValueFromAuthenticatedToken { JWTParser .parse(it).jwtClaimsSet.issuer }
27+ }
4428
45- if (authentication is JwtAuthenticationToken ) {
46- return authentication.token.tokenValue.let {
47- JWTParser .parse(it).jwtClaimsSet.getStringClaim(configuration.authorization.mailJwtClaim)
48- }
29+ fun getCurrentAccountIdentifier (configuration : CsmPlatformProperties ): String {
30+ return getValueFromAuthenticatedToken {
31+ val jwtClaimsSet = JWTParser .parse(it).jwtClaimsSet
32+ jwtClaimsSet.getStringClaim(configuration.authorization.mailJwtClaim)
33+ ? : jwtClaimsSet.getStringClaim(configuration.authorization.applicationIdJwtClaim)
4934 }
35+ }
5036
51- return (authentication as BearerTokenAuthentication ).token.tokenValue.let {
52- JWTParser .parse(it).jwtClaimsSet.getStringClaim(configuration.authorization.mailJwtClaim)
37+ fun getCurrentAuthenticatedRoles (configuration : CsmPlatformProperties ): List <String > {
38+ return getValueFromAuthenticatedToken {
39+ JWTParser .parse(it).jwtClaimsSet.getStringListClaim(configuration.authorization.rolesJwtClaim)
5340 }
5441}
5542
56- fun getCurrentAuthenticatedRoles ( configuration : CsmPlatformProperties ): List < String > {
43+ fun < T > getValueFromAuthenticatedToken ( actionLambda : ( String ) -> T ): T {
5744 if (getCurrentAuthentication() == null ) {
5845 throw IllegalStateException (" User Authentication not found in Security Context" )
5946 }
60-
6147 val authentication = getCurrentAuthentication()
62-
6348 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)
49+ return authentication.token.tokenValue.let { actionLambda(it) }
7150 }
51+ return (authentication as BearerTokenAuthentication ).token.tokenValue.let { actionLambda(it) }
7252}
0 commit comments