use of org.apache.shiro.authc.SimpleAuthenticationInfo in project shiro by apache.
the class ShiroModuleTest method testConfigure.
@Test
public void testConfigure() {
final MockRealm mockRealm = createMock(MockRealm.class);
AuthenticationToken authToken = createMock(AuthenticationToken.class);
AuthenticationInfo info = new SimpleAuthenticationInfo("mockUser", "password", "mockRealm");
expect(mockRealm.supports(authToken)).andReturn(true);
expect(mockRealm.getAuthenticationInfo(authToken)).andReturn(info);
replay(mockRealm);
Injector injector = Guice.createInjector(new ShiroModule() {
@Override
protected void configureShiro() {
bindRealm().to(MockRealm.class);
}
@Provides
public MockRealm createRealm() {
return mockRealm;
}
});
SecurityManager securityManager = injector.getInstance(SecurityManager.class);
assertNotNull(securityManager);
SecurityUtils.setSecurityManager(securityManager);
final Subject subject = new Subject.Builder(securityManager).buildSubject();
securityManager.login(subject, authToken);
verify(mockRealm);
}
use of org.apache.shiro.authc.SimpleAuthenticationInfo 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);
}
}
use of org.apache.shiro.authc.SimpleAuthenticationInfo in project zeppelin by apache.
the class PamRealm method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken userToken = (UsernamePasswordToken) token;
UnixUser user;
try {
user = (new PAM(this.getService())).authenticate(userToken.getUsername(), new String(userToken.getPassword()));
} catch (PAMException e) {
throw new AuthenticationException("Authentication failed for PAM.", e);
}
return new SimpleAuthenticationInfo(new UserPrincipal(user), userToken.getCredentials(), getName());
}
use of org.apache.shiro.authc.SimpleAuthenticationInfo in project SSM by Intel-bigdata.
the class PamRealm method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken userToken = (UsernamePasswordToken) token;
UnixUser user;
try {
user = (new PAM(this.getService())).authenticate(userToken.getUsername(), new String(userToken.getPassword()));
} catch (PAMException e) {
throw new AuthenticationException("Authentication failed for PAM.", e);
}
return new SimpleAuthenticationInfo(new UserPrincipal(user), userToken.getCredentials(), getName());
}
use of org.apache.shiro.authc.SimpleAuthenticationInfo in project SSM by Intel-bigdata.
the class ZeppelinHubRealm method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authToken;
if (StringUtils.isBlank(token.getUsername())) {
throw new AccountException("Empty usernames are not allowed by this realm.");
}
String loginPayload = createLoginPayload(token.getUsername(), token.getPassword());
User user = authenticateUser(loginPayload);
LOG.debug("{} successfully login via ZeppelinHub", user.login);
return new SimpleAuthenticationInfo(user.login, token.getPassword(), name);
}
Aggregations