Search in sources :

Example 16 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class CasConfiguration method internalInit.

@Override
protected void internalInit() {
    if (CommonHelper.isBlank(this.loginUrl) && CommonHelper.isBlank(this.prefixUrl) && CommonHelper.isBlank(this.restUrl)) {
        throw new TechnicalException("loginUrl, prefixUrl and restUrl cannot be all blank");
    }
    if (urlResolver == null) {
        urlResolver = new DefaultUrlResolver();
    }
    initializeClientConfiguration();
    initializeLogoutHandler();
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) DefaultUrlResolver(org.pac4j.core.http.url.DefaultUrlResolver)

Example 17 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class CasAuthenticator method validate.

@Override
public void validate(final TokenCredentials credentials, final WebContext context) {
    init();
    final String ticket = credentials.getToken();
    try {
        final String finalCallbackUrl = callbackUrlResolver.compute(urlResolver, callbackUrl, clientName, context);
        final Assertion assertion = configuration.retrieveTicketValidator(context).validate(ticket, finalCallbackUrl);
        final AttributePrincipal principal = assertion.getPrincipal();
        logger.debug("principal: {}", principal);
        final String id = principal.getName();
        final Map<String, Object> newPrincipalAttributes = new HashMap<>();
        final Map<String, Object> newAuthenticationAttributes = new HashMap<>();
        // restore both sets of attributes
        final Map<String, Object> oldPrincipalAttributes = principal.getAttributes();
        final Map<String, Object> oldAuthenticationAttributes = assertion.getAttributes();
        final InternalAttributeHandler attrHandler = ProfileHelper.getInternalAttributeHandler();
        if (oldPrincipalAttributes != null) {
            oldPrincipalAttributes.entrySet().stream().forEach(e -> newPrincipalAttributes.put(e.getKey(), attrHandler.restore(e.getValue())));
        }
        if (oldAuthenticationAttributes != null) {
            oldAuthenticationAttributes.entrySet().stream().forEach(e -> newAuthenticationAttributes.put(e.getKey(), attrHandler.restore(e.getValue())));
        }
        final CommonProfile profile;
        // in case of CAS proxy, don't restore the profile, just build a CAS one
        if (configuration.getProxyReceptor() != null) {
            profile = getProfileDefinition().newProfile(principal, configuration.getProxyReceptor());
            profile.setId(ProfileHelper.sanitizeIdentifier(profile, id));
            getProfileDefinition().convertAndAdd(profile, newPrincipalAttributes, newAuthenticationAttributes);
        } else {
            profile = ProfileHelper.restoreOrBuildProfile(getProfileDefinition(), id, newPrincipalAttributes, newAuthenticationAttributes, principal, configuration.getProxyReceptor());
        }
        logger.debug("profile returned by CAS: {}", profile);
        credentials.setUserProfile(profile);
    } catch (final TicketValidationException e) {
        String message = "cannot validate CAS ticket: " + ticket;
        throw new TechnicalException(message, e);
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) InternalAttributeHandler(org.pac4j.core.profile.InternalAttributeHandler) HashMap(java.util.HashMap) CommonProfile(org.pac4j.core.profile.CommonProfile) Assertion(org.jasig.cas.client.validation.Assertion) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal) TicketValidationException(org.jasig.cas.client.validation.TicketValidationException)

Example 18 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class AbstractEncryptionConfiguration method encrypt.

@Override
public String encrypt(final JWT jwt) {
    init();
    if (jwt instanceof SignedJWT) {
        // Create JWE object with signed JWT as payload
        final var jweObject = new JWEObject(new JWEHeader.Builder(this.algorithm, this.method).contentType("JWT").build(), new Payload((SignedJWT) jwt));
        try {
            // Perform encryption
            jweObject.encrypt(buildEncrypter());
        } catch (final JOSEException e) {
            throw new TechnicalException(e);
        }
        // Serialise to JWE compact form
        return jweObject.serialize();
    } else {
        // create header
        final var header = new JWEHeader(this.algorithm, this.method);
        try {
            // encrypted jwt
            var encryptedJwt = new EncryptedJWT(header, jwt.getJWTClaimsSet());
            // Perform encryption
            encryptedJwt.encrypt(buildEncrypter());
            // serialize
            return encryptedJwt.serialize();
        } catch (final JOSEException | ParseException e) {
            throw new TechnicalException(e);
        }
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) SignedJWT(com.nimbusds.jwt.SignedJWT) ParseException(java.text.ParseException) EncryptedJWT(com.nimbusds.jwt.EncryptedJWT)

Example 19 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class SecretSignatureConfiguration method sign.

@Override
public SignedJWT sign(final JWTClaimsSet claims) {
    init();
    try {
        final JWSSigner signer = new MACSigner(this.secret);
        final var signedJWT = new SignedJWT(new JWSHeader(algorithm), claims);
        signedJWT.sign(signer);
        return signedJWT;
    } catch (final JOSEException e) {
        throw new TechnicalException(e);
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) MACSigner(com.nimbusds.jose.crypto.MACSigner) SignedJWT(com.nimbusds.jwt.SignedJWT)

Example 20 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class Saml2MetadataFilter method internalFilter.

@Override
protected void internalFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
    CommonHelper.assertNotNull("config", getSharedConfig());
    CommonHelper.assertNotNull("clientName", clientName);
    SAML2Client client;
    final var result = getSharedConfig().getClients().findClient(this.clientName);
    if (result.isPresent()) {
        client = (SAML2Client) result.get();
    } else {
        throw new TechnicalException("No SAML2 client: " + this.clientName);
    }
    client.init();
    response.getWriter().write(client.getServiceProviderMetadataResolver().getMetadata());
    response.getWriter().flush();
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) SAML2Client(org.pac4j.saml.client.SAML2Client)

Aggregations

TechnicalException (org.pac4j.core.exception.TechnicalException)81 IOException (java.io.IOException)26 URI (java.net.URI)7 URISyntaxException (java.net.URISyntaxException)7 HashMap (java.util.HashMap)7 OAuthException (com.github.scribejava.core.exceptions.OAuthException)6 JWT (com.nimbusds.jwt.JWT)6 ParseException (com.nimbusds.oauth2.sdk.ParseException)6 HttpURLConnection (java.net.HttpURLConnection)6 Test (org.junit.Test)6 OidcCredentials (org.pac4j.oidc.credentials.OidcCredentials)6 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)5 SignedJWT (com.nimbusds.jwt.SignedJWT)5 ArrayList (java.util.ArrayList)5 ComponentInitializationException (net.shibboleth.utilities.java.support.component.ComponentInitializationException)5 JOSEException (com.nimbusds.jose.JOSEException)4 URL (java.net.URL)4 HTTPRequest (com.nimbusds.oauth2.sdk.http.HTTPRequest)3 HTTPResponse (com.nimbusds.oauth2.sdk.http.HTTPResponse)3 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)3