Search in sources :

Example 1 with TechnicalException

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

the class RatpackPac4j method initiateAuthentication.

private static void initiateAuthentication(Context ctx, Class<? extends Client<?, ?>> clientType) {
    Request request = ctx.getRequest();
    Clients clients = ctx.get(Clients.class);
    Client<?, ?> client = clients.findClient(clientType);
    RatpackWebContext.from(ctx, false).then(webContext -> {
        webContext.getSession().set(Pac4jSessionKeys.REQUESTED_URL, request.getUri());
        try {
            client.redirect(webContext, true);
        } catch (Exception e) {
            if (e instanceof RequiresHttpAction) {
                webContext.sendResponse((RequiresHttpAction) e);
                return;
            } else {
                ctx.error(new TechnicalException("Failed to redirect", e));
            }
        }
        webContext.sendResponse();
    });
}
Also used : RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) TechnicalException(org.pac4j.core.exception.TechnicalException) Request(ratpack.http.Request) Clients(org.pac4j.core.client.Clients) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 2 with TechnicalException

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

the class KnoxSessionStore method set.

public void set(WebContext context, String key, Object value) {
    logger.debug("Save in session: {} = {}", key, value);
    final Cookie cookie = new Cookie(PAC4J_SESSION_PREFIX + key, compressEncryptBase64(value));
    try {
        String domain = Urls.getDomainName(context.getFullRequestURL(), this.domainSuffix);
        if (domain == null) {
            domain = context.getServerName();
        }
        cookie.setDomain(domain);
    } catch (final Exception e) {
        throw new TechnicalException(e);
    }
    cookie.setHttpOnly(true);
    cookie.setSecure(ContextHelper.isHttpsOrSecure(context));
    context.addResponseCookie(cookie);
}
Also used : Cookie(org.pac4j.core.context.Cookie) TechnicalException(org.pac4j.core.exception.TechnicalException) IOException(java.io.IOException) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 3 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 SignedJWT 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 4 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 SignedJWT 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 5 with TechnicalException

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

the class JwtAuthenticator method createJwtProfile.

@SuppressWarnings("unchecked")
protected void createJwtProfile(final TokenCredentials credentials, final JWT jwt) throws ParseException {
    final JWTClaimsSet claimSet = jwt.getJWTClaimsSet();
    String subject = claimSet.getSubject();
    if (subject == null) {
        throw new TechnicalException("JWT must contain a subject ('sub' claim)");
    }
    final Date expirationTime = claimSet.getExpirationTime();
    if (expirationTime != null) {
        final Date now = new Date();
        if (expirationTime.before(now)) {
            logger.error("The JWT is expired: no profile is built");
            return;
        }
    }
    final Map<String, Object> attributes = new HashMap<>(claimSet.getClaims());
    attributes.remove(JwtClaims.SUBJECT);
    final List<String> roles = (List<String>) attributes.get(JwtGenerator.INTERNAL_ROLES);
    attributes.remove(JwtGenerator.INTERNAL_ROLES);
    final List<String> permissions = (List<String>) attributes.get(JwtGenerator.INTERNAL_PERMISSIONS);
    attributes.remove(JwtGenerator.INTERNAL_PERMISSIONS);
    final CommonProfile profile = ProfileHelper.restoreOrBuildProfile(getProfileDefinition(), subject, attributes, null);
    if (roles != null) {
        profile.addRoles(roles);
    }
    if (permissions != null) {
        profile.addPermissions(permissions);
    }
    credentials.setUserProfile(profile);
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) HashMap(java.util.HashMap) CommonProfile(org.pac4j.core.profile.CommonProfile) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date)

Aggregations

TechnicalException (org.pac4j.core.exception.TechnicalException)54 IOException (java.io.IOException)16 JWT (com.nimbusds.jwt.JWT)6 SignedJWT (com.nimbusds.jwt.SignedJWT)4 HTTPRequest (com.nimbusds.oauth2.sdk.http.HTTPRequest)4 HTTPResponse (com.nimbusds.oauth2.sdk.http.HTTPResponse)4 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)4 HttpURLConnection (java.net.HttpURLConnection)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 OAuthException (com.github.scribejava.core.exceptions.OAuthException)3 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)3 ParseException (com.nimbusds.oauth2.sdk.ParseException)3 BearerAccessToken (com.nimbusds.oauth2.sdk.token.BearerAccessToken)3 BufferedWriter (java.io.BufferedWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3