Search in sources :

Example 16 with AttributePrincipal

use of org.jasig.cas.client.authentication.AttributePrincipal in project pac4j by pac4j.

the class AbstractCasRestClient method validateServiceTicket.

public CasProfile validateServiceTicket(final String serviceURL, final TokenCredentials ticket, final WebContext context) {
    try {
        final Assertion assertion = configuration.retrieveTicketValidator(context).validate(ticket.getToken(), serviceURL);
        final AttributePrincipal principal = assertion.getPrincipal();
        final CasProfile casProfile = new CasProfile();
        casProfile.setId(ProfileHelper.sanitizeIdentifier(casProfile, principal.getName()));
        casProfile.addAttributes(principal.getAttributes());
        return casProfile;
    } catch (final TicketValidationException e) {
        throw new TechnicalException(e);
    }
}
Also used : CasProfile(org.pac4j.cas.profile.CasProfile) TechnicalException(org.pac4j.core.exception.TechnicalException) Assertion(org.jasig.cas.client.validation.Assertion) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal) TicketValidationException(org.jasig.cas.client.validation.TicketValidationException)

Example 17 with AttributePrincipal

use of org.jasig.cas.client.authentication.AttributePrincipal in project uhgroupings by uhawaii-system-its-ti-iam.

the class UserDetailsServiceTest method testAdminUsers.

// Rebase. Test admin users for code coverage purposes.
// Related to ticket-500, used hardcoded values that were deleted.
@Ignore
@Test
public void testAdminUsers() {
    Map<String, Object> map = new HashMap<>();
    map.put("uid", "duckart");
    map.put("uhUuid", "89999999");
    AttributePrincipal principal = new AttributePrincipalImpl("duckart", map);
    Assertion assertion = new AssertionImpl(principal);
    CasUserDetailsServiceImplj userDetailsService = new CasUserDetailsServiceImplj(userBuilder);
    User user = (User) userDetailsService.loadUserDetails(assertion);
    // Basics.
    assertThat(user.getUsername(), is("duckart"));
    assertThat(user.getUid(), is("duckart"));
    assertThat(user.getUhUuid(), is("89999999"));
    // Granted Authorities.
    assertTrue(user.getAuthorities().size() > 0);
    assertTrue(user.hasRole(Role.ANONYMOUS));
    assertTrue(user.hasRole(Role.UH));
    assertTrue(user.hasRole(Role.EMPLOYEE));
    assertTrue(user.hasRole(Role.ADMIN));
    // Check a made-up junky role name.
    map = new HashMap<>();
    map.put("uid", "someuser");
    map.put("uhUuid", "10000001");
    principal = new AttributePrincipalImpl("someuser", map);
    assertion = new AssertionImpl(principal);
    user = (User) userDetailsService.loadUserDetails(assertion);
    assertThat(user.getUsername(), is("someuser"));
    assertThat(user.getUid(), is("someuser"));
    assertThat(user.getUhUuid(), is("10000001"));
    assertTrue(user.getAuthorities().size() > 0);
    assertTrue(user.hasRole(Role.ANONYMOUS));
    assertTrue(user.hasRole(Role.UH));
    assertTrue(user.hasRole(Role.EMPLOYEE));
    assertTrue(user.hasRole(Role.ADMIN));
}
Also used : AssertionImpl(org.jasig.cas.client.validation.AssertionImpl) HashMap(java.util.HashMap) Assertion(org.jasig.cas.client.validation.Assertion) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal) AttributePrincipalImpl(org.jasig.cas.client.authentication.AttributePrincipalImpl) Ignore(org.junit.Ignore) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with AttributePrincipal

use of org.jasig.cas.client.authentication.AttributePrincipal in project mycore by MyCoRe-Org.

the class MCRCASServlet method doGetPost.

public void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest req = job.getRequest();
    HttpServletResponse res = job.getResponse();
    String ticket = req.getParameter("ticket");
    if ((ticket == null) || (ticket.trim().length() == 0)) {
        res.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    // Validate ticket at CAS server
    Cas20ProxyTicketValidator sv = new Cas20ProxyTicketValidator(serverURL);
    sv.setAcceptAnyProxy(true);
    Assertion a = sv.validate(ticket, clientURL);
    AttributePrincipal principal = a.getPrincipal();
    // Get user name logged in
    String userName = principal.getName();
    LOGGER.info("Login {}", userName);
    MCRUser user;
    boolean userExists = MCRUserManager.exists(userName, realmID);
    if (userExists)
        user = MCRUserManager.getUser(userName, realmID);
    else
        user = new MCRUser(userName, realmID);
    // Get user properties from LDAP server
    boolean userChanged = MCRLDAPClient.instance().updateUserProperties(user);
    if (userChanged && userExists) {
        MCRUserManager.updateUser(user);
    }
    // Store login user in session and redirect browser to target url
    MCRSessionMgr.getCurrentSession().setUserInformation(user);
    // MCR-1154
    req.changeSessionId();
    MCRLoginServlet.redirect(res);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRUser(org.mycore.user2.MCRUser) Assertion(org.jasig.cas.client.validation.Assertion) HttpServletResponse(javax.servlet.http.HttpServletResponse) Cas20ProxyTicketValidator(org.jasig.cas.client.validation.Cas20ProxyTicketValidator) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal)

Example 19 with AttributePrincipal

use of org.jasig.cas.client.authentication.AttributePrincipal in project shiro by apache.

the class CasRealm method doGetAuthenticationInfo.

/**
 * Authenticates a user and retrieves its information.
 *
 * @param token the authentication token
 * @throws AuthenticationException if there is an error during authentication.
 */
@Override
@SuppressWarnings("unchecked")
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    CasToken casToken = (CasToken) token;
    if (token == null) {
        return null;
    }
    String ticket = (String) casToken.getCredentials();
    if (!StringUtils.hasText(ticket)) {
        return null;
    }
    TicketValidator ticketValidator = ensureTicketValidator();
    try {
        // contact CAS server to validate service ticket
        Assertion casAssertion = ticketValidator.validate(ticket, getCasService());
        // get principal, user id and attributes
        AttributePrincipal casPrincipal = casAssertion.getPrincipal();
        String userId = casPrincipal.getName();
        log.debug("Validate ticket : {} in CAS server : {} to retrieve user : {}", new Object[] { ticket, getCasServerUrlPrefix(), userId });
        Map<String, Object> attributes = casPrincipal.getAttributes();
        // refresh authentication token (user id + remember me)
        casToken.setUserId(userId);
        String rememberMeAttributeName = getRememberMeAttributeName();
        String rememberMeStringValue = (String) attributes.get(rememberMeAttributeName);
        boolean isRemembered = rememberMeStringValue != null && Boolean.parseBoolean(rememberMeStringValue);
        if (isRemembered) {
            casToken.setRememberMe(true);
        }
        // create simple authentication info
        List<Object> principals = CollectionUtils.asList(userId, attributes);
        PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());
        return new SimpleAuthenticationInfo(principalCollection, ticket);
    } catch (TicketValidationException e) {
        throw new CasAuthenticationException("Unable to validate ticket [" + ticket + "]", e);
    }
}
Also used : SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal)

Aggregations

AttributePrincipal (org.jasig.cas.client.authentication.AttributePrincipal)19 Assertion (org.jasig.cas.client.validation.Assertion)9 HashMap (java.util.HashMap)4 AttributePrincipalImpl (org.jasig.cas.client.authentication.AttributePrincipalImpl)4 AssertionImpl (org.jasig.cas.client.validation.AssertionImpl)4 Test (org.junit.Test)4 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 TicketValidationException (org.jasig.cas.client.validation.TicketValidationException)3 Principal (java.security.Principal)2 HttpSession (javax.servlet.http.HttpSession)2 Ignore (org.junit.Ignore)2 TechnicalException (org.pac4j.core.exception.TechnicalException)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ParseException (com.vividsolutions.jts.io.ParseException)1 SecurityServiceException (ddf.security.service.SecurityServiceException)1 STSClientConfiguration (ddf.security.sts.client.configuration.STSClientConfiguration)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Iterator (java.util.Iterator)1