-
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.
Implementation UserProfileHandler (issue #8)
- Loading branch information
Showing
4 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
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
71 changes: 71 additions & 0 deletions
71
imixs-oidc/src/main/java/org/imixs/oidc/UserProfileHandler.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,71 @@ | ||
package org.imixs.oidc; | ||
|
||
import java.io.Serializable; | ||
import java.nio.file.AccessDeniedException; | ||
import java.security.Principal; | ||
import java.util.logging.Logger; | ||
|
||
import org.imixs.marty.profile.ProfileEvent; | ||
import org.imixs.workflow.ItemCollection; | ||
|
||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Inject; | ||
import jakarta.security.enterprise.identitystore.openid.AccessToken; | ||
import jakarta.security.enterprise.identitystore.openid.OpenIdContext; | ||
|
||
/** | ||
* This class UserProfileHandler is a CDI Observer bean listening to updates of | ||
* a Imixs-Marty profile during the login process. | ||
* It automatically updates the user profile with the attributes provided by the | ||
* OpenID provider. | ||
* | ||
*/ | ||
@RequestScoped | ||
public class UserProfileHandler implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
private static Logger logger = Logger.getLogger(OidcAuthenticationHandler.class.getName()); | ||
|
||
@Inject | ||
Principal principal; | ||
|
||
@Inject | ||
private OpenIdContext context; | ||
|
||
/** | ||
* ProfileEvent listener to update a new profile with the user attributes | ||
* provided by the OpenID provider. | ||
* | ||
* @param workflowEvent | ||
* @throws AccessDeniedException | ||
*/ | ||
public void onProfileEvent(@Observes ProfileEvent profileEvent) throws AccessDeniedException { | ||
|
||
int eventType = profileEvent.getEventType(); | ||
|
||
ItemCollection profile = profileEvent.getProfile(); | ||
if (profile == null) { | ||
return; | ||
} | ||
|
||
// reset orderItems if workItem has changed | ||
if (context != null | ||
&& (ProfileEvent.ON_PROFILE_CREATE == eventType || ProfileEvent.ON_PROFILE_LOGIN == eventType)) { | ||
|
||
AccessToken accessToken = context.getAccessToken(); | ||
String userName = "" + accessToken.getClaim("name"); | ||
String email = "" + accessToken.getClaim("email"); | ||
|
||
if (!email.equals(profile.getItemValueString("txtemail")) | ||
|| !userName.equals(profile.getItemValueString("txtusername"))) { | ||
logger.info("...update profile data..."); | ||
profile.setItemValue("txtemail", email); | ||
profile.setItemValue("txtusername", userName); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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