Search in sources :

Example 1 with EmailPasswordAuthToken

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

the class LoginHandlerTest method testLoginMultipleIdProvidersInOrder.

@Test
public void testLoginMultipleIdProvidersInOrder() {
    final IdProvider idProvider1 = IdProvider.create().displayName("Id Provider 1").key(IdProviderKey.from("idprovider1")).build();
    final IdProvider idProvider3 = IdProvider.create().displayName("Id Provider 3").key(IdProviderKey.from("idprovider3")).build();
    final IdProvider idProvider2 = IdProvider.create().displayName("Id Provider 2").key(IdProviderKey.from("idprovider2")).build();
    final IdProviders idProviders = IdProviders.from(idProvider1, idProvider3, idProvider2);
    final AuthenticationInfo authInfo = TestDataFixtures.createAuthenticationInfo();
    final EmailPasswordAuthToken expectedAuthToken = new EmailPasswordAuthToken();
    expectedAuthToken.setEmail("user1@enonic.com");
    expectedAuthToken.setPassword("pwd123");
    expectedAuthToken.setIdProvider(idProvider3.getKey());
    final AuthTokenMatcher matcher = new AuthTokenMatcher(expectedAuthToken);
    Mockito.when(this.securityService.authenticate(Mockito.argThat(matcher))).thenReturn(authInfo);
    Mockito.when(this.securityService.getIdProviders()).thenReturn(idProviders);
    runFunction("/test/login-test.js", "loginMultipleIdProvidersInOrder");
    final Session session = ContextAccessor.current().getLocalScope().getSession();
    final AuthenticationInfo sessionAuthInfo = session.getAttribute(AuthenticationInfo.class);
    assertEquals(authInfo, sessionAuthInfo);
    assertEquals(3, matcher.loginIdProviderAttempts.size());
    assertEquals("idprovider1", matcher.loginIdProviderAttempts.get(0).toString());
    assertEquals("idprovider2", matcher.loginIdProviderAttempts.get(1).toString());
    assertEquals("idprovider3", matcher.loginIdProviderAttempts.get(2).toString());
}
Also used : IdProviders(com.enonic.xp.security.IdProviders) IdProvider(com.enonic.xp.security.IdProvider) EmailPasswordAuthToken(com.enonic.xp.security.auth.EmailPasswordAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Session(com.enonic.xp.session.Session) Test(org.junit.jupiter.api.Test)

Example 2 with EmailPasswordAuthToken

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

the class SecurityServiceImplTest method testAuthenticateByEmailPwd.

@Test
public void testAuthenticateByEmailPwd() throws Exception {
    runAsAdmin(() -> {
        final CreateUserParams createUser = CreateUserParams.create().userKey(PrincipalKey.ofUser(SYSTEM, "User1")).displayName("User 1").email("user1@enonic.com").login("User1").password("password").build();
        final User user = securityService.createUser(createUser);
        refresh();
        final EmailPasswordAuthToken authToken = new EmailPasswordAuthToken();
        authToken.setEmail("user1@enonic.com");
        authToken.setPassword("password");
        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) EmailPasswordAuthToken(com.enonic.xp.security.auth.EmailPasswordAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with EmailPasswordAuthToken

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

the class SecurityServiceImplTest method testAuthenticateByEmailPwdWrongPwd.

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

Example 4 with EmailPasswordAuthToken

use of com.enonic.xp.security.auth.EmailPasswordAuthToken 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 5 with EmailPasswordAuthToken

use of com.enonic.xp.security.auth.EmailPasswordAuthToken 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)

Aggregations

AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)6 EmailPasswordAuthToken (com.enonic.xp.security.auth.EmailPasswordAuthToken)6 UsernamePasswordAuthToken (com.enonic.xp.security.auth.UsernamePasswordAuthToken)3 Test (org.junit.jupiter.api.Test)3 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)2 CreateUserParams (com.enonic.xp.security.CreateUserParams)2 IdProvider (com.enonic.xp.security.IdProvider)1 IdProviders (com.enonic.xp.security.IdProviders)1 User (com.enonic.xp.security.User)1 VerifiedEmailAuthToken (com.enonic.xp.security.auth.VerifiedEmailAuthToken)1 VerifiedUsernameAuthToken (com.enonic.xp.security.auth.VerifiedUsernameAuthToken)1 Session (com.enonic.xp.session.Session)1