Search in sources :

Example 1 with RememberMeToken

use of com.haulmont.cuba.security.entity.RememberMeToken in project cuba by cuba-platform.

the class RememberMeAuthenticationProvider method loadRememberMeToken.

@Nullable
protected RememberMeToken loadRememberMeToken(User user, String rememberMeToken) {
    EntityManager em = persistence.getEntityManager();
    TypedQuery<RememberMeToken> query = em.createQuery("select rt from sec$RememberMeToken rt where rt.token = :token and rt.user.id = :userId", RememberMeToken.class);
    query.setParameter("token", rememberMeToken);
    query.setParameter("userId", user.getId());
    return query.getFirstResult();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) RememberMeToken(com.haulmont.cuba.security.entity.RememberMeToken) Nullable(javax.annotation.Nullable)

Example 2 with RememberMeToken

use of com.haulmont.cuba.security.entity.RememberMeToken in project cuba by cuba-platform.

the class RememberMeAuthenticationProvider method authenticate.

@Override
public AuthenticationDetails authenticate(Credentials credentials) throws LoginException {
    RememberMeCredentials rememberMe = (RememberMeCredentials) credentials;
    String login = rememberMe.getLogin();
    Locale credentialsLocale = rememberMe.getLocale() == null ? messages.getTools().getDefaultLocale() : rememberMe.getLocale();
    if (Strings.isNullOrEmpty(login)) {
        // empty login is not valid
        throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale));
    }
    checkUserCredentials(credentials);
    User user = loadUser(login);
    if (user == null) {
        throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale));
    }
    RememberMeToken loginToken = loadRememberMeToken(user, rememberMe.getRememberMeToken());
    if (loginToken == null) {
        throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale));
    }
    Locale userLocale = getUserLocale(rememberMe, user);
    UserSession session = createSession(rememberMe, user, userLocale);
    setClientSessionParams(rememberMe, session);
    AuthenticationDetails authenticationDetails = new SimpleAuthenticationDetails(session);
    checkUserAccess(rememberMe, authenticationDetails);
    return authenticationDetails;
}
Also used : Locale(java.util.Locale) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession) LoginException(com.haulmont.cuba.security.global.LoginException) RememberMeToken(com.haulmont.cuba.security.entity.RememberMeToken)

Aggregations

RememberMeToken (com.haulmont.cuba.security.entity.RememberMeToken)2 EntityManager (com.haulmont.cuba.core.EntityManager)1 User (com.haulmont.cuba.security.entity.User)1 LoginException (com.haulmont.cuba.security.global.LoginException)1 UserSession (com.haulmont.cuba.security.global.UserSession)1 Locale (java.util.Locale)1 Nullable (javax.annotation.Nullable)1