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());
}
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());
});
}
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());
});
}
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;
}
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;
}
Aggregations