1- package org .cryptomator .hub ;
1+ package org .cryptomator .hub . keycloak ;
22
33import jakarta .enterprise .context .ApplicationScoped ;
44import jakarta .inject .Inject ;
5- import org .cryptomator .hub .entities .Authority ;
6- import org .cryptomator .hub .entities .Group ;
7- import org .cryptomator .hub .entities .User ;
85import org .eclipse .microprofile .config .inject .ConfigProperty ;
96import org .keycloak .admin .client .Keycloak ;
107import org .keycloak .admin .client .resource .RealmResource ;
1916import java .util .stream .Collectors ;
2017
2118@ ApplicationScoped
22- public class KeycloakRemoteUserProvider implements RemoteUserProvider {
19+ public class KeycloakAuthorityProvider {
2320
2421 //visible for testing
2522 static final int MAX_COUNT_PER_REQUEST = 5_000 ;
@@ -30,15 +27,14 @@ public class KeycloakRemoteUserProvider implements RemoteUserProvider {
3027 @ ConfigProperty (name = "hub.keycloak.realm" )
3128 String keycloakRealm ;
3229
33- @ Override
34- public List <User > users () {
30+ public List <KeycloakUserDto > users () {
3531 return users (keycloak .realm (keycloakRealm ));
3632 }
3733
3834 //visible for testing
39- List <User > users (RealmResource realm ) {
40- List <User > users = new ArrayList <>();
41- List <User > currentRequestedUsers ;
35+ List <KeycloakUserDto > users (RealmResource realm ) {
36+ List <KeycloakUserDto > users = new ArrayList <>();
37+ List <KeycloakUserDto > currentRequestedUsers ;
4238
4339 do {
4440 currentRequestedUsers = realm .users ().list (users .size (), MAX_COUNT_PER_REQUEST ).stream ().map (this ::mapToUser ).toList ();
@@ -52,7 +48,7 @@ List<User> users(RealmResource realm) {
5248 }
5349
5450 //visible for testing
55- Optional <User > cryptomatorCliUser (RealmResource realm ) {
51+ Optional <KeycloakUserDto > cryptomatorCliUser (RealmResource realm ) {
5652 var clients = realm .clients ().findByClientId ("cryptomatorhub-cli" );
5753 if (clients .isEmpty ()) {
5854 return Optional .empty ();
@@ -63,38 +59,29 @@ Optional<User> cryptomatorCliUser(RealmResource realm) {
6359 return Optional .of (mapToUser (clientUser ));
6460 }
6561
66- private User mapToUser (UserRepresentation userRepresentation ) {
67- var userEntity = new User ();
68- userEntity .setId (userRepresentation .getId ());
69- userEntity .setName (userRepresentation .getUsername ());
70- userEntity .setEmail (userRepresentation .getEmail ());
71- parsePictureUrl (userRepresentation .getAttributes ()).ifPresent (userEntity ::setPictureUrl );
72- return userEntity ;
62+ private KeycloakUserDto mapToUser (UserRepresentation userRepresentation ) {
63+ var pictureUrl = parsePictureUrl (userRepresentation .getAttributes ());
64+ return new KeycloakUserDto (userRepresentation .getId (), userRepresentation .getUsername (), userRepresentation .getEmail (), pictureUrl );
7365 }
7466
75- private Optional < String > parsePictureUrl (Map <String , List <String >> attributes ) {
67+ private String parsePictureUrl (Map <String , List <String >> attributes ) {
7668 try {
77- return Optional . ofNullable ( attributes .get ("picture" ).get (0 ) );
69+ return attributes .get ("picture" ).get (0 );
7870 } catch (NullPointerException e ) {
79- return Optional . empty () ;
71+ return null ;
8072 }
8173 }
8274
83- @ Override
84- public List <Group > groups () {
75+ public List <KeycloakGroupDto > groups () {
8576 return groups (keycloak .realm (keycloakRealm ));
8677 }
8778
8879 //visible for testing
89- List <Group > groups (RealmResource realm ) {
80+ List <KeycloakGroupDto > groups (RealmResource realm ) {
9081 return deepCollectGroups (realm ).stream ().map (group -> {
9182 // TODO add sub groups and the members of the sub group to it too using `group.getSubGroups()` recursively
9283 var members = deepCollectMembers (realm , group .getId ());
93- var groupEntity = new Group ();
94- groupEntity .setId (group .getId ());
95- groupEntity .setName (group .getName ());
96- groupEntity .setMembers (members );
97- return groupEntity ;
84+ return new KeycloakGroupDto (group .getId (), group .getName (), members );
9885 }).toList ();
9986 }
10087
@@ -112,7 +99,7 @@ private List<GroupRepresentation> deepCollectGroups(RealmResource realm) {
11299 return groups ;
113100 }
114101
115- private Set <Authority > deepCollectMembers (RealmResource realm , String groupId ) {
102+ private Set <KeycloakUserDto > deepCollectMembers (RealmResource realm , String groupId ) {
116103 var group = realm .groups ().group (groupId );
117104
118105 List <UserRepresentation > members = new ArrayList <>();
0 commit comments