Search in sources :

Example 96 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class DuoAuthenticationHandler method authenticateDuoApiCredential.

private AuthenticationHandlerExecutionResult authenticateDuoApiCredential(final Credential credential) throws FailedLoginException {
    try {
        final DuoSecurityAuthenticationService duoAuthenticationService = getDuoAuthenticationService();
        final DuoDirectCredential c = DuoDirectCredential.class.cast(credential);
        if (duoAuthenticationService.authenticate(c).getKey()) {
            final Principal principal = c.getAuthentication().getPrincipal();
            LOGGER.debug("Duo has successfully authenticated [{}]", principal.getId());
            return createHandlerResult(credential, principal, new ArrayList<>());
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    throw new FailedLoginException("Duo authentication has failed");
}
Also used : FailedLoginException(javax.security.auth.login.FailedLoginException) Principal(org.apereo.cas.authentication.principal.Principal) GeneralSecurityException(java.security.GeneralSecurityException) FailedLoginException(javax.security.auth.login.FailedLoginException)

Example 97 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class OAuth20UserAuthenticator method validate.

@Override
public void validate(final UsernamePasswordCredentials credentials, final WebContext context) throws CredentialsException {
    final UsernamePasswordCredential casCredential = new UsernamePasswordCredential(credentials.getUsername(), credentials.getPassword());
    try {
        final String clientId = context.getRequestParameter(OAuth20Constants.CLIENT_ID);
        final Service service = this.webApplicationServiceFactory.createService(clientId);
        final RegisteredService registeredService = OAuth20Utils.getRegisteredOAuthServiceByClientId(this.servicesManager, clientId);
        RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(registeredService);
        final AuthenticationResult authenticationResult = this.authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(null, casCredential);
        final Authentication authentication = authenticationResult.getAuthentication();
        final Principal principal = authentication.getPrincipal();
        final OAuthUserProfile profile = new OAuthUserProfile();
        final String id = registeredService.getUsernameAttributeProvider().resolveUsername(principal, service, registeredService);
        LOGGER.debug("Created profile id [{}]", id);
        profile.setId(id);
        final Map<String, Object> attributes = registeredService.getAttributeReleasePolicy().getAttributes(principal, service, registeredService);
        profile.addAttributes(attributes);
        LOGGER.debug("Authenticated user profile [{}]", profile);
        credentials.setUserProfile(profile);
    } catch (final Exception e) {
        throw new CredentialsException("Cannot login user using CAS internal authentication", e);
    }
}
Also used : RegisteredService(org.apereo.cas.services.RegisteredService) Authentication(org.apereo.cas.authentication.Authentication) RegisteredService(org.apereo.cas.services.RegisteredService) Service(org.apereo.cas.authentication.principal.Service) CredentialsException(org.pac4j.core.exception.CredentialsException) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) OAuthUserProfile(org.apereo.cas.support.oauth.profile.OAuthUserProfile) Principal(org.apereo.cas.authentication.principal.Principal) CredentialsException(org.pac4j.core.exception.CredentialsException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult)

Example 98 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class DefaultOAuth2UserProfileDataCreator method createFrom.

@Override
@Audit(action = "OAUTH2_USER_PROFILE_DATA", actionResolverName = "OAUTH2_USER_PROFILE_DATA_ACTION_RESOLVER", resourceResolverName = "OAUTH2_USER_PROFILE_DATA_RESOURCE_RESOLVER")
public Map<String, Object> createFrom(final AccessToken accessToken, final J2EContext context) {
    final Principal principal = getAccessTokenAuthenticationPrincipal(accessToken, context);
    final Map<String, Object> map = new HashMap<>();
    map.put(OAuth20UserProfileViewRenderer.MODEL_ATTRIBUTE_ID, principal.getId());
    map.put(OAuth20UserProfileViewRenderer.MODEL_ATTRIBUTE_ATTRIBUTES, principal.getAttributes());
    finalizeProfileResponse(accessToken, map, principal);
    return map;
}
Also used : HashMap(java.util.HashMap) Principal(org.apereo.cas.authentication.principal.Principal) Audit(org.apereo.inspektr.audit.annotation.Audit)

Example 99 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class AbstractOAuth20Tests method internalVerifyRefreshTokenOk.

protected Pair<AccessToken, RefreshToken> internalVerifyRefreshTokenOk(final OAuthRegisteredService service, final boolean json) throws Exception {
    final Principal principal = createPrincipal();
    final RefreshToken refreshToken = addRefreshToken(principal, service);
    return internalVerifyRefreshTokenOk(service, json, refreshToken, principal);
}
Also used : RefreshToken(org.apereo.cas.ticket.refreshtoken.RefreshToken) Principal(org.apereo.cas.authentication.principal.Principal)

Example 100 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class OAuth20AccessTokenControllerMemcachedTests method verifyOAuthCodeIsAddedToMemcached.

@Test
public void verifyOAuthCodeIsAddedToMemcached() {
    final Principal p = createPrincipal();
    final OAuthCode code = addCode(p, addRegisteredService());
    final Ticket ticket = this.ticketRegistry.getTicket(code.getId(), OAuthCode.class);
    assertNotNull(ticket);
}
Also used : Ticket(org.apereo.cas.ticket.Ticket) OAuthCode(org.apereo.cas.ticket.code.OAuthCode) Principal(org.apereo.cas.authentication.principal.Principal) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Principal (org.apereo.cas.authentication.principal.Principal)114 HashMap (java.util.HashMap)33 RegisteredService (org.apereo.cas.services.RegisteredService)31 Test (org.junit.Test)29 Authentication (org.apereo.cas.authentication.Authentication)26 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)26 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)26 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)25 Map (java.util.Map)23 Slf4j (lombok.extern.slf4j.Slf4j)23 lombok.val (lombok.val)19 List (java.util.List)15 StringUtils (org.apache.commons.lang3.StringUtils)15 OAuthCode (org.apereo.cas.ticket.code.OAuthCode)15 CollectionUtils (org.apereo.cas.util.CollectionUtils)15 ArrayList (java.util.ArrayList)14 Optional (java.util.Optional)14 Service (org.apereo.cas.authentication.principal.Service)14 Collection (java.util.Collection)11 Collectors (java.util.stream.Collectors)10