forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KEYCLOAK-5629] Add credential endpoints to account service
- Loading branch information
Showing
5 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
...ices/src/main/java/org/keycloak/services/resources/account/AccountCredentialResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package org.keycloak.services.resources.account; | ||
|
||
import org.keycloak.credential.CredentialModel; | ||
import org.keycloak.credential.CredentialProvider; | ||
import org.keycloak.credential.PasswordCredentialProvider; | ||
import org.keycloak.credential.PasswordCredentialProviderFactory; | ||
import org.keycloak.events.EventBuilder; | ||
import org.keycloak.events.EventType; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.UserCredentialModel; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.services.ErrorResponse; | ||
import org.keycloak.utils.MediaType; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
|
||
public class AccountCredentialResource { | ||
|
||
private final KeycloakSession session; | ||
private final EventBuilder event; | ||
private final UserModel user; | ||
private final RealmModel realm; | ||
|
||
public AccountCredentialResource(KeycloakSession session, EventBuilder event, UserModel user) { | ||
this.session = session; | ||
this.event = event; | ||
this.user = user; | ||
realm = session.getContext().getRealm(); | ||
} | ||
|
||
@GET | ||
@Path("password") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public PasswordDetails passwordDetails() { | ||
PasswordCredentialProvider passwordProvider = (PasswordCredentialProvider) session.getProvider(CredentialProvider.class, PasswordCredentialProviderFactory.PROVIDER_ID); | ||
CredentialModel password = passwordProvider.getPassword(realm, user); | ||
|
||
PasswordDetails details = new PasswordDetails(); | ||
if (password != null) { | ||
details.setRegistered(true); | ||
details.setLastUpdate(password.getCreatedDate()); | ||
} else { | ||
details.setRegistered(false); | ||
} | ||
|
||
return details; | ||
} | ||
|
||
@POST | ||
@Path("password") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public Response passwordUpdate(PasswordUpdate update) { | ||
event.event(EventType.UPDATE_PASSWORD); | ||
|
||
UserCredentialModel cred = UserCredentialModel.password(update.getCurrentPassword()); | ||
if (!session.userCredentialManager().isValid(realm, user, cred)) { | ||
event.error(org.keycloak.events.Errors.INVALID_USER_CREDENTIALS); | ||
return ErrorResponse.error(Errors.INVALID_CREDENTIALS, Response.Status.BAD_REQUEST); | ||
} | ||
|
||
session.userCredentialManager().updateCredential(realm, user, UserCredentialModel.password(update.getNewPassword(), false)); | ||
|
||
return Response.ok().build(); | ||
} | ||
|
||
public static class PasswordDetails { | ||
|
||
private boolean registered; | ||
private long lastUpdate; | ||
|
||
public boolean isRegistered() { | ||
return registered; | ||
} | ||
|
||
public void setRegistered(boolean registered) { | ||
this.registered = registered; | ||
} | ||
|
||
public long getLastUpdate() { | ||
return lastUpdate; | ||
} | ||
|
||
public void setLastUpdate(long lastUpdate) { | ||
this.lastUpdate = lastUpdate; | ||
} | ||
|
||
} | ||
|
||
public static class PasswordUpdate { | ||
|
||
private String currentPassword; | ||
private String newPassword; | ||
|
||
public String getCurrentPassword() { | ||
return currentPassword; | ||
} | ||
|
||
public void setCurrentPassword(String currentPassword) { | ||
this.currentPassword = currentPassword; | ||
} | ||
|
||
public String getNewPassword() { | ||
return newPassword; | ||
} | ||
|
||
public void setNewPassword(String newPassword) { | ||
this.newPassword = newPassword; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
services/src/main/java/org/keycloak/services/resources/account/PasswordUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.keycloak.services.resources.account; | ||
|
||
import org.keycloak.credential.CredentialModel; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.UserModel; | ||
|
||
public class PasswordUtil { | ||
|
||
private KeycloakSession session; | ||
private UserModel user; | ||
|
||
public PasswordUtil(KeycloakSession session, UserModel user) { | ||
this.session = session; | ||
this.user = user; | ||
} | ||
|
||
public boolean isConfigured(KeycloakSession session, RealmModel realm, UserModel user) { | ||
return session.userCredentialManager().isConfiguredFor(realm, user, CredentialModel.PASSWORD); | ||
} | ||
|
||
public void update() { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters