Search in sources :

Example 6 with JWTCreationException

use of com.auth0.jwt.exceptions.JWTCreationException in project litemall by linlinjava.

the class JwtHelper method createToken.

public String createToken(Integer userId) {
    try {
        Algorithm algorithm = Algorithm.HMAC256(SECRET);
        Map<String, Object> map = new HashMap<String, Object>();
        Date nowDate = new Date();
        // 过期时间:2小时
        Date expireDate = getAfterDate(nowDate, 0, 0, 0, 2, 0, 0);
        map.put("alg", "HS256");
        map.put("typ", "JWT");
        String token = JWT.create().withHeader(map).withClaim("userId", userId).withIssuer(ISSUSER).withSubject(SUBJECT).withAudience(AUDIENCE).withIssuedAt(nowDate).withExpiresAt(expireDate).sign(algorithm);
        return token;
    } catch (JWTCreationException exception) {
        exception.printStackTrace();
    }
    return null;
}
Also used : HashMap(java.util.HashMap) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException)

Example 7 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)

Example 8 with JWTCreationException

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

the class JwtUtil 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)

Example 9 with JWTCreationException

use of com.auth0.jwt.exceptions.JWTCreationException in project framework by galasa-dev.

the class Authenticate method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Gson gson = new Gson();
    Principal principal = req.getUserPrincipal();
    if (principal == null) {
        // TODO check that it was a basic auth principal to prevent JWT reauthenticating
        resp.setStatus(401);
        // *** Ability to set the realm
        resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
        // NOSONAR //TODO catch this as SQ says
        resp.getWriter().write("Requires authentication");
        return;
    }
    if (req.isUserInRole("admin")) {
        String jwt;
        try {
            jwt = createJWT(principal.getName(), "admin", FOUR_HOURS_EXPIRE);
        } catch (JWTCreationException e) {
            resp.setStatus(500);
            // *** Ability to set the realm
            resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
            // NOSONAR //TODO catch this as SQ says
            resp.getWriter().write("Token could not be generated");
            return;
        }
        AuthJson auth = new AuthJson();
        auth.cps = jwt;
        auth.dss = jwt;
        auth.ras = jwt;
        String json = gson.toJson(auth);
        resp.setContentType("application/json");
        try {
            resp.getWriter().write(json);
        } catch (IOException e) {
            resp.setStatus(500);
            // *** Ability to set the realm
            resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
            // NOSONAR //TODO catch this as SQ says
            resp.getWriter().write("Failed to create json");
            return;
        }
        return;
    }
    if (req.isUserInRole("user")) {
        String jwt;
        try {
            jwt = createJWT(principal.getName(), "user", FOUR_HOURS_EXPIRE);
        } catch (JWTCreationException e) {
            resp.setStatus(500);
            // *** Ability to set the realm
            resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
            // NOSONAR //TODO catch this as SQ says
            resp.getWriter().write("Token could not be generated");
            return;
        }
        AuthJson auth = new AuthJson();
        auth.cps = jwt;
        auth.dss = jwt;
        auth.ras = jwt;
        String json = gson.toJson(auth);
        resp.setContentType("application/json");
        try {
            resp.getWriter().write(json);
        } catch (IOException e) {
            resp.setStatus(500);
            // *** Ability to set the realm
            resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
            // NOSONAR //TODO catch this as SQ says
            resp.getWriter().write("Failed to create json");
            return;
        }
        return;
    }
    resp.setStatus(401);
    // *** Ability to set the realm
    resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
    // NOSONAR
    resp.getWriter().write("Does not have the 'user' role");
}
Also used : Gson(com.google.gson.Gson) IOException(java.io.IOException) Principal(java.security.Principal) JWTCreationException(com.auth0.jwt.exceptions.JWTCreationException)

Example 10 with JWTCreationException

use of com.auth0.jwt.exceptions.JWTCreationException in project framework by galasa-dev.

the class Authenticate method createJWT.

public String createJWT(String subject, String role, long expireDuration) throws JWTCreationException {
    Algorithm algorithm = Algorithm.HMAC256(this.configurationProperties.get(SECRET_KEY).toString());
    long time = System.currentTimeMillis();
    Date dateNow = new Date(time);
    Date dateExpire = new Date(time + expireDuration);
    String token = JWT.create().withIssuer("galasa").withIssuedAt(dateNow).withSubject(subject).withClaim("role", role).withExpiresAt(dateExpire).sign(algorithm);
    return token;
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

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