use of com.enonic.xp.security.auth.AuthenticationInfo in project xp by enonic.
the class SecurityServiceImplTest method testAuthenticateByEmail.
@Test
public void testAuthenticateByEmail() 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 VerifiedEmailAuthToken authToken = new VerifiedEmailAuthToken();
authToken.setEmail("user1@enonic.com");
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.AuthenticationInfo 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.AuthenticationInfo in project xp by enonic.
the class JsonExceptionMapper method createContextJson.
private static ObjectNode createContextJson() {
final Context context = ContextAccessor.current();
final AuthenticationInfo authInfo = context.getAuthInfo();
final ObjectNode node = JsonNodeFactory.instance.objectNode();
node.put("authenticated", (authInfo != null) && authInfo.isAuthenticated());
final ArrayNode principals = node.putArray("principals");
if (authInfo != null) {
for (final PrincipalKey principal : authInfo.getPrincipals()) {
principals.add(principal.toString());
}
}
return node;
}
use of com.enonic.xp.security.auth.AuthenticationInfo in project xp by enonic.
the class ContextScriptTest method initialize.
@Override
protected void initialize() throws Exception {
super.initialize();
final SecurityService securityService = Mockito.mock(SecurityService.class);
addService(SecurityService.class, securityService);
final User user = User.create().login(PrincipalKey.ofSuperUser().getId()).displayName("Super User").key(PrincipalKey.ofSuperUser()).build();
final AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN, RoleKeys.EVERYONE).build();
Mockito.when(securityService.authenticate(Mockito.any())).thenReturn(authInfo);
}
use of com.enonic.xp.security.auth.AuthenticationInfo in project xp by enonic.
the class IdProviderFilter method doHandle.
@Override
protected void doHandle(final HttpServletRequest req, final HttpServletResponse res, final FilterChain chain) throws Exception {
// If the current user is not authenticated
final AuthenticationInfo authInfo = ContextAccessor.current().getAuthInfo();
if (!authInfo.isAuthenticated()) {
// Executes the function autoLogin of the IdProvider
IdProviderControllerExecutionParams executionParams = IdProviderControllerExecutionParams.create().functionName("autoLogin").servletRequest(req).build();
idProviderControllerService.execute(executionParams);
}
// Wraps the response to handle 403 errors
final IdProviderResponseWrapper responseWrapper = new IdProviderResponseWrapper(idProviderControllerService, req, res);
final IdProviderRequestWrapper requestWrapper = new IdProviderRequestWrapper(req);
chain.doFilter(requestWrapper, responseWrapper);
}
Aggregations