Search in sources :

Example 1 with VerifiedUsernameAuthToken

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

the class ContextHandlerBean method getAuthenticationInfo.

private AuthenticationInfo getAuthenticationInfo(final String username, final String idProvider) {
    final VerifiedUsernameAuthToken token = new VerifiedUsernameAuthToken();
    token.setUsername(username);
    token.setIdProvider(idProvider == null ? null : IdProviderKey.from(idProvider));
    return this.securityService.get().authenticate(token);
}
Also used : VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken)

Example 2 with VerifiedUsernameAuthToken

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

the class SecurityServiceImpl method authenticate.

@Override
public AuthenticationInfo authenticate(final AuthenticationToken token) {
    if (!(token instanceof VerifiedUsernameAuthToken) && !(token instanceof VerifiedEmailAuthToken)) {
        addRandomDelay();
    }
    if (isSuAuthenticationEnabled(token)) {
        return authenticateSu((UsernamePasswordAuthToken) token);
    }
    if (token.getIdProvider() != null) {
        return doAuthenticate(token);
    } else {
        final IdProviders idProviders = callAsAuthenticated(this::getIdProviders);
        for (IdProvider idProvider : idProviders) {
            token.setIdProvider(idProvider.getKey());
            final AuthenticationInfo authInfo = doAuthenticate(token);
            if (authInfo.isAuthenticated()) {
                return authInfo;
            }
        }
        return AuthenticationInfo.unAuthenticated();
    }
}
Also used : IdProviders(com.enonic.xp.security.IdProviders) VerifiedEmailAuthToken(com.enonic.xp.security.auth.VerifiedEmailAuthToken) IdProvider(com.enonic.xp.security.IdProvider) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 3 with VerifiedUsernameAuthToken

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

the class SchedulableTaskImpl method taskContext.

private Context taskContext() {
    if (job.getUser() == null) {
        return ContextBuilder.from(ContextAccessor.current()).authInfo(AuthenticationInfo.unAuthenticated()).build();
    }
    final AuthenticationInfo authInfo = OsgiSupport.withService(SecurityService.class, securityService -> {
        final VerifiedUsernameAuthToken token = new VerifiedUsernameAuthToken();
        token.setIdProvider(job.getUser().getIdProviderKey());
        token.setUsername(job.getUser().getId());
        return securityService.authenticate(token);
    });
    return ContextBuilder.from(ContextAccessor.current()).authInfo(authInfo).build();
}
Also used : VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 4 with VerifiedUsernameAuthToken

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

the class SecurityServiceImplTest method testAuthenticateByUsername.

@Test
public void testAuthenticateByUsername() 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 VerifiedUsernameAuthToken authToken = new VerifiedUsernameAuthToken();
        authToken.setUsername("user1");
        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) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with VerifiedUsernameAuthToken

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

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