public <T> T getService(final Class<T> serviceClass) { return (client == null || !isAuthenticated()) ? anonymousClient.getService(serviceClass) : client.getService(serviceClass); }
public <T> T getService(final Class<T> serviceClass) { return (client == null || !isAuthenticated()) ? anonymousClient.getService(serviceClass) : client.getService(serviceClass); }
@SuppressWarnings("unchecked") private <T> T getCachedService(final Class<T> serviceClass) { T service; if (services.containsKey(serviceClass)) { service = (T) services.get(serviceClass); } else { service = client.getService(serviceClass); services.put(serviceClass, service); } WebClient.client(service).type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON); return service; }
@Override protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store"); response.setHeader("Pragma", "no-cache"); response.setStatus(HttpServletResponse.SC_SEE_OTHER); SyncopeClientFactoryBean clientFactory = (SyncopeClientFactoryBean) request.getServletContext(). getAttribute(Constants.SYNCOPE_CLIENT_FACTORY); String accessToken = (String) request.getSession().getAttribute(Constants.OIDCCLIENTJWT); if (StringUtils.isBlank(accessToken)) { throw new IllegalArgumentException("No access token found "); } SyncopeClient client = clientFactory.create(accessToken); OIDCLogoutRequestTO requestTO = client.getService(OIDCClientService.class). createLogoutRequest(request.getSession().getAttribute(OIDCConstants.OP).toString()); String postLogoutRedirectURI = StringUtils.substringBefore(request.getRequestURL().toString(), "/beforelogout") + "/logout"; UriBuilder ub = UriBuilder.fromUri(requestTO.getEndSessionEndpoint()); ub.queryParam(OIDCConstants.POST_LOGOUT_REDIRECT_URI, postLogoutRedirectURI); response.setHeader(HttpHeaders.LOCATION, ub.build().toASCIIString()); } }
@Override protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { SyncopeClient anonymous = (SyncopeClient) request.getServletContext(). getAttribute(Constants.SYNCOPE_ANONYMOUS_CLIENT); SAML2SPService service = anonymous.getService(SAML2SPService.class); WebClient.client(service).accept(MediaType.APPLICATION_XML_TYPE).type(MediaType.APPLICATION_XML_TYPE); try { Response metadataResponse = service.getMetadata( StringUtils.substringBefore(request.getRequestURL().toString(), "/saml2sp"), "saml2sp"); response.setContentType(metadataResponse.getMediaType().toString()); IOUtils.copy((InputStream) metadataResponse.getEntity(), response.getOutputStream()); ((InputStream) metadataResponse.getEntity()).close(); } catch (Exception e) { throw new ServletException(e.getMessage()); } } }
/** * Invalidates the JWT currently in use. */ public void logout() { getService(AccessTokenService.class).logout(); cleanup(); }
/** * Attempts to extend the lifespan of the JWT currently in use. */ public void refresh() { String jwt = getService(AccessTokenService.class).refresh().getHeaderString(RESTHeaders.TOKEN); restClientFactory.getHeaders().put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + jwt)); }
/** * Attempts to extend the lifespan of the JWT currently in use. */ public void refresh() { String jwt = getService(AccessTokenService.class).refresh().getHeaderString(RESTHeaders.TOKEN); restClientFactory.getHeaders().put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + jwt)); }
@Override protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { String idp = request.getParameter(Constants.PARAM_IDP); SyncopeClient anonymous = (SyncopeClient) request.getServletContext(). getAttribute(Constants.SYNCOPE_ANONYMOUS_CLIENT); try { SAML2RequestTO requestTO = anonymous.getService(SAML2SPService.class).createLoginRequest( StringUtils.substringBefore(request.getRequestURL().toString(), "/saml2sp"), idp); prepare(response, requestTO); } catch (Exception e) { LOG.error("While preparing authentication request to IdP", e); String errorURL = getServletContext().getInitParameter(Constants.CONTEXT_PARAM_LOGIN_ERROR_URL); if (errorURL == null) { request.setAttribute("exception", e); request.getRequestDispatcher("loginError.jsp").forward(request, response); e.printStackTrace(response.getWriter()); } else { response.sendRedirect(errorURL + "?errorMessage=" + URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8)); } } } }
/** * Invalidates the JWT currently in use. */ public void logout() { getService(AccessTokenService.class).logout(); cleanup(); }
public List<String> getDomains() { synchronized (LOG) { if (domains == null) { domains = newClientFactory().create( new AnonymousAuthenticationHandler(anonymousUser, anonymousKey)). getService(DomainService.class).list().stream().map(EntityTO::getKey). collect(Collectors.toList()); domains.add(0, SyncopeConstants.MASTER_DOMAIN); domains = ListUtils.unmodifiableList(domains); } } return domains; }
SAML2RequestTO requestTO = client.getService(SAML2SPService.class).createLogoutRequest( StringUtils.substringBefore(request.getRequestURL().toString(), "/saml2sp"));
public Pair<Map<String, Set<String>>, UserTO> self() { // Explicitly disable header value split because it interferes with JSON deserialization below UserSelfService service = getService(UserSelfService.class); WebClient.getConfig(WebClient.client(service)).getRequestContext().put(HEADER_SPLIT_PROPERTY, false); Response response = service.read(); if (response.getStatusInfo().getStatusCode() != Response.Status.OK.getStatusCode()) { Exception ex = exceptionMapper.fromResponse(response); if (ex != null) { throw (RuntimeException) ex; } } try { return Pair.of( OBJECT_MAPPER.readValue( response.getHeaderString(RESTHeaders.OWNED_ENTITLEMENTS), new TypeReference<Map<String, Set<String>>>() { }), response.readEntity(UserTO.class)); } catch (IOException e) { throw new IllegalStateException(e); } }
public Pair<Map<String, Set<String>>, UserTO> self() { // Explicitly disable header value split because it interferes with JSON deserialization below UserSelfService service = getService(UserSelfService.class); WebClient.getConfig(WebClient.client(service)).getRequestContext().put(HEADER_SPLIT_PROPERTY, false); Response response = service.read(); if (response.getStatusInfo().getStatusCode() != Response.Status.OK.getStatusCode()) { Exception ex = exceptionMapper.fromResponse(response); if (ex != null) { throw (RuntimeException) ex; } } try { return Pair.of( OBJECT_MAPPER.readValue( response.getHeaderString(RESTHeaders.OWNED_ENTITLEMENTS), new TypeReference<Map<String, Set<String>>>() { }), response.readEntity(UserTO.class)); } catch (IOException e) { throw new IllegalStateException(e); } }
@Override public void onClick(final AjaxRequestTarget target, final UserTO ignore) { try { SyncopeConsoleSession.get().getAnonymousClient().getService(UserSelfService.class). requestPasswordReset(model.getObject().getUsername(), null); SyncopeConsoleSession.get().info(getString(Constants.OPERATION_SUCCEEDED)); target.add(container); } catch (Exception e) { LOG.error("While actioning object {}", model.getObject().getKey(), e); SyncopeConsoleSession.get().error( StringUtils.isBlank(e.getMessage()) ? e.getClass().getName() : e.getMessage()); } ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target); } }, ActionType.REQUEST_PASSWORD_RESET, StandardEntitlement.USER_UPDATE).
public SyncopeEnduserSession(final Request request) { super(request); // define cookie utility to manage application cookies cookieUtils = new CookieUtils(); anonymousClient = SyncopeEnduserApplication.get().getClientFactory(). create(new AnonymousAuthenticationHandler( SyncopeEnduserApplication.get().getAnonymousUser(), SyncopeEnduserApplication.get().getAnonymousKey())); platformInfo = anonymousClient.getService(SyncopeService.class).platform(); datePlainSchemas = anonymousClient.getService(SchemaService.class). search(new SchemaQuery.Builder().type(SchemaType.PLAIN).build()); datePlainSchemas.removeAll(datePlainSchemas.stream(). filter(object -> object.getType() != AttrSchemaType.Date).collect(Collectors.toSet())); }
public SyncopeEnduserSession(final Request request) { super(request); // define cookie utility to manage application cookies cookieUtils = new CookieUtils(); anonymousClient = SyncopeWebApplication.get().getClientFactory(). create(new AnonymousAuthenticationHandler( SyncopeWebApplication.get().getAnonymousUser(), SyncopeWebApplication.get().getAnonymousKey())); platformInfo = anonymousClient.getService(SyncopeService.class).platform(); datePlainSchemas = anonymousClient.getService(SchemaService.class). search(new SchemaQuery.Builder().type(SchemaType.PLAIN).build()); datePlainSchemas.removeAll(datePlainSchemas.stream(). filter(object -> object.getType() != AttrSchemaType.Date).collect(Collectors.toSet())); }
public <T> T getService(final MediaType mediaType, final Class<T> serviceClass) { T service; synchronized (clientFactory) { SyncopeClientFactoryBean.ContentType preType = clientFactory.getContentType(); clientFactory.setContentType(SyncopeClientFactoryBean.ContentType.fromString(mediaType.toString())); service = clientFactory.create(getJWT()).getService(serviceClass); clientFactory.setContentType(preType); } return service; }
restClientFactory.setPassword(((BasicAuthenticationHandler) handler).getPassword()); String jwt = getService(AccessTokenService.class).login().getHeaderString(RESTHeaders.TOKEN); restClientFactory.getHeaders().put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + jwt));
public SyncopeConsoleSession(final Request request) { super(request); clientFactory = SyncopeWebApplication.get().newClientFactory(); anonymousClient = clientFactory. create(new AnonymousAuthenticationHandler( SyncopeWebApplication.get().getAnonymousUser(), SyncopeWebApplication.get().getAnonymousKey())); platformInfo = anonymousClient.getService(SyncopeService.class).platform(); systemInfo = anonymousClient.getService(SyncopeService.class).system(); executor = new ThreadPoolTaskExecutor(); executor.setWaitForTasksToCompleteOnShutdown(false); executor.setCorePoolSize(SyncopeWebApplication.get().getCorePoolSize()); executor.setMaxPoolSize(SyncopeWebApplication.get().getMaxPoolSize()); executor.setQueueCapacity(SyncopeWebApplication.get().getQueueCapacity()); executor.initialize(); }