Skip to content

Commit 9603364

Browse files
committed
added support for Keycloak
1 parent e730596 commit 9603364

6 files changed

Lines changed: 52 additions & 10 deletions

File tree

db-scripts/mariadb-schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ CREATE TABLE `user_account` (
6464
`last_name` VARCHAR(255) DEFAULT NULL,
6565
`locked` BIT(1) NOT NULL,
6666
`password` VARCHAR(60) DEFAULT NULL,
67-
`provider` ENUM('LOCAL','FACEBOOK','GOOGLE','APPLE') DEFAULT NULL,
67+
`provider` ENUM('LOCAL','FACEBOOK','GOOGLE','APPLE','KEYCLOAK') DEFAULT NULL,
6868
`registration_date` DATETIME(6) DEFAULT NULL,
6969
`failed_login_attempts` INT(11) NOT NULL,
7070
`locked_date` DATETIME(6) DEFAULT NULL,

src/main/java/com/digitalsanctuary/spring/user/controller/UserPageController.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ public class UserPageController {
2727
@Value("${user.registration.googleEnabled}")
2828
private boolean googleEnabled;
2929

30+
@Value("${user.registration.keycloakEnabled}")
31+
private boolean keycloakEnabled;
32+
3033
/**
3134
* Login Page.
3235
*
3336
* @param userDetails the user details
34-
* @param session the session
35-
* @param model the model
37+
* @param session the session
38+
* @param model the model
3639
*
3740
* @return the string
3841
*/
@@ -45,15 +48,16 @@ public String login(@AuthenticationPrincipal DSUserDetails userDetails, HttpSess
4548
}
4649
model.addAttribute("googleEnabled", googleEnabled);
4750
model.addAttribute("facebookEnabled", facebookEnabled);
51+
model.addAttribute("keycloakEnabled", keycloakEnabled);
4852
return "user/login";
4953
}
5054

5155
/**
5256
* Register Page.
5357
*
5458
* @param userDetails the user details
55-
* @param session the session
56-
* @param model the model
59+
* @param session the session
60+
* @param model the model
5761
* @return the string
5862
*/
5963
@GetMapping("${user.security.registrationURI:/user/register.html}")
@@ -65,6 +69,7 @@ public String register(@AuthenticationPrincipal DSUserDetails userDetails, HttpS
6569
}
6670
model.addAttribute("googleEnabled", googleEnabled);
6771
model.addAttribute("facebookEnabled", facebookEnabled);
72+
model.addAttribute("keycloakEnabled", keycloakEnabled);
6873
return "user/register";
6974
}
7075

@@ -82,8 +87,8 @@ public String registrationPending() {
8287
* Registration complete.
8388
*
8489
* @param userDetails the user details
85-
* @param session the session
86-
* @param model the model
90+
* @param session the session
91+
* @param model the model
8792
*
8893
* @return the string
8994
*/
@@ -134,10 +139,11 @@ public String forgotPasswordChange() {
134139
return "user/forgot-password-change";
135140
}
136141

142+
137143
/**
138144
* @param userDetails the user details
139-
* @param request the request
140-
* @param model the model
145+
* @param request the request
146+
* @param model the model
141147
* @return String
142148
*/
143149
@GetMapping("${user.security.updateUserURI:/user/update-user.html}")

src/main/java/com/digitalsanctuary/spring/user/persistence/model/User.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public enum Provider {
4040
/**
4141
* Login using Apple as the authentication provider.
4242
*/
43-
APPLE
43+
APPLE,
44+
45+
/**
46+
* Login using Keycloak as the authentication provider.
47+
*/
48+
KEYCLOAK
4449
}
4550

4651
/** The id. */

src/main/java/com/digitalsanctuary/spring/user/service/DSOAuth2UserService.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public User handleOAuthLoginSuccess(String registrationId, OAuth2User oAuth2User
5757
user = getUserFromGoogleOAuth2User(oAuth2User);
5858
} else if (registrationId.equalsIgnoreCase("facebook")) {
5959
user = getUserFromFacebookOAuth2User(oAuth2User);
60+
} else if (registrationId.equalsIgnoreCase("keycloak")) {
61+
user = getUserFromKeycloakOAuth2User(oAuth2User);
6062
} else {
6163
log.error("Sorry! Login with " + registrationId + " is not supported yet.");
6264
throw new OAuth2AuthenticationException(new OAuth2Error("Login Exception"),
@@ -141,6 +143,28 @@ public User getUserFromGoogleOAuth2User(OAuth2User principal) {
141143
return user;
142144
}
143145

146+
/**
147+
*
148+
* Retrieves user information from a Keycloak OAuth2User object.
149+
*
150+
* @param principal The OAuth2User object containing information about the authenticated user.
151+
* @return A User object representing the authenticated user.
152+
*/
153+
public User getUserFromKeycloakOAuth2User(OAuth2User principal) {
154+
log.debug("Getting user info from Google OAuth2 provider with principal: {}", principal);
155+
if (principal == null) {
156+
return null;
157+
}
158+
log.debug("Principal attributes: {}", principal.getAttributes());
159+
User user = new User();
160+
user.setEmail(principal.getAttribute("email"));
161+
user.setFirstName(principal.getAttribute("firstName"));
162+
user.setLastName(principal.getAttribute("lastName"));
163+
user.setId(principal.getAttribute("lastName"));
164+
user.setProvider(User.Provider.KEYCLOAK);
165+
return user;
166+
}
167+
144168
/**
145169
* Retrieves user information from a Facebook OAuth2User object.
146170
*

src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"type": "java.lang.String",
1111
"description": "A description for 'user.registration.google-enabled'"
1212
},
13+
{
14+
"name": "user.registration.keycloak-enabled",
15+
"type": "java.lang.String",
16+
"description": "A description for 'user.registration.keycloak-enabled'"
17+
},
1318
{
1419
"name": "user.registration.send-verification-email",
1520
"type": "java.lang.String",

src/main/resources/config/dsspringuserconfig.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ user.registration.googleEnabled=false
2929
# If true, Facebook OAuth2 will be enabled for registration.
3030
user.registration.facebookEnabled=false
3131

32+
# If true, Keycloak OAuth2 will be enabled for registration.
33+
user.registration.keycloakEnabled=false
3234

3335

3436
# The number of failed login attempts before the user account is locked out. Set this to 0 to disable account lockout.

0 commit comments

Comments
 (0)