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);
}
}
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));
}
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);
}
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);
}
}
Aggregations