Skip to content

Commit 0c4fb7a

Browse files
committed
fix: avoid error 500 if no claims in token
1 parent 854eb2e commit 0c4fb7a

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/main/java/fr/insee/genesis/configuration/auth/security/OIDCSecurityConfig.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,36 @@ public Collection<GrantedAuthority> convert(Jwt source) {
8585
String[] claimPath = inseeSecurityTokenProperties.getOidcClaimRole().split("\\.");
8686
Map<String, Object> claims = source.getClaims();
8787
try {
88+
if (!claims.isEmpty()) {
89+
for (int i = 0; i < claimPath.length - 1; i++) {
90+
claims = (Map<String, Object>) claims.get(claimPath[i]);
91+
}
8892

89-
for (int i = 0; i < claimPath.length - 1; i++) {
90-
claims = (Map<String, Object>) claims.get(claimPath[i]);
91-
}
92-
93-
List<String> tokenClaims = (List<String>) claims.getOrDefault(claimPath[claimPath.length - 1], List.of());
94-
// Collect distinct values from mapping associated with input keys
95-
List<String> claimedRoles = tokenClaims.stream()
96-
.filter(roleConfiguration.getRolesByClaim()::containsKey) // Ensure the key exists in the mapping
97-
.flatMap(key -> roleConfiguration.getRolesByClaim().get(key).stream()) // Get the list of values associated with the key
98-
.distinct() // Remove duplicates
99-
.toList();
93+
List<String> tokenClaims = (List<String>) claims.getOrDefault(claimPath[claimPath.length - 1], List.of());
94+
// Collect distinct values from mapping associated with input keys
95+
List<String> claimedRoles = tokenClaims.stream()
96+
.filter(roleConfiguration.getRolesByClaim()::containsKey) // Ensure the key exists in the mapping
97+
.flatMap(key -> roleConfiguration.getRolesByClaim().get(key).stream()) // Get the list of values associated with the key
98+
.distinct() // Remove duplicates
99+
.toList();
100100

101-
return Collections.unmodifiableCollection(claimedRoles.stream().map(s -> new GrantedAuthority() {
102-
@Override
103-
public String getAuthority() {
104-
return ROLE_PREFIX + s;
105-
}
101+
return Collections.unmodifiableCollection(claimedRoles.stream().map(s -> new GrantedAuthority() {
102+
@Override
103+
public String getAuthority() {
104+
return ROLE_PREFIX + s;
105+
}
106106

107-
@Override
108-
public String toString() {
109-
return getAuthority();
110-
}
111-
}).toList());
107+
@Override
108+
public String toString() {
109+
return getAuthority();
110+
}
111+
}).toList());
112+
}
112113
} catch (ClassCastException e) {
113114
// role path not correctly found, assume that no role for this user
114115
return List.of();
115116
}
117+
return List.of();
116118
}
117119
};
118120
}

0 commit comments

Comments
 (0)