Search in sources :

Example 11 with JWTCreationException

use of com.auth0.jwt.exceptions.JWTCreationException in project LogHub by fbacchella.

the class JwtToken method processRequest.

@Override
protected void processRequest(FullHttpRequest request, ChannelHandlerContext ctx) throws HttpRequestFailure {
    Principal p = ctx.channel().attr(AbstractNettyServer.PRINCIPALATTRIBUTE).get();
    try {
        String token = alg.getToken(p);
        ByteBuf content = Unpooled.copiedBuffer(token + "\r\n", CharsetUtil.UTF_8);
        writeResponse(ctx, request, content, content.readableBytes());
    } catch (JWTCreationException exception) {
        throw new HttpRequestFailure(HttpResponseStatus.SERVICE_UNAVAILABLE, "JWT creation failed", Collections.emptyMap());
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Principal(java.security.Principal) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException)

Example 12 with JWTCreationException

use of com.auth0.jwt.exceptions.JWTCreationException in project structr by structr.

the class JWTHelper method createTokensForUserWithSecret.

private static Map<String, String> createTokensForUserWithSecret(Principal user, Date accessTokenExpirationDate, Date refreshTokenExpirationDate, String instanceName) throws FrameworkException {
    final String secret = Settings.JWTSecret.getValue();
    final String jwtIssuer = Settings.JWTIssuer.getValue();
    try {
        final Algorithm alg = Algorithm.HMAC256(secret.getBytes(StandardCharsets.UTF_8));
        return createTokens(user, alg, accessTokenExpirationDate, refreshTokenExpirationDate, instanceName, jwtIssuer);
    } catch (JWTCreationException ex) {
        throw new FrameworkException(500, "The configured secret is too weak (must be at least 32 characters)");
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException)

Example 13 with JWTCreationException

use of com.auth0.jwt.exceptions.JWTCreationException in project dockstore by dockstore.

the class GitHubHelper method checkJWT.

/**
 * Refresh the JWT for GitHub apps
 * @param gitHubAppId
 * @param gitHubPrivateKeyFile
 */
public static void checkJWT(String gitHubAppId, String gitHubPrivateKeyFile) {
    RSAPrivateKey rsaPrivateKey = null;
    System.out.println("working dir=" + Paths.get("").toAbsolutePath().toString());
    try {
        String pemFileContent = FileUtils.readFileToString(new File(gitHubPrivateKeyFile), StandardCharsets.UTF_8);
        final PemReader.Section privateKey = PemReader.readFirstSectionAndClose(new StringReader(pemFileContent), "PRIVATE KEY");
        if (privateKey != null) {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getBase64DecodedBytes());
            rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        } else {
            LOG.error("No private key found in " + gitHubPrivateKeyFile);
        }
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException ex) {
        LOG.error(ex.getMessage(), ex);
    }
    if (rsaPrivateKey != null) {
        final int tenMinutes = 600000;
        try {
            Algorithm algorithm = Algorithm.RSA256(null, rsaPrivateKey);
            String jsonWebToken = JWT.create().withIssuer(gitHubAppId).withIssuedAt(new Date()).withExpiresAt(new Date(Calendar.getInstance().getTimeInMillis() + tenMinutes)).sign(algorithm);
            CacheConfigManager.setJsonWebToken(jsonWebToken);
        } catch (JWTCreationException ex) {
            LOG.error(ex.getMessage(), ex);
        }
    }
}
Also used : IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException) PemReader(com.google.api.client.util.PemReader) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) File(java.io.File) KeyFactory(java.security.KeyFactory)

Example 14 with JWTCreationException

use of com.auth0.jwt.exceptions.JWTCreationException in project toy by gmoon92.

the class JwtUtils method generate.

public String generate(User user) {
    try {
        ZonedDateTime today = ZonedDateTime.now();
        String token = JWT.create().withIssuer(apiVersion).withClaim("username", user.getUsername()).withClaim("role", user.getRole().name()).withIssuedAt(Date.from(today.toInstant())).withExpiresAt(Date.from(today.plusDays(DAY_OF_EXPIRATION).toInstant())).sign(algorithm);
        return String.format("%s %s", AuthenticationSchema.BEARER.getName(), token);
    } catch (JWTCreationException e) {
        throw new JWTCreationException("Invalid Signing configuration or Couldn't convert Claims.", e);
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException)

Aggregations

JWTCreationException (com.auth0.jwt.exceptions.JWTCreationException)11 Algorithm (com.auth0.jwt.algorithms.Algorithm)8 ZonedDateTime (java.time.ZonedDateTime)4 Date (java.util.Date)4 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Principal (java.security.Principal)2 HashMap (java.util.HashMap)2 JWTCreator (com.auth0.jwt.JWTCreator)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 PemReader (com.google.api.client.util.PemReader)1 JsonObject (com.google.gson.JsonObject)1 OAuthResponseException (com.salesforce.einsteinbot.sdk.exception.OAuthResponseException)1 ByteBuf (io.netty.buffer.ByteBuf)1 UnsupportedJWTSigningAlgorithmException (io.supertokens.jwt.exceptions.UnsupportedJWTSigningAlgorithmException)1 JWTSigningKeyInfo (io.supertokens.pluginInterface.jwt.JWTSigningKeyInfo)1 File (java.io.File)1 StringReader (java.io.StringReader)1 KeyFactory (java.security.KeyFactory)1