Skip to content

Get the authenticated user profiles

LELEU Jérôme edited this page Nov 14, 2018 · 3 revisions

Like for any Undertow web application, you can get the authenticated user via the exchange.getSecurityContext().getAuthenticatedAccount(). If the user is authenticated, the appropriate account will be stored in the context as a Pac4jAccount, on which you can get the main profile (getProfile method) or all profiles (getProfiles method) of the authenticated user:

SecurityContext securityContext = exchange.getSecurityContext();
if (securityContext != null) {
  Account account = securityContext.getAuthenticatedAccount();
  if (account instanceof Pac4jAccount) {
    List<CommonProfile> = ((Pac4jAccount) account).getProfiles();
  }
}

In fact, the Undertow SecurityContext is populated by the SecurityHandler, so you may need to use the AnonymousClient or directly the regular pac4j ProfileManager for anonymous urls:

UndertowWebContext context = new UndertowWebContext(exchange);
ProfileManager manager = new ProfileManager(context);
Optional<CommonProfile> profile = manager.get(true);

The retrieved profile is at least a CommonProfile, from which you can retrieve the most common attributes that all profiles share. But you can also cast the user profile to the appropriate profile according to the provider used for authentication. For example, after a Facebook authentication:

FacebookProfile facebookProfile = (FacebookProfile) commonProfile;
Clone this wiki locally