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