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