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