use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class AbstractPac4jAuthenticationHandler method createResult.
/**
* Build the handler result.
*
* @param credentials the provided credentials
* @param profile the retrieved user profile
* @return the built handler result
* @throws GeneralSecurityException On authentication failure.
*/
protected AuthenticationHandlerExecutionResult createResult(final ClientCredential credentials, final UserProfile profile) throws GeneralSecurityException {
if (profile == null) {
throw new FailedLoginException("Authentication did not produce a user profile for: " + credentials);
}
final String id;
if (isTypedIdUsed) {
id = profile.getTypedId();
LOGGER.debug("Delegated authentication indicates usage of typed profile id [{}]", id);
} else {
id = profile.getId();
}
if (StringUtils.isBlank(id)) {
throw new FailedLoginException("No identifier found for this user profile: " + profile);
}
credentials.setUserProfile(profile);
credentials.setTypedIdUsed(isTypedIdUsed);
final Principal principal = this.principalFactory.createPrincipal(id, new LinkedHashMap<>(profile.getAttributes()));
LOGGER.debug("Constructed authenticated principal [{}] based on user profile [{}]", principal, profile);
return createHandlerResult(credentials, principal, new ArrayList<>(0));
}
use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class ClientAuthenticationHandlerTests method verifyOkWithSimpleIdentifier.
@Test
public void verifyOkWithSimpleIdentifier() throws GeneralSecurityException, PreventedException {
this.handler.setTypedIdUsed(false);
final FacebookProfile facebookProfile = new FacebookProfile();
facebookProfile.setId(ID);
this.fbClient.setProfileCreator((oAuth20Credentials, webContext) -> facebookProfile);
final AuthenticationHandlerExecutionResult result = this.handler.authenticate(this.clientCredential);
final Principal principal = result.getPrincipal();
assertEquals(ID, principal.getId());
}
use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class ClientAuthenticationHandlerTests method verifyOk.
@Test
public void verifyOk() throws GeneralSecurityException, PreventedException {
final FacebookProfile facebookProfile = new FacebookProfile();
facebookProfile.setId(ID);
this.fbClient.setProfileCreator((oAuth20Credentials, webContext) -> facebookProfile);
final AuthenticationHandlerExecutionResult result = this.handler.authenticate(this.clientCredential);
final Principal principal = result.getPrincipal();
assertEquals(FacebookProfile.class.getName() + '#' + ID, principal.getId());
}
use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class ChainingPrincipalResolver method resolve.
@Override
public Principal resolve(final Credential credential, final Optional<Principal> principal, final Optional<AuthenticationHandler> handler) {
val principals = new ArrayList<Principal>(chain.size());
chain.stream().filter(resolver -> resolver.supports(credential)).forEach(resolver -> {
LOGGER.debug("Invoking principal resolver [{}]", resolver.getName());
val p = resolver.resolve(credential, principal, handler);
if (p != null) {
LOGGER.debug("Resolved principal [{}]", p);
principals.add(p);
}
});
if (principals.isEmpty()) {
LOGGER.warn("None of the principal resolvers in the chain were able to produce a principal");
return NullPrincipal.getInstance();
}
val attributes = new HashMap<String, List<Object>>();
val merger = CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger());
principals.forEach(p -> {
if (p != null) {
LOGGER.debug("Resolved principal [{}]", p);
val principalAttributes = p.getAttributes();
if (principalAttributes != null && !principalAttributes.isEmpty()) {
LOGGER.debug("Adding attributes [{}] for the final principal", principalAttributes);
attributes.putAll(CoreAuthenticationUtils.mergeAttributes(attributes, principalAttributes, merger));
}
}
});
return principalElectionStrategy.nominate(principals, attributes);
}
use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class CasSimpleMultifactorSendTokenAction method getOrCreateToken.
/**
* Get or create a token.
*
* @param requestContext the request context
* @param principal the principal
* @return the token
*/
protected CasSimpleMultifactorAuthenticationTicket getOrCreateToken(final RequestContext requestContext, final Principal principal) {
val currentToken = WebUtils.getSimpleMultifactorAuthenticationToken(requestContext, CasSimpleMultifactorAuthenticationTicket.class);
return Optional.ofNullable(currentToken).filter(token -> !token.isExpired()).orElseGet(() -> {
WebUtils.removeSimpleMultifactorAuthenticationToken(requestContext);
val service = WebUtils.getService(requestContext);
val mfaFactory = (CasSimpleMultifactorAuthenticationTicketFactory) ticketFactory.get(CasSimpleMultifactorAuthenticationTicket.class);
val token = mfaFactory.create(service, CollectionUtils.wrap(CasSimpleMultifactorAuthenticationConstants.PROPERTY_PRINCIPAL, principal));
LOGGER.debug("Created multifactor authentication token [{}] for service [{}]", token.getId(), service);
return token;
});
}
Aggregations