use of com.enonic.xp.session.Session 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.session.Session in project xp by enonic.
the class LoginHandlerTest method testInvalidLogin.
@Test
public void testInvalidLogin() {
final AuthenticationInfo authInfo = AuthenticationInfo.unAuthenticated();
Mockito.when(this.securityService.authenticate(Mockito.any())).thenReturn(authInfo);
runFunction("/test/login-test.js", "invalidLogin");
final Session session = ContextAccessor.current().getLocalScope().getSession();
final AuthenticationInfo sessionAuthInfo = session.getAttribute(AuthenticationInfo.class);
assertNull(sessionAuthInfo);
}
use of com.enonic.xp.session.Session in project xp by enonic.
the class IdProviderRequestWrapperTest method isUserInRole.
@Test
void isUserInRole() {
final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN).build();
final Context context = ContextBuilder.create().build();
final Session session = new SessionMock();
context.getLocalScope().setSession(session);
session.setAttribute(authenticationInfo);
final Boolean isAdmin = context.callWith(() -> new IdProviderRequestWrapper(request).isUserInRole(RoleKeys.ADMIN.getId()));
assertTrue(isAdmin);
verifyNoInteractions(request);
}
use of com.enonic.xp.session.Session in project xp by enonic.
the class IdProviderRequestWrapperTest method getUserPrincipal.
@Test
void getUserPrincipal() {
final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).build();
final Context context = ContextBuilder.create().build();
final Session session = new SessionMock();
context.getLocalScope().setSession(session);
session.setAttribute(authenticationInfo);
final Principal principal = context.callWith(() -> new IdProviderRequestWrapper(request).getUserPrincipal());
assertEquals(principal, user);
verifyNoInteractions(request);
}
use of com.enonic.xp.session.Session in project xp by enonic.
the class ContextBuilder method mergeLocalScopeAttributes.
private static HashMap<String, Object> mergeLocalScopeAttributes(final LocalScope localScope) {
final Map<String, Object> localAttributes = localScope.getAttributes();
final Session session = localScope.getSession();
final Map<String, Object> sessionAttributes = session == null ? Map.of() : session.getAttributes();
return Stream.of(localAttributes, sessionAttributes).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, HashMap::new));
}
Aggregations