11package com .faforever .api .security ;
22
3- import com .nimbusds .jose .shaded .gson .JsonArray ;
4- import com .nimbusds .jose .shaded .gson .JsonObject ;
3+ import java .util .List ;
4+ import java .util .Map ;
5+
56import org .springframework .core .convert .converter .Converter ;
67import org .springframework .security .authentication .AbstractAuthenticationToken ;
78import org .springframework .security .oauth2 .jwt .Jwt ;
89
9- import java .util .List ;
10- import java .util .Optional ;
11- import java .util .stream .Stream ;
12- import java .util .stream .StreamSupport ;
13-
1410/**
1511 * Jwt converter that reads scopes + custom FAF roles from the token extension.
1612 */
@@ -31,32 +27,20 @@ private int extractUserId(Jwt source) {
3127 }
3228
3329 private String extractUsername (Jwt source ) {
34- JsonObject ext = source .getClaim ("ext" );
35- String username = Optional .ofNullable (ext )
36- .flatMap (jsonObject -> Optional .ofNullable (jsonObject .get ("username" ).getAsString ()))
37- .orElse ("[undefined]" );
38-
30+ Map <String , Object > ext = source .getClaim ("ext" );
31+ String username = (String ) ext .getOrDefault ("username" , "[undefined]" );
3932 return username ;
4033 }
4134
4235 private List <FafScope > extractScopes (Jwt source ) {
43- JsonArray jwtScopes = source .getClaim ("scp" );
44- List <FafScope > scopes = Optional .ofNullable (jwtScopes )
45- .map (jsonArray -> StreamSupport .stream (jsonArray .spliterator (), false ).map (scope -> new FafScope (scope .getAsString ())))
46- .orElse (Stream .empty ())
47- .toList ();
48-
36+ List <String > jwtScopes = source .getClaim ("scp" );
37+ List <FafScope > scopes = jwtScopes .stream ().map (FafScope ::new ).toList ();
4938 return scopes ;
5039 }
5140
5241 public List <FafRole > extractRoles (Jwt source ) {
53- JsonObject ext = source .getClaim ("ext" );
54- List <FafRole > roles = Optional .ofNullable (ext )
55- .flatMap (jsonObject -> Optional .ofNullable ((JsonArray ) jsonObject .get ("roles" )))
56- .map (jsonArray -> StreamSupport .stream (jsonArray .spliterator (), false ).map (role -> new FafRole (role .getAsString ())))
57- .orElse (Stream .empty ())
58- .toList ();
59-
42+ Map <String , Object > ext = source .getClaim ("ext" );
43+ final List <FafRole > roles = ((List <String >) ext .getOrDefault ("roles" , List .of ())).stream ().map (FafRole ::new ).toList ();
6044 return roles ;
6145 }
6246}
0 commit comments