Search in sources :

Example 1 with IdProviderKey

use of com.enonic.xp.security.IdProviderKey in project xp by enonic.

the class IdentityHandler method doHandle.

@Override
protected PortalResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    final String restPath = findRestPath(webRequest);
    final Matcher matcher = PATTERN.matcher(restPath);
    if (!matcher.find()) {
        throw WebException.notFound("Not a valid idprovider url pattern");
    }
    final IdProviderKey idProviderKey = IdProviderKey.from(matcher.group(ID_PROVIDER_GROUP_INDEX));
    final VirtualHost virtualHost = VirtualHostHelper.getVirtualHost(webRequest.getRawRequest());
    if (!(virtualHost == null || virtualHost.getIdProviderKeys().contains(idProviderKey))) {
        throw WebException.forbidden(String.format("'%s' id provider is forbidden", idProviderKey));
    }
    String idProviderFunction = matcher.group(2);
    final PortalRequest portalRequest = webRequest instanceof PortalRequest ? (PortalRequest) webRequest : new PortalRequest(webRequest);
    portalRequest.setContextPath(findPreRestPath(portalRequest) + "/" + matcher.group(ID_PROVIDER_GROUP_INDEX));
    if (idProviderFunction != null) {
        checkTicket(portalRequest);
    }
    if (idProviderFunction == null) {
        idProviderFunction = webRequest.getMethod().toString().toLowerCase();
    }
    final IdentityHandlerWorker worker = new IdentityHandlerWorker(portalRequest);
    worker.idProviderKey = idProviderKey;
    worker.idProviderFunction = idProviderFunction;
    worker.contentResolver = new ContentResolver(contentService);
    worker.idProviderControllerService = this.idProviderControllerService;
    final Trace trace = Tracer.newTrace("portalRequest");
    if (trace == null) {
        return worker.execute();
    }
    trace.put("path", webRequest.getPath());
    trace.put("method", webRequest.getMethod().toString());
    trace.put("host", webRequest.getHost());
    trace.put("httpRequest", webRequest);
    trace.put("httpResponse", webResponse);
    trace.put("context", ContextAccessor.current());
    return Tracer.traceEx(trace, () -> {
        final PortalResponse response = worker.execute();
        addTraceInfo(trace, response);
        return response;
    });
}
Also used : Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) Matcher(java.util.regex.Matcher) IdProviderKey(com.enonic.xp.security.IdProviderKey) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) PortalRequest(com.enonic.xp.portal.PortalRequest) ContentResolver(com.enonic.xp.portal.impl.ContentResolver)

Example 2 with IdProviderKey

use of com.enonic.xp.security.IdProviderKey in project xp by enonic.

the class IdProviderControllerServiceImpl method execute.

@Override
public PortalResponse execute(final IdProviderControllerExecutionParams params) throws IOException {
    final IdProviderKey idProviderKey = retrieveIdProviderKey(params);
    final IdProvider idProvider = retrieveIdProvider(idProviderKey);
    final IdProviderDescriptor idProviderDescriptor = retrieveIdProviderDescriptor(idProvider);
    if (idProviderDescriptor != null) {
        final IdProviderControllerScript idProviderControllerScript = idProviderControllerScriptFactory.fromScript(idProviderDescriptor.getResourceKey());
        final String functionName = params.getFunctionName();
        if (idProviderControllerScript.hasMethod(functionName)) {
            PortalRequest portalRequest = params.getPortalRequest();
            if (portalRequest == null) {
                portalRequest = new PortalRequestAdapter().adapt(params.getServletRequest());
            }
            portalRequest.setApplicationKey(idProviderDescriptor.getKey());
            portalRequest.setIdProvider(idProvider);
            final PortalResponse portalResponse = idProviderControllerScript.execute(functionName, portalRequest);
            if (portalResponse != null) {
                final HttpServletResponse response = params.getResponse();
                if (response != null) {
                    responseSerializationService.serialize(portalRequest, portalResponse, response);
                }
            }
            return portalResponse;
        }
    }
    return null;
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) PortalRequestAdapter(com.enonic.xp.portal.impl.PortalRequestAdapter) IdProvider(com.enonic.xp.security.IdProvider) IdProviderDescriptor(com.enonic.xp.idprovider.IdProviderDescriptor) IdProviderKey(com.enonic.xp.security.IdProviderKey) HttpServletResponse(javax.servlet.http.HttpServletResponse) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 3 with IdProviderKey

use of com.enonic.xp.security.IdProviderKey in project xp by enonic.

the class AuthenticationInfoTest method testSerialization.

@Test
void testSerialization() throws Exception {
    final User user = User.create().login("userlogin").displayName("my user").key(PrincipalKey.ofUser(IdProviderKey.from("myidprovider"), "userid")).email("user@email").description("description").modifiedTime(Instant.now(clock)).build();
    final IdProviderKey idProvider = IdProviderKey.from("myStore");
    final PrincipalKey group1 = PrincipalKey.ofGroup(idProvider, "group1");
    final PrincipalKey group2 = PrincipalKey.from("group:myStore:group2");
    final PrincipalKey role1 = PrincipalKey.from("role:administrators");
    final AuthenticationInfo info = AuthenticationInfo.create().user(user).principals(group1).principals(PrincipalKeys.from(group2, role1)).build();
    final byte[] serializedObject = serialize(info);
    final AuthenticationInfo deserializedObject = (AuthenticationInfo) deserialize(serializedObject);
    assertEquals(deserializedObject, info);
}
Also used : User(com.enonic.xp.security.User) IdProviderKey(com.enonic.xp.security.IdProviderKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 4 with IdProviderKey

use of com.enonic.xp.security.IdProviderKey in project xp by enonic.

the class AuthenticationInfoTest method testWithPrincipals.

@Test
public void testWithPrincipals() {
    final User user = User.create().login("userlogin").displayName("my user").key(PrincipalKey.ofUser(IdProviderKey.from("myidprovider"), "userid")).email("user@email").modifiedTime(Instant.now(clock)).build();
    final IdProviderKey idProvider = IdProviderKey.from("myStore");
    final PrincipalKey group1 = PrincipalKey.ofGroup(idProvider, "group1");
    final PrincipalKey group2 = PrincipalKey.from("group:myStore:group2");
    final PrincipalKey role1 = PrincipalKey.from("role:administrators");
    final AuthenticationInfo info = AuthenticationInfo.create().user(user).principals(group1).principals(PrincipalKeys.from(group2, role1)).build();
    assertEquals("userlogin", info.getUser().getLogin());
    assertEquals("my user", info.getUser().getDisplayName());
    assertEquals(PrincipalKey.from("user:myidprovider:userid"), info.getUser().getKey());
    assertEquals(4, info.getPrincipals().getSize());
    assertTrue(info.getPrincipals().contains(PrincipalKey.from("user:myidprovider:userid")));
    assertTrue(info.getPrincipals().contains(group1));
    assertTrue(info.getPrincipals().contains(group2));
    assertTrue(info.getPrincipals().contains(role1));
    assertFalse(info.hasRole("userid"));
    assertFalse(info.hasRole("group1"));
    assertFalse(info.hasRole("group2"));
    assertTrue(info.hasRole("administrators"));
}
Also used : User(com.enonic.xp.security.User) IdProviderKey(com.enonic.xp.security.IdProviderKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 5 with IdProviderKey

use of com.enonic.xp.security.IdProviderKey in project xp by enonic.

the class AuthenticationInfoTest method testCopy.

@Test
public void testCopy() {
    final User user = User.create().login("userlogin").displayName("my user").key(PrincipalKey.ofUser(IdProviderKey.from("myidprovider"), "userid")).email("user@email").modifiedTime(Instant.now(clock)).build();
    final IdProviderKey idProvider = IdProviderKey.from("myStore");
    final PrincipalKey group1 = PrincipalKey.ofGroup(idProvider, "group1");
    final PrincipalKey group2 = PrincipalKey.from("group:myStore:group2");
    final PrincipalKey role1 = PrincipalKey.from("role:administrators");
    final AuthenticationInfo info = AuthenticationInfo.create().user(user).principals(group1).principals(PrincipalKeys.from(group2, role1)).build();
    final AuthenticationInfo copy = AuthenticationInfo.copyOf(info).build();
    assertEquals(info, copy);
    assertEquals(info.hashCode(), copy.hashCode());
}
Also used : User(com.enonic.xp.security.User) IdProviderKey(com.enonic.xp.security.IdProviderKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Aggregations

IdProviderKey (com.enonic.xp.security.IdProviderKey)11 PrincipalKey (com.enonic.xp.security.PrincipalKey)4 User (com.enonic.xp.security.User)4 Test (org.junit.jupiter.api.Test)4 IdProvider (com.enonic.xp.security.IdProvider)3 PropertySet (com.enonic.xp.data.PropertySet)2 PortalRequest (com.enonic.xp.portal.PortalRequest)2 PortalResponse (com.enonic.xp.portal.PortalResponse)2 VirtualHost (com.enonic.xp.web.vhost.VirtualHost)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 PropertyTree (com.enonic.xp.data.PropertyTree)1 IdProviderDescriptor (com.enonic.xp.idprovider.IdProviderDescriptor)1 NodeQuery (com.enonic.xp.node.NodeQuery)1 ContentResolver (com.enonic.xp.portal.impl.ContentResolver)1 PortalRequestAdapter (com.enonic.xp.portal.impl.PortalRequestAdapter)1 IdProviderConfig (com.enonic.xp.security.IdProviderConfig)1 IdProviderKeys (com.enonic.xp.security.IdProviderKeys)1 IdProviders (com.enonic.xp.security.IdProviders)1 PrincipalType (com.enonic.xp.security.PrincipalType)1