Search in sources :

Example 76 with TechnicalException

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

the class SAML2ClientTests method testSaml2ConfigurationOfKeyStore.

@Test
public void testSaml2ConfigurationOfKeyStore() throws IOException {
    final Resource rs = new FileSystemResource("testKeystore.jks");
    if (rs.exists() && !rs.getFile().delete()) {
        throw new TechnicalException("File could not be deleted");
    }
    final var cfg = new SAML2Configuration("testKeystore.jks", "pac4j-test-passwd", "pac4j-test-passwd", "resource:testshib-providers.xml");
    cfg.init();
    final var p = new KeyStoreCredentialProvider(cfg);
    assertNotNull(p.getKeyInfoGenerator());
    assertNotNull(p.getCredentialResolver());
    assertNotNull(p.getKeyInfo());
    assertNotNull(p.getKeyInfoCredentialResolver());
    assertNotNull(p.getCredential());
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) SAML2Configuration(org.pac4j.saml.config.SAML2Configuration) KeyStoreCredentialProvider(org.pac4j.saml.crypto.KeyStoreCredentialProvider) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) Test(org.junit.Test)

Example 77 with TechnicalException

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

the class SAML2Utils method buildChainingMetadataResolver.

public static ChainingMetadataResolver buildChainingMetadataResolver(final SAML2MetadataResolver idpMetadataProvider, final SAML2MetadataResolver spMetadataProvider) {
    final var metadataManager = new ChainingMetadataResolver();
    metadataManager.setId(ChainingMetadataResolver.class.getCanonicalName());
    try {
        final List<MetadataResolver> list = new ArrayList<>();
        list.add(idpMetadataProvider.resolve());
        list.add(spMetadataProvider.resolve());
        metadataManager.setResolvers(list);
        metadataManager.initialize();
    } catch (final ResolverException e) {
        throw new TechnicalException("Error adding idp or sp metadatas to manager", e);
    } catch (final ComponentInitializationException e) {
        throw new TechnicalException("Error initializing manager", e);
    }
    return metadataManager;
}
Also used : ChainingMetadataResolver(org.opensaml.saml.metadata.resolver.ChainingMetadataResolver) ResolverException(net.shibboleth.utilities.java.support.resolver.ResolverException) TechnicalException(org.pac4j.core.exception.TechnicalException) ComponentInitializationException(net.shibboleth.utilities.java.support.component.ComponentInitializationException) ArrayList(java.util.ArrayList) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver) ChainingMetadataResolver(org.opensaml.saml.metadata.resolver.ChainingMetadataResolver) SAML2MetadataResolver(org.pac4j.saml.metadata.SAML2MetadataResolver)

Example 78 with TechnicalException

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

the class SAML2IdentityProviderMetadataResolverTest method resolveMetadataOverUrlWithHostnameVerifier.

@Test
public void resolveMetadataOverUrlWithHostnameVerifier() throws Exception {
    var configuration = new SAML2Configuration();
    configuration.setIdentityProviderMetadataResource(new UrlResource("https://self-signed.badssl.com"));
    metadataResolver = new SAML2IdentityProviderMetadataResolver(configuration);
    try {
        metadataResolver.init();
    } catch (final TechnicalException e) {
        assertEquals(SSLHandshakeException.class, e.getCause().getClass());
    }
    metadataResolver.setHostnameVerifier((s, sslSession) -> true);
    metadataResolver.setSslSocketFactory(disabledSslContext().getSocketFactory());
    try {
        metadataResolver.init();
    } catch (final TechnicalException e) {
        assertEquals(XMLParserException.class, e.getCause().getClass());
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) SAML2Configuration(org.pac4j.saml.config.SAML2Configuration) UrlResource(org.springframework.core.io.UrlResource) XMLParserException(net.shibboleth.utilities.java.support.xml.XMLParserException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Test(org.junit.Test)

Example 79 with TechnicalException

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

the class OAuthCredentialsExtractor method extract.

@Override
public Optional<Credentials> extract(final WebContext context, final SessionStore sessionStore) {
    final boolean hasBeenCancelled = (Boolean) configuration.getHasBeenCancelledFactory().apply(context);
    // check if the authentication has been cancelled
    if (hasBeenCancelled) {
        logger.debug("authentication has been cancelled by user");
        return Optional.empty();
    }
    // check errors
    try {
        var errorFound = false;
        final var oauthCredentialsException = new OAuthCredentialsException("Failed to retrieve OAuth credentials, error parameters found");
        for (final var key : OAuthCredentialsException.ERROR_NAMES) {
            final var value = context.getRequestParameter(key);
            if (value.isPresent()) {
                errorFound = true;
                oauthCredentialsException.setErrorMessage(key, value.get());
            }
        }
        if (errorFound) {
            throw oauthCredentialsException;
        } else {
            return getOAuthCredentials(context, sessionStore);
        }
    } catch (final OAuthException e) {
        throw new TechnicalException(e);
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) OAuthException(com.github.scribejava.core.exceptions.OAuthException) OAuthCredentialsException(org.pac4j.oauth.exception.OAuthCredentialsException)

Example 80 with TechnicalException

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

the class OAuth10RedirectionActionBuilder method getRedirectionAction.

@Override
public Optional<RedirectionAction> getRedirectionAction(final WebContext context, final SessionStore sessionStore) {
    try {
        final var service = (OAuth10aService) this.configuration.buildService(context, client);
        final OAuth1RequestToken requestToken;
        try {
            requestToken = service.getRequestToken();
        } catch (final IOException | InterruptedException | ExecutionException e) {
            throw new HttpCommunicationException("Error getting token: " + e.getMessage());
        }
        logger.debug("requestToken: {}", requestToken);
        // save requestToken in user session
        sessionStore.set(context, configuration.getRequestTokenSessionAttributeName(client.getName()), requestToken);
        final var authorizationUrl = service.getAuthorizationUrl(requestToken);
        logger.debug("authorizationUrl: {}", authorizationUrl);
        return Optional.of(HttpActionHelper.buildRedirectUrlAction(context, authorizationUrl));
    } catch (final OAuthException e) {
        throw new TechnicalException(e);
    }
}
Also used : OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) TechnicalException(org.pac4j.core.exception.TechnicalException) HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) OAuthException(com.github.scribejava.core.exceptions.OAuthException) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

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