Search in sources :

Example 31 with SimpleAuthenticationInfo

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);
}
Also used : AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) AuthenticationInfo(org.apache.shiro.authc.AuthenticationInfo) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) Subject(org.apache.shiro.subject.Subject) Test(org.junit.Test)

Example 32 with SimpleAuthenticationInfo

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

Example 33 with SimpleAuthenticationInfo

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());
}
Also used : UnixUser(org.jvnet.libpam.UnixUser) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AuthenticationException(org.apache.shiro.authc.AuthenticationException) PAM(org.jvnet.libpam.PAM) PAMException(org.jvnet.libpam.PAMException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 34 with SimpleAuthenticationInfo

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());
}
Also used : UnixUser(org.jvnet.libpam.UnixUser) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AuthenticationException(org.apache.shiro.authc.AuthenticationException) PAM(org.jvnet.libpam.PAM) PAMException(org.jvnet.libpam.PAMException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 35 with SimpleAuthenticationInfo

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);
}
Also used : AccountException(org.apache.shiro.authc.AccountException) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Aggregations

SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)39 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)15 AuthenticationException (org.apache.shiro.authc.AuthenticationException)12 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)9 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)5 AccountException (org.apache.shiro.authc.AccountException)4 Hash (org.apache.shiro.crypto.hash.Hash)4 Test (org.junit.Test)4 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)3 LockedAccountException (org.apache.shiro.authc.LockedAccountException)3 UnknownAccountException (org.apache.shiro.authc.UnknownAccountException)3 HashRequest (org.apache.shiro.crypto.hash.HashRequest)3 PAM (org.jvnet.libpam.PAM)3 PAMException (org.jvnet.libpam.PAMException)3 UnixUser (org.jvnet.libpam.UnixUser)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)2 ByteSource (org.apache.shiro.util.ByteSource)2 UserDO (cn.dubidubi.model.base.UserDO)1 TbUser (cn.exrick.manager.pojo.TbUser)1 PmphUser (com.bc.pmpheep.back.po.PmphUser)1