Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
SametBasturkk committed Sep 4, 2024
1 parent db2d3e0 commit 02d95e7
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.keycloak.TokenVerifier;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.common.VerificationException;
import org.keycloak.representations.AccessToken;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.idm.CredentialRepresentation;
Expand Down Expand Up @@ -84,6 +85,10 @@ public void updatePlanRole(String username, String plan) {
public String userLogin(String username, String password) {
log.info("Attempting login for user: {}", username);
User user = userRepository.findByUsername(username);
if (user == null) {
log.error("User {} not found", username);
throw new RuntimeException("User not found");
}
user.setLastLogin(new Date());
try {
Keycloak keycloakClient = Keycloak.getInstance(
Expand Down Expand Up @@ -179,13 +184,19 @@ private String getUserAttribute(UserRepresentation user, String attributeName) {

private AccessToken parseToken(String token) {
try {
return TokenVerifier.create(token, AccessToken.class).getToken();
TokenVerifier<AccessToken> verifier = TokenVerifier.create(token, AccessToken.class);
verifier.withChecks(TokenVerifier.IS_ACTIVE);
return verifier.getToken();
} catch (VerificationException e) {
log.error("Token validation failed: {}", e.getMessage());
throw new RuntimeException("Invalid token", e);
} catch (Exception e) {
log.error("Failed to parse token: {}", e.getMessage());
throw new RuntimeException("Invalid token", e);
}
}


private UserResource getUserResource(String username) {
List<UserRepresentation> users = keycloak.userResource().search(username);
if (users.isEmpty()) {
Expand Down

0 comments on commit 02d95e7

Please sign in to comment.