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

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

Introduction

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

Prototype

public void setPrincipals(PrincipalCollection principals) 

Source Link

Document

Sets the identifying principal(s) represented by this instance.

Usage

From source file:b4f.seguridad.SecurityAuthenticator.java

@Override
public AuthenticationInfo authenticate(AuthenticationToken at) throws AuthenticationException {

    if (DEBUG) {/*  w ww.j a va 2  s  .co m*/
        System.out.println("[SECURITY AUTHENTICATOR] Autenticando: " + at);
    }

    //SE ACCEDI CON UN JWT TOKEN
    if (at instanceof JwtToken) {
        JwtToken authToken = (JwtToken) at;
        if (authToken.getToken() != null && !authToken.getToken().equals("")) {

            if (!authToken.validar()) {
                throw new AccountException("Token invalido.");
            }

            try {
                Usuario user = UsersManager.getUser(authToken.getUser());
                if (user == null)
                    throw new Exception("Token invalido");

                SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo();
                authenticationInfo.setPrincipals(new SimplePrincipalCollection(user, user.getUsuario()));
                return authenticationInfo;
            } catch (Exception ex) {
                Logger.getLogger(ShiroAuthorizingRealm.class.getName()).log(Level.SEVERE, null, ex);
                throw new AuthenticationException(ex.getMessage());
            }

        } else {
            throw new AccountException("Token invalido.");
        }
    }

    DefaultSecurityManager dsm = new DefaultSecurityManager(getRealm());
    AuthenticationInfo authenticationInfo = dsm.authenticate(at);
    if (DEBUG) {
        System.out.println("[SECURITY AUTHENTICATOR] " + authenticationInfo);
    }
    return authenticationInfo;

}

From source file:co.edu.uniandes.csw.miso4204.security.auth.SecurityAuthenticator.java

public AuthenticationInfo authenticate(AuthenticationToken at) throws AuthenticationException {
    JwtToken authToken = (JwtToken) at;//w  ww.j  av a  2  s .  c  o  m
    if (authToken.getToken() != null) {
        if (!authToken.getToken().equals("")) {
            //Descifrar token y establecer info de usuario
            UserDTO user = decodeUser(authToken.getToken());
            if (validarToken(user)) {
                SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo();
                authenticationInfo.setPrincipals(new SimplePrincipalCollection(user, user.getUsername()));
                return authenticationInfo;
            }
        }
    }
    throw new AccountException("Token invalido.");
}

From source file:co.edu.uniandes.csw.uniandes.api.JWT.filter.JwtAuthenticator.java

public AuthenticationInfo authenticate(AuthenticationToken at) throws AuthenticationException {
    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo();
    //      VerifyToken ver = new VerifyToken();
    //      UserDTO user = VerifyToken.getDataUser(at.toString());
    info.setPrincipals(new SimplePrincipalCollection("user", "user"));
    return info;/*from www  . j av  a 2s .co m*/
}

From source file:co.edu.uniandes.csw.uniandes.seguridad.JwtAuthenticator.java

public AuthenticationInfo authenticate(AuthenticationToken at) throws AuthenticationException {
    JwtToken authToken = (JwtToken) at;//from   w  w  w  . j av  a2 s.  c  om
    if (authToken.getToken() != null) {
        //Descifrar token y establecer info de usuario
        if (validarToken(authToken.getToken())) {
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo();
            info.setPrincipals(new SimplePrincipalCollection(new Usuario("usuario1", "1"), "usuario1"));
            return info;
        }
    }
    throw new AccountException("Token invalido.");
}

From source file:co.edu.uniandes.hospitalkennedy.security.otro.SecurityAuthenticator.java

public AuthenticationInfo authenticate(AuthenticationToken at) throws AuthenticationException {

    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");

    JwtToken authToken = (JwtToken) at;//from   ww  w .  j  av  a 2  s .c  om
    if (authToken.getToken() != null) {
        if (!authToken.getToken().equals("")) {
            //Descifrar token y establecer info de usuario
            UserDTO user = decodeUser(authToken.getToken());
            if (validarToken(user)) {
                SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo();
                authenticationInfo.setPrincipals(new SimplePrincipalCollection(user, user.getUsername()));

                PathInfo pathInfo = PathInfo.getInstance();

                if (!pathInfo.autenticar(user.getGrupo()))
                    throw new AccountException("Token invalido.");

                return authenticationInfo;
            }
        }
    }
    throw new AccountException("Token invalido.");
}

From source file:ddf.security.realm.sts.AbstractStsRealm.java

License:Open Source License

/**
 * Perform authentication based on the supplied token.
 *//*from  ww  w. ja v a2  s . c om*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
    String method = "doGetAuthenticationInfo(    AuthenticationToken token )";
    LOGGER.entry(method);

    Object credential;

    if (token instanceof SAMLAuthenticationToken) {
        credential = token.getCredentials();
    } else if (token instanceof BaseAuthenticationToken) {
        credential = ((BaseAuthenticationToken) token).getCredentialsAsXMLString();
    } else {
        credential = token.getCredentials().toString();
    }
    if (credential == null) {
        String msg = "Unable to authenticate credential.  A NULL credential was provided in the supplied authentication token. This may be due to an error with the SSO server that created the token.";
        LOGGER.error(msg);
        throw new AuthenticationException(msg);
    } else {
        //removed the credentials from the log message for now, I don't think we should be dumping user/pass into log
        LOGGER.debug("Received credentials.");
    }

    if (!settingsConfigured) {
        configureStsClient();
        settingsConfigured = true;
    } else {
        setClaimsOnStsClient(createClaimsElement());
    }

    SecurityToken securityToken;
    if (token instanceof SAMLAuthenticationToken && credential instanceof SecurityToken) {
        securityToken = renewSecurityToken((SecurityToken) credential);
    } else {
        securityToken = requestSecurityToken(credential);
    }

    LOGGER.debug("Creating token authentication information with SAML.");
    SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    SecurityAssertion assertion = new SecurityAssertionImpl(securityToken);
    principals.add(assertion.getPrincipal(), NAME);
    principals.add(assertion, NAME);
    simpleAuthenticationInfo.setPrincipals(principals);
    simpleAuthenticationInfo.setCredentials(credential);

    LOGGER.exit(method);
    return simpleAuthenticationInfo;
}

From source file:ddf.security.realm.sts.StsRealm.java

License:Open Source License

/** Perform authentication based on the supplied token. */
@Override//w  w w  .j  a va2 s  . c  o  m
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
    Object credential;

    // perform validation
    if (token instanceof SAMLAuthenticationToken) {
        try {
            samlAssertionValidator.validate((SAMLAuthenticationToken) token);
            credential = token.getCredentials();
        } catch (AuthenticationFailureException e) {
            String msg = "Unable to validate request's authentication.";
            LOGGER.info(msg);
            throw new AuthenticationException(msg, e);
        }
    } else if (token instanceof STSAuthenticationToken) {
        credential = ((STSAuthenticationToken) token).getCredentialsAsString();
    } else {
        credential = token.getCredentials().toString();
    }

    if (credential == null) {
        String msg = "Unable to authenticate credential.  A NULL credential was provided in the supplied authentication token. This may be due to an error with the SSO server that created the token.";
        LOGGER.info(msg);
        throw new AuthenticationException(msg);
    } else {
        // removed the credentials from the log message for now, I don't think we should be dumping
        // user/pass into log
        LOGGER.debug("Received credentials.");
    }

    SecurityToken securityToken;
    if (token instanceof SAMLAuthenticationToken) {

        securityToken = AccessController
                .doPrivileged((PrivilegedAction<SecurityToken>) () -> checkRenewSecurityToken(credential));
    } else {
        securityToken = AccessController
                .doPrivileged((PrivilegedAction<SecurityToken>) () -> requestSecurityToken(credential));
    }

    LOGGER.debug("Creating token authentication information with SAML.");
    SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
    SimplePrincipalCollection principals = createPrincipalFromToken(securityToken);
    simpleAuthenticationInfo.setPrincipals(principals);
    simpleAuthenticationInfo.setCredentials(credential);

    return simpleAuthenticationInfo;
}

From source file:eu.forgestore.ws.util.ShiroUTAuthorizingRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken at) throws AuthenticationException {

    logger.info("AuthenticationToken at=" + at.toString());

    UsernamePasswordToken token = (UsernamePasswordToken) at;
    logger.info("tokengetUsername at=" + token.getUsername());
    //logger.info("tokengetPassword at=" + String.valueOf(token.getPassword()));
    //logger.info("tokengetPrincipal at=" + token.getPrincipal());

    FStoreUser bu = fstoreRepositoryRef.getUserByUsername(token.getUsername());
    if (bu == null) {
        throw new AuthenticationException("Sorry! No login for you.");
    }/*from w w  w.j  a  v  a 2  s.com*/

    String originalPass = bu.passwordValue();
    String suppliedPass = EncryptionUtil.hash(String.valueOf(token.getPassword()));
    logger.info("originalPass =" + originalPass);
    logger.info("suppliedPass =" + suppliedPass);
    if (originalPass.equals(suppliedPass)) {
        logger.info("======= USER is AUTHENTICATED OK =======");
    } else {
        throw new AuthenticationException("Sorry! No login for you.");
    }

    // try {
    // currentUser.login(token);
    // } catch (AuthenticationException ex) {
    // logger.info(ex.getMessage(), ex);
    // throw new AuthenticationException("Sorry! No login for you.");
    // }
    // // Perform authorization check
    // if (!requiredRoles.isEmpty() && !currentUser.hasAllRoles(requiredRoles)) {
    // logger.info("Authorization failed for authenticated user");
    // throw new AuthenticationException("Sorry! No login for you.");
    // }

    SimpleAuthenticationInfo sa = new SimpleAuthenticationInfo();
    sa.setCredentials(token.getCredentials());
    SimplePrincipalCollection principals = new org.apache.shiro.subject.SimplePrincipalCollection();
    principals.add(token.getPrincipal(), "bakerrealm");

    sa.setPrincipals(principals);
    return sa;
}

From source file:gr.upatras.ece.nam.baker.util.ShiroUTAuthorizingRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken at) throws AuthenticationException {

    logger.info("AuthenticationToken at=" + at.toString());

    UsernamePasswordToken token = (UsernamePasswordToken) at;
    logger.info("tokengetUsername at=" + token.getUsername());
    //logger.info("tokengetPassword at=" + String.valueOf(token.getPassword()));
    //logger.info("tokengetPrincipal at=" + token.getPrincipal());

    BakerUser bu = bakerRepositoryRef.getUserByUsername(token.getUsername());
    if (bu == null) {
        throw new AuthenticationException("Sorry! No login for you.");
    }//from  ww w  . ja v  a2 s. co  m

    String originalPass = bu.getPassword();
    String suppliedPass = EncryptionUtil.hash(String.valueOf(token.getPassword()));
    logger.info("originalPass =" + originalPass);
    logger.info("suppliedPass =" + suppliedPass);
    if (originalPass.equals(suppliedPass)) {
        logger.info("======= USER is AUTHENTICATED OK =======");
    } else {
        throw new AuthenticationException("Sorry! No login for you.");
    }

    // try {
    // currentUser.login(token);
    // } catch (AuthenticationException ex) {
    // logger.info(ex.getMessage(), ex);
    // throw new AuthenticationException("Sorry! No login for you.");
    // }
    // // Perform authorization check
    // if (!requiredRoles.isEmpty() && !currentUser.hasAllRoles(requiredRoles)) {
    // logger.info("Authorization failed for authenticated user");
    // throw new AuthenticationException("Sorry! No login for you.");
    // }

    SimpleAuthenticationInfo sa = new SimpleAuthenticationInfo();
    sa.setCredentials(token.getCredentials());
    SimplePrincipalCollection principals = new org.apache.shiro.subject.SimplePrincipalCollection();
    principals.add(token.getPrincipal(), "bakerrealm");

    sa.setPrincipals(principals);
    return sa;
}

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 {/* w  w  w  .ja v  a2  s .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;
    }
}