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

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

Introduction

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

Prototype

public SimpleAuthenticationInfo() 

Source Link

Document

Default no-argument constructor.

Usage

From source file:b4f.seguridad.SecurityAuthenticator.java

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

    if (DEBUG) {//from www. j a  v  a2s .  c  o  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;//from w  w w  . j a  va 2s  .  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  w ww . j  ava  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 ww. ja  v a  2  s  .com*/
    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  w  w w .  ja va  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:com.miki.webapp.shiro.EntityRealm.java

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

    final UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

    SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();

    user = userDAO.getOneBy("login", token.getUsername());

    if (user != null) {

        //Control de l'activation du compte
        if (!user.isActif()) {
            throw new LockedAccountException(
                    "Dsol votre compte est inactif, veuillez contacter l'administrateur Svp");
        }/*w w  w .  ja  va  2 s. co m*/

        //Connexion
        simpleAuthenticationInfo = new SimpleAuthenticationInfo(user.getLogin(), user.getMotDePasse(),
                getName());
        return simpleAuthenticationInfo;
    } else {
        throw new UnknownAccountException(
                "L'utilisateur ne se trouve pas dans le systme, veuillez ressayer Svp !");
    }
}

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

License:Open Source License

/**
 * Perform authentication based on the supplied token.
 *//*from  ww w .  j  av  a 2s. co  m*/
@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/*from   w w w.j  a va2 s  .  com*/
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.");
    }/* w w  w .ja  v  a2  s .co m*/

    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 va  2s  . c  o 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;
}