Search in sources :

Example 41 with TechnicalException

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

the class YahooAuthenticator method validate.

@Override
public void validate(final OpenIdCredentials credentials, final WebContext context) {
    final ParameterList parameterList = credentials.getParameterList();
    final DiscoveryInformation discoveryInformation = credentials.getDiscoveryInformation();
    logger.debug("parameterList: {}", parameterList);
    logger.debug("discoveryInformation: {}", discoveryInformation);
    try {
        // verify the response
        final VerificationResult verification = this.client.getConsumerManager().verify(this.client.computeFinalCallbackUrl(context), parameterList, discoveryInformation);
        // examine the verification result and extract the verified identifier
        final Identifier verified = verification.getVerifiedId();
        if (verified != null) {
            final AuthSuccess authSuccess = (AuthSuccess) verification.getAuthResponse();
            logger.debug("authSuccess: {}", authSuccess);
            final YahooOpenIdProfile profile = createProfile(authSuccess);
            profile.setId(verified.getIdentifier());
            logger.debug("profile: {}", profile);
            credentials.setUserProfile(profile);
            return;
        }
    } catch (final OpenIDException e) {
        throw new TechnicalException("OpenID exception", e);
    }
    final String message = "No verifiedId found";
    throw new TechnicalException(message);
}
Also used : YahooOpenIdProfile(org.pac4j.openid.profile.yahoo.YahooOpenIdProfile) Identifier(org.openid4java.discovery.Identifier) TechnicalException(org.pac4j.core.exception.TechnicalException) VerificationResult(org.openid4java.consumer.VerificationResult) OpenIDException(org.openid4java.OpenIDException) DiscoveryInformation(org.openid4java.discovery.DiscoveryInformation) AuthSuccess(org.openid4java.message.AuthSuccess) ParameterList(org.openid4java.message.ParameterList)

Example 42 with TechnicalException

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

the class YahooRedirectActionBuilder method redirect.

@Override
public RedirectAction redirect(final WebContext context) {
    try {
        // perform discovery on the user-supplied identifier
        final List discoveries = this.client.getConsumerManager().discover(YAHOO_GENERIC_USER_IDENTIFIER);
        // attempt to associate with the OpenID provider
        // and retrieve one service endpoint for authentication
        final DiscoveryInformation discoveryInformation = this.client.getConsumerManager().associate(discoveries);
        // save discovery information in session
        context.getSessionStore().set(context, this.client.getDiscoveryInformationSessionAttributeName(), discoveryInformation);
        // create authentication request to be sent to the OpenID provider
        final AuthRequest authRequest = this.client.getConsumerManager().authenticate(discoveryInformation, this.client.computeFinalCallbackUrl(context));
        // create fetch request for attributes
        final FetchRequest fetchRequest = getFetchRequest();
        if (fetchRequest != null) {
            authRequest.addExtension(fetchRequest);
        }
        final String redirectionUrl = authRequest.getDestinationUrl(true);
        logger.debug("redirectionUrl: {}", redirectionUrl);
        return RedirectAction.redirect(redirectionUrl);
    } catch (final OpenIDException e) {
        throw new TechnicalException("OpenID exception", e);
    }
}
Also used : AuthRequest(org.openid4java.message.AuthRequest) TechnicalException(org.pac4j.core.exception.TechnicalException) OpenIDException(org.openid4java.OpenIDException) DiscoveryInformation(org.openid4java.discovery.DiscoveryInformation) FetchRequest(org.openid4java.message.ax.FetchRequest) List(java.util.List)

Example 43 with TechnicalException

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

the class OAuth10RedirectActionBuilder method redirect.

@Override
public RedirectAction redirect(final WebContext context) {
    try {
        final OAuth10aService service = this.configuration.buildService(context, client, null);
        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
        context.getSessionStore().set(context, configuration.getRequestTokenSessionAttributeName(client.getName()), requestToken);
        final String authorizationUrl = service.getAuthorizationUrl(requestToken);
        logger.debug("authorizationUrl: {}", authorizationUrl);
        return RedirectAction.redirect(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)

Example 44 with TechnicalException

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

the class OidcLogoutActionBuilder method getLogoutAction.

@Override
public RedirectAction getLogoutAction(final WebContext context, final U currentProfile, final String targetUrl) {
    final String logoutUrl = configuration.getLogoutUrl();
    if (CommonHelper.isNotBlank(logoutUrl)) {
        try {
            final URI endSessionEndpoint = new URI(logoutUrl);
            final JWT idToken = currentProfile.getIdToken();
            LogoutRequest logoutRequest;
            if (CommonHelper.isNotBlank(targetUrl)) {
                logoutRequest = new LogoutRequest(endSessionEndpoint, idToken, new URI(targetUrl), null);
            } else {
                logoutRequest = new LogoutRequest(endSessionEndpoint, idToken);
            }
            if (ajaxRequestResolver.isAjax(context)) {
                context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, "");
                context.setResponseHeader(HttpConstants.LOCATION_HEADER, logoutRequest.toURI().toString());
                throw HttpAction.status(403, context);
            }
            return RedirectAction.redirect(logoutRequest.toURI().toString());
        } catch (final URISyntaxException e) {
            throw new TechnicalException(e);
        }
    }
    return null;
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) JWT(com.nimbusds.jwt.JWT) LogoutRequest(com.nimbusds.openid.connect.sdk.LogoutRequest) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 45 with TechnicalException

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

the class AzureAdProfile method isExpired.

@Override
public boolean isExpired() {
    try {
        JWT jwt = this.getIdToken();
        JWTClaimsSet claims = jwt.getJWTClaimsSet();
        Date expiresOn = claims.getExpirationTime();
        Calendar now = Calendar.getInstance();
        now.add(Calendar.SECOND, idTokenExpireAdvance);
        if (expiresOn.before(now.getTime())) {
            return true;
        }
    } catch (ParseException e) {
        throw new TechnicalException(e);
    }
    return false;
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) JWT(com.nimbusds.jwt.JWT) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) Calendar(java.util.Calendar) ParseException(java.text.ParseException) Date(java.util.Date)

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