Search in sources :

Example 1 with OIDCRequest

use of org.apache.syncope.common.lib.oidc.OIDCRequest in project syncope by apache.

the class OIDCC4UILogic method createLoginRequest.

@PreAuthorize("hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
public OIDCRequest createLoginRequest(final String redirectURI, final String opName) {
    // 1. look for OidcClient
    OidcClient oidcClient = getOidcClient(opName, redirectURI);
    oidcClient.setCallbackUrl(redirectURI);
    // 2. create OIDCRequest
    WithLocationAction action = oidcClient.getRedirectionAction(new OIDC4UIContext(), NoOpSessionStore.INSTANCE).map(WithLocationAction.class::cast).orElseThrow(() -> {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Unknown);
        sce.getElements().add("No RedirectionAction generated for LoginRequest");
        return sce;
    });
    OIDCRequest loginRequest = new OIDCRequest();
    loginRequest.setLocation(action.getLocation());
    return loginRequest;
}
Also used : OIDC4UIContext(org.apache.syncope.core.logic.oidc.OIDC4UIContext) OidcClient(org.pac4j.oidc.client.OidcClient) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) WithLocationAction(org.pac4j.core.exception.http.WithLocationAction) OIDCRequest(org.apache.syncope.common.lib.oidc.OIDCRequest) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with OIDCRequest

use of org.apache.syncope.common.lib.oidc.OIDCRequest in project syncope by apache.

the class BeforeLogoutResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
    String postLogoutRedirectURI = StringUtils.substringBefore(request.getRequestURL().toString(), "/before-logout") + "/logout";
    OIDCC4UIService service = BaseSession.class.cast(Session.get()).getService(OIDCC4UIService.class);
    OIDCRequest logoutRequest = service.createLogoutRequest(postLogoutRedirectURI);
    ResourceResponse response = new ResourceResponse();
    response.setStatusCode(Response.Status.FOUND.getStatusCode());
    response.getHeaders().addHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store");
    response.getHeaders().addHeader("Pragma", "no-cache");
    response.getHeaders().addHeader(HttpHeaders.LOCATION, logoutRequest.getLocation());
    Session.get().invalidate();
    return response;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BaseSession(org.apache.syncope.client.ui.commons.BaseSession) OIDCC4UIService(org.apache.syncope.common.rest.api.service.OIDCC4UIService) OIDCRequest(org.apache.syncope.common.lib.oidc.OIDCRequest)

Example 3 with OIDCRequest

use of org.apache.syncope.common.lib.oidc.OIDCRequest in project syncope by apache.

the class LoginResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    String op = attributes.getRequest().getQueryParameters().getParameterValue(OIDCC4UIConstants.PARAM_OP).toString();
    HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
    String redirectURI = StringUtils.substringBefore(request.getRequestURL().toString(), "/login") + "/code-consumer";
    OIDCC4UIService service = BaseSession.class.cast(Session.get()).getAnonymousService(OIDCC4UIService.class);
    OIDCRequest loginRequest = service.createLoginRequest(redirectURI, op);
    Session.get().setAttribute(OIDCConstants.OP, op);
    ResourceResponse response = new ResourceResponse();
    response.setStatusCode(Response.Status.FOUND.getStatusCode());
    response.getHeaders().addHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store");
    response.getHeaders().addHeader("Pragma", "no-cache");
    response.getHeaders().addHeader(HttpHeaders.LOCATION, loginRequest.getLocation());
    return response;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BaseSession(org.apache.syncope.client.ui.commons.BaseSession) OIDCC4UIService(org.apache.syncope.common.rest.api.service.OIDCC4UIService) OIDCRequest(org.apache.syncope.common.lib.oidc.OIDCRequest)

Example 4 with OIDCRequest

use of org.apache.syncope.common.lib.oidc.OIDCRequest in project syncope by apache.

the class OIDCC4UILogic method createLogoutRequest.

@PreAuthorize("isAuthenticated() and not(hasRole('" + IdRepoEntitlement.ANONYMOUS + "'))")
public OIDCRequest createLogoutRequest(final String accessToken, final String redirectURI) {
    // 0. fetch the current JWT used for Syncope authentication
    JWTClaimsSet claimsSet;
    try {
        SignedJWT jwt = SignedJWT.parse(accessToken);
        claimsSet = jwt.getJWTClaimsSet();
    } catch (ParseException e) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidAccessToken);
        sce.getElements().add(e.getMessage());
        throw sce;
    }
    // 1. look for OidcClient
    OidcClient oidcClient = getOidcClient((String) claimsSet.getClaim(JWT_CLAIM_OP_NAME), redirectURI);
    oidcClient.setCallbackUrl(redirectURI);
    // 2. create OIDCRequest
    OidcProfile profile = new OidcProfile();
    profile.setIdTokenString((String) claimsSet.getClaim(JWT_CLAIM_ID_TOKEN));
    WithLocationAction action = oidcClient.getLogoutAction(new OIDC4UIContext(), NoOpSessionStore.INSTANCE, profile, redirectURI).map(WithLocationAction.class::cast).orElseThrow(() -> {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Unknown);
        sce.getElements().add("No RedirectionAction generated for LogoutRequest");
        return sce;
    });
    OIDCRequest logoutRequest = new OIDCRequest();
    logoutRequest.setLocation(action.getLocation());
    return logoutRequest;
}
Also used : OIDC4UIContext(org.apache.syncope.core.logic.oidc.OIDC4UIContext) OidcClient(org.pac4j.oidc.client.OidcClient) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) OidcProfile(org.pac4j.oidc.profile.OidcProfile) WithLocationAction(org.pac4j.core.exception.http.WithLocationAction) SignedJWT(com.nimbusds.jwt.SignedJWT) ParseException(java.text.ParseException) OIDCRequest(org.apache.syncope.common.lib.oidc.OIDCRequest) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

OIDCRequest (org.apache.syncope.common.lib.oidc.OIDCRequest)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 BaseSession (org.apache.syncope.client.ui.commons.BaseSession)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 OIDCC4UIService (org.apache.syncope.common.rest.api.service.OIDCC4UIService)2 OIDC4UIContext (org.apache.syncope.core.logic.oidc.OIDC4UIContext)2 WithLocationAction (org.pac4j.core.exception.http.WithLocationAction)2 OidcClient (org.pac4j.oidc.client.OidcClient)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 ParseException (java.text.ParseException)1 OidcProfile (org.pac4j.oidc.profile.OidcProfile)1