Search in sources :

Example 21 with TechnicalException

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

the class RSASignatureConfiguration method sign.

@Override
public SignedJWT sign(JWTClaimsSet claims) {
    init();
    CommonHelper.assertNotNull("privateKey", privateKey);
    try {
        final JWSSigner signer = new RSASSASigner(this.privateKey);
        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) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) SignedJWT(com.nimbusds.jwt.SignedJWT)

Example 22 with TechnicalException

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

the class SunJaasKerberosTicketValidator method internalInit.

@Override
protected void internalInit(final boolean forceReinit) {
    // then internalInit() runs lazily during the first validateTicket() call
    try {
        CommonHelper.assertNotNull("servicePrincipal must be specified", this.servicePrincipal);
        CommonHelper.assertNotNull("keyTab must be specified", this.keyTabLocation);
        var keyTabLocationAsString = this.keyTabLocation.getURL().toExternalForm();
        // As Java 6 accepts it with and without the prefix, we don't need to check for Java 7
        if (keyTabLocationAsString.startsWith("file:")) {
            keyTabLocationAsString = keyTabLocationAsString.substring(5);
        }
        var loginConfig = new LoginConfig(keyTabLocationAsString, this.servicePrincipal, this.debug);
        Set<Principal> princ = new HashSet<>(1);
        princ.add(new KerberosPrincipal(this.servicePrincipal));
        var sub = new Subject(false, princ, new HashSet<>(), new HashSet<>());
        var lc = new LoginContext("", sub, null, loginConfig);
        lc.login();
        this.serviceSubject = lc.getSubject();
    } catch (final LoginException | IOException e) {
        throw new TechnicalException(e);
    }
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) TechnicalException(org.pac4j.core.exception.TechnicalException) IOException(java.io.IOException) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) LoginException(javax.security.auth.login.LoginException) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 23 with TechnicalException

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

the class SAML2HttpClientBuilder method build.

public HttpClient build() {
    try {
        final var builder = new Pac4jHttpClientBuilder();
        builder.resetDefaults();
        if (this.connectionTimeout != null) {
            builder.setConnectionTimeout(this.connectionTimeout);
        }
        builder.setUseSystemProperties(this.useSystemProperties);
        if (this.socketTimeout != null) {
            builder.setSocketTimeout(this.socketTimeout);
        }
        builder.setHttpFollowRedirects(this.followRedirects);
        builder.setMaxConnectionsTotal(this.maxConnectionsTotal);
        builder.setConnectionCloseAfterResponse(this.closeConnectionAfterResponse);
        if (this.credentialsProvider != null) {
            builder.getApacheBuilder().setDefaultCredentialsProvider(credentialsProvider);
        }
        return builder.buildClient();
    } catch (final Exception e) {
        throw new TechnicalException(e);
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 24 with TechnicalException

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

the class VelocityEngineFactory method getEngine.

public static VelocityEngine getEngine() {
    try {
        final var props = new Properties();
        props.putAll(net.shibboleth.utilities.java.support.velocity.VelocityEngine.getDefaultProperties());
        props.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
        props.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        final var velocityEngine = net.shibboleth.utilities.java.support.velocity.VelocityEngine.newVelocityEngine(props);
        return velocityEngine;
    } catch (final Exception e) {
        throw new TechnicalException("Error configuring velocity", e);
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) Properties(java.util.Properties) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 25 with TechnicalException

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

the class CasAuthenticator method validate.

@Override
public void validate(final Credentials cred, final WebContext context, final SessionStore sessionStore) {
    init();
    final var credentials = (TokenCredentials) cred;
    final var ticket = credentials.getToken();
    try {
        final var finalCallbackUrl = callbackUrlResolver.compute(urlResolver, callbackUrl, clientName, context);
        final var assertion = configuration.retrieveTicketValidator(context).validate(ticket, finalCallbackUrl);
        final var principal = assertion.getPrincipal();
        logger.debug("principal: {}", principal);
        final var id = principal.getName();
        final Map<String, Object> newPrincipalAttributes = new HashMap<>();
        final Map<String, Object> newAuthenticationAttributes = new HashMap<>();
        // restore both sets of attributes
        final var oldPrincipalAttributes = principal.getAttributes();
        final var oldAuthenticationAttributes = assertion.getAttributes();
        if (oldPrincipalAttributes != null) {
            oldPrincipalAttributes.entrySet().stream().forEach(e -> newPrincipalAttributes.put(e.getKey(), e.getValue()));
        }
        if (oldAuthenticationAttributes != null) {
            oldAuthenticationAttributes.entrySet().stream().forEach(e -> newAuthenticationAttributes.put(e.getKey(), e.getValue()));
        }
        final var profile = getProfileDefinition().newProfile(id, configuration.getProxyReceptor(), principal);
        profile.setId(ProfileHelper.sanitizeIdentifier(id));
        getProfileDefinition().convertAndAdd(profile, newPrincipalAttributes, newAuthenticationAttributes);
        logger.debug("profile returned by CAS: {}", profile);
        credentials.setUserProfile(profile);
    } catch (final TicketValidationException e) {
        var message = "cannot validate CAS ticket: " + ticket;
        throw new TechnicalException(message, e);
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) HashMap(java.util.HashMap) TicketValidationException(org.jasig.cas.client.validation.TicketValidationException) TokenCredentials(org.pac4j.core.credentials.TokenCredentials)

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