Example usage for org.apache.shiro.authc SimpleAuthenticationInfo getPrincipals

List of usage examples for org.apache.shiro.authc SimpleAuthenticationInfo getPrincipals

Introduction

In this page you can find the example usage for org.apache.shiro.authc SimpleAuthenticationInfo getPrincipals.

Prototype

public PrincipalCollection getPrincipals() 

Source Link

Usage

From source file:com.axelor.auth.cas.AuthCasRealm.java

License:Open Source License

@Override
@Transactional/*from  w w w  .ja va  2 s  .c o  m*/
@SuppressWarnings("all")
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    final SimpleAuthenticationInfo info = (SimpleAuthenticationInfo) super.doGetAuthenticationInfo(token);
    final List<?> principals = info.getPrincipals().asList();

    if (principals.isEmpty()) {
        return null;
    }

    final Map<String, String> attrs = new HashMap<>();
    try {
        attrs.putAll((Map) principals.get(1));
    } catch (Exception e) {
    }

    AppSettings settings = AppSettings.get();
    AuthService service = AuthService.getInstance();
    Inflector inflector = Inflector.getInstance();

    String code = (String) principals.get(0);
    User user = AuthUtils.getUser(code);

    // generate user object
    if (user == null) {

        String name = attrs.get(settings.get(CONFIG_CAS_ATTRS_USER_NAME, "name"));
        String email = attrs.get(settings.get(CONFIG_CAS_ATTRS_USER_EMAIL, "mail"));

        if (StringUtils.isBlank(name)) {
            name = inflector.titleize(code.replace(".", " "));
        }

        user = new User(code, name);
        user.setEmail(email);
        user.setPassword(UUID.randomUUID().toString());
        user = JPA.save(user);
        service.encrypt(user);
    }

    if (!AuthUtils.isActive(user)) {
        return null;
    }

    return info;
}

From source file:com.github.richardwilly98.esdms.services.AuthenticationProvider.java

License:Open Source License

private PrincipalCollection getPrincipals(String token) throws ServiceException {
    User user = null;/*from   ww  w .j ava 2  s  .co  m*/
    Session session = get(token);
    if (session != null) {
        String login = session.getUserId();
        user = userService.get(login);
    }
    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, "", "");
    return info.getPrincipals();
}

From source file:com.github.richardwilly98.esdms.shiro.EsAuthenticationFilter.java

License:Open Source License

private PrincipalCollection getPrincipals(String token) throws ServiceException {
    if (log.isTraceEnabled()) {
        log.trace(String.format("Start getPrincipals - %s", token));
    }//from  w ww  . j  a v  a  2 s.  c o m
    User user = null;
    Session session = authenticationService.get(token);
    if (session != null) {
        String login = session.getUserId();
        user = userService.get(login);
        log.trace(String.format("getPrincipals - Found user %s from token %s", login, token));
    } else {
        log.info(String.format("getPrincipals - Cannot find user with token %s", token));
        return null;
    }
    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, "", "");
    return info.getPrincipals();
}

From source file:io.github.howiefh.jeews.modules.sys.security.credentials.JsonWebTokenCredentialsMatcher.java

License:Apache License

@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
    JsonWebToken jsonWebToken = (JsonWebToken) token;
    JWTVerifier verifier = new JWTVerifier(secret, audience);
    try {//from w ww .ja  va  2s  .  co m
        Map<String, Object> map = verifier.verify(jsonWebToken.getToken());
        SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo) info;
        String realmName = authenticationInfo.getPrincipals().getRealmNames().iterator().next();
        SimplePrincipalCollection principals = new SimplePrincipalCollection();
        principals.add(map.get("iss"), realmName);
        authenticationInfo.setPrincipals(principals);
        return true;
    } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | SignatureException
            | IOException | JWTVerifyException e) {
        log.debug(e.getMessage());
        return false;
    }
}

From source file:org.eclipse.kapua.service.authentication.shiro.credential.BCryptCredentialsMatcher.java

License:Open Source License

@Override
public boolean doCredentialsMatch(AuthenticationToken authenticationToken,
        AuthenticationInfo authenticationInfo) {
    ////from w w  w  .ja  v  a 2s  .  co m
    // Token data
    UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
    String tokenUsername = token.getUsername();
    String tokenPassword = new String(token.getPassword());

    //
    // Info data
    SimpleAuthenticationInfo info = (SimpleAuthenticationInfo) authenticationInfo;
    String infoUsername = (String) info.getPrincipals().getPrimaryPrincipal();
    String infoPassword = (String) info.getCredentials();

    //
    // Match token with info
    boolean credentialMatch = false;
    if (tokenUsername.equals(infoUsername)) {
        if (BCrypt.checkpw(tokenPassword, infoPassword)) {
            credentialMatch = true;

            // FIXME: if true cache token password for authentication performance improvement
        }
    }

    return credentialMatch;
}

From source file:org.solrsystem.ingest.shiro.JndiLdapRealmWithUser.java

License:Apache License

@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token, Object ldapPrincipal,
        Object ldapCredentials, LdapContext ldapContext) throws NamingException {
    SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo) super.createAuthenticationInfo(
            token, ldapPrincipal, ldapCredentials, ldapContext);
    MutablePrincipalCollection mpc = (MutablePrincipalCollection) authenticationInfo.getPrincipals();
    final SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

    // get all attributes
    constraints.setReturningAttributes(null);
    String templ = getUserDnTemplate();
    String userDn = MessageFormat.format(templ, mpc.getPrimaryPrincipal());
    final NamingEnumeration<SearchResult> answer = ldapContext.search(userDn, "(objectClass=*)", constraints);

    if (answer.hasMore()) {
        Attributes attrs = answer.next().getAttributes();
        if (answer.hasMore()) {
            throw new NamingException("Non-unique user specified by:" + userDn);
        }//from w w  w  .  ja  va2  s. co  m
        //TODO: make this Guicy
        User user = new UserFromLdap(attrs, mpc);

        // at present there should only be one realm involved.
        Iterator<String> realmIter = mpc.getRealmNames().iterator();
        String firstRealm = realmIter.next();
        if (realmIter.hasNext()) {
            // ugh, need a new solution here
            String explanation = String.format("More than one realm found! (%s and %s)", firstRealm,
                    realmIter.next());
            throw new NamingException(explanation);
        }
        mpc.add(user, firstRealm);
    } else {
        throw new NamingException("Invalid User specified by:" + userDn);
    }

    return authenticationInfo;
}