Search in sources :

Example 1 with UsernamePasswordAuthToken

use of com.enonic.xp.security.auth.UsernamePasswordAuthToken in project xp by enonic.

the class SecurityServiceImplTest method testAuthenticateByUsernamePwd.

@Test
public void testAuthenticateByUsernamePwd() throws Exception {
    runAsAdmin(() -> {
        final CreateUserParams createUser = CreateUserParams.create().userKey(PrincipalKey.ofUser(SYSTEM, "User1")).displayName("User 1").email("user1@enonic.com").login("User1").password("runar").build();
        final User user = securityService.createUser(createUser);
        refresh();
        final UsernamePasswordAuthToken authToken = new UsernamePasswordAuthToken();
        authToken.setUsername("User1");
        authToken.setPassword("runar");
        authToken.setIdProvider(SYSTEM);
        final AuthenticationInfo authInfo = securityService.authenticate(authToken);
        assertTrue(authInfo.isAuthenticated());
        assertEquals(user.getKey(), authInfo.getUser().getKey());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) UsernamePasswordAuthToken(com.enonic.xp.security.auth.UsernamePasswordAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with UsernamePasswordAuthToken

use of com.enonic.xp.security.auth.UsernamePasswordAuthToken in project xp by enonic.

the class SecurityServiceImplTest method setPassword.

@Test
public void setPassword() throws Exception {
    runAsAdmin(() -> {
        final PrincipalKey userKey1 = PrincipalKey.ofUser(SYSTEM, "User1");
        final CreateUserParams createUser1 = CreateUserParams.create().userKey(userKey1).displayName("User 1").email("user1@enonic.com").login("User1").password("fisk").build();
        final User user = securityService.createUser(createUser1);
        refresh();
        final UsernamePasswordAuthToken authToken = new UsernamePasswordAuthToken();
        authToken.setUsername("user1");
        authToken.setPassword("runar");
        authToken.setIdProvider(SYSTEM);
        AuthenticationInfo authInfo = securityService.authenticate(authToken);
        assertFalse(authInfo.isAuthenticated());
        securityService.setPassword(user.getKey(), "runar");
        AuthenticationInfo authInfo2 = securityService.authenticate(authToken);
        assertTrue(authInfo2.isAuthenticated());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) UsernamePasswordAuthToken(com.enonic.xp.security.auth.UsernamePasswordAuthToken) PrincipalKey(com.enonic.xp.security.PrincipalKey) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with UsernamePasswordAuthToken

use of com.enonic.xp.security.auth.UsernamePasswordAuthToken in project xp by enonic.

the class AuthHelper method authenticate.

private AuthenticationInfo authenticate(final String user, final String password, final IdProviderKey idProviderKey, final boolean rememberMe) {
    AuthenticationInfo authInfo = null;
    if (isValidEmail(user)) {
        final EmailPasswordAuthToken emailAuthToken = new EmailPasswordAuthToken();
        emailAuthToken.setEmail(user);
        emailAuthToken.setPassword(password);
        emailAuthToken.setIdProvider(idProviderKey);
        emailAuthToken.setRememberMe(rememberMe);
        authInfo = securityService.authenticate(emailAuthToken);
    }
    if (authInfo == null || !authInfo.isAuthenticated()) {
        final UsernamePasswordAuthToken usernameAuthToken = new UsernamePasswordAuthToken();
        usernameAuthToken.setUsername(user);
        usernameAuthToken.setPassword(password);
        usernameAuthToken.setIdProvider(idProviderKey);
        usernameAuthToken.setRememberMe(rememberMe);
        authInfo = securityService.authenticate(usernameAuthToken);
    }
    return authInfo;
}
Also used : UsernamePasswordAuthToken(com.enonic.xp.security.auth.UsernamePasswordAuthToken) EmailPasswordAuthToken(com.enonic.xp.security.auth.EmailPasswordAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 4 with UsernamePasswordAuthToken

use of com.enonic.xp.security.auth.UsernamePasswordAuthToken in project xp by enonic.

the class LoginHandler method authenticate.

private AuthenticationInfo authenticate(IdProviderKey idProvider) {
    AuthenticationInfo authInfo = null;
    if (isValidEmail(this.user)) {
        if (this.skipAuth) {
            final VerifiedEmailAuthToken verifiedEmailAuthToken = new VerifiedEmailAuthToken();
            verifiedEmailAuthToken.setEmail(this.user);
            verifiedEmailAuthToken.setIdProvider(idProvider);
            authInfo = runAsAuthenticated(() -> this.securityService.get().authenticate(verifiedEmailAuthToken));
        } else {
            final EmailPasswordAuthToken emailAuthToken = new EmailPasswordAuthToken();
            emailAuthToken.setEmail(this.user);
            emailAuthToken.setPassword(this.password);
            emailAuthToken.setIdProvider(idProvider);
            authInfo = runAsAuthenticated(() -> this.securityService.get().authenticate(emailAuthToken));
        }
    }
    if (authInfo == null || !authInfo.isAuthenticated()) {
        if (this.skipAuth) {
            final VerifiedUsernameAuthToken usernameAuthToken = new VerifiedUsernameAuthToken();
            usernameAuthToken.setUsername(this.user);
            usernameAuthToken.setIdProvider(idProvider);
            authInfo = runAsAuthenticated(() -> this.securityService.get().authenticate(usernameAuthToken));
        } else {
            final UsernamePasswordAuthToken usernameAuthToken = new UsernamePasswordAuthToken();
            usernameAuthToken.setUsername(this.user);
            usernameAuthToken.setPassword(this.password);
            usernameAuthToken.setIdProvider(idProvider);
            authInfo = runAsAuthenticated(() -> this.securityService.get().authenticate(usernameAuthToken));
        }
    }
    return authInfo;
}
Also used : VerifiedEmailAuthToken(com.enonic.xp.security.auth.VerifiedEmailAuthToken) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) UsernamePasswordAuthToken(com.enonic.xp.security.auth.UsernamePasswordAuthToken) EmailPasswordAuthToken(com.enonic.xp.security.auth.EmailPasswordAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 5 with UsernamePasswordAuthToken

use of com.enonic.xp.security.auth.UsernamePasswordAuthToken in project xp by enonic.

the class AuthHelper method authenticate.

private AuthenticationInfo authenticate(final String user, final String password, final boolean rememberMe) {
    AuthenticationInfo authInfo = null;
    if (isValidEmail(user)) {
        final EmailPasswordAuthToken emailAuthToken = new EmailPasswordAuthToken();
        emailAuthToken.setEmail(user);
        emailAuthToken.setPassword(password);
        emailAuthToken.setRememberMe(rememberMe);
        authInfo = securityService.authenticate(emailAuthToken);
    }
    if (authInfo == null || !authInfo.isAuthenticated()) {
        final UsernamePasswordAuthToken usernameAuthToken = new UsernamePasswordAuthToken();
        usernameAuthToken.setUsername(user);
        usernameAuthToken.setPassword(password);
        usernameAuthToken.setRememberMe(rememberMe);
        authInfo = securityService.authenticate(usernameAuthToken);
    }
    return authInfo;
}
Also used : UsernamePasswordAuthToken(com.enonic.xp.security.auth.UsernamePasswordAuthToken) EmailPasswordAuthToken(com.enonic.xp.security.auth.EmailPasswordAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Aggregations

AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)5 UsernamePasswordAuthToken (com.enonic.xp.security.auth.UsernamePasswordAuthToken)5 EmailPasswordAuthToken (com.enonic.xp.security.auth.EmailPasswordAuthToken)3 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)2 CreateUserParams (com.enonic.xp.security.CreateUserParams)2 User (com.enonic.xp.security.User)2 Test (org.junit.jupiter.api.Test)2 PrincipalKey (com.enonic.xp.security.PrincipalKey)1 VerifiedEmailAuthToken (com.enonic.xp.security.auth.VerifiedEmailAuthToken)1 VerifiedUsernameAuthToken (com.enonic.xp.security.auth.VerifiedUsernameAuthToken)1