Search in sources :

Example 1 with JwtException

use of io.jsonwebtoken.JwtException in project jjwt by jwtk.

the class EllipticCurveProvider method transcodeSignatureToConcat.

/**
     * Transcodes the JCA ASN.1/DER-encoded signature into the concatenated
     * R + S format expected by ECDSA JWS.
     *
     * @param derSignature The ASN1./DER-encoded. Must not be {@code null}.
     * @param outputLength The expected length of the ECDSA JWS signature.
     *
     * @return The ECDSA JWS encoded signature.
     *
     * @throws JwtException If the ASN.1/DER signature format is invalid.
     */
public static byte[] transcodeSignatureToConcat(final byte[] derSignature, int outputLength) throws JwtException {
    if (derSignature.length < 8 || derSignature[0] != 48) {
        throw new JwtException("Invalid ECDSA signature format");
    }
    int offset;
    if (derSignature[1] > 0) {
        offset = 2;
    } else if (derSignature[1] == (byte) 0x81) {
        offset = 3;
    } else {
        throw new JwtException("Invalid ECDSA signature format");
    }
    byte rLength = derSignature[offset + 1];
    int i = rLength;
    while ((i > 0) && (derSignature[(offset + 2 + rLength) - i] == 0)) i--;
    byte sLength = derSignature[offset + 2 + rLength + 1];
    int j = sLength;
    while ((j > 0) && (derSignature[(offset + 2 + rLength + 2 + sLength) - j] == 0)) j--;
    int rawLen = Math.max(i, j);
    rawLen = Math.max(rawLen, outputLength / 2);
    if ((derSignature[offset - 1] & 0xff) != derSignature.length - offset || (derSignature[offset - 1] & 0xff) != 2 + rLength + 2 + sLength || derSignature[offset] != 2 || derSignature[offset + 2 + rLength] != 2) {
        throw new JwtException("Invalid ECDSA signature format");
    }
    final byte[] concatSignature = new byte[2 * rawLen];
    System.arraycopy(derSignature, (offset + 2 + rLength) - i, concatSignature, rawLen - i, i);
    System.arraycopy(derSignature, (offset + 2 + rLength + 2 + sLength) - j, concatSignature, 2 * rawLen - j, j);
    return concatSignature;
}
Also used : JwtException(io.jsonwebtoken.JwtException)

Example 2 with JwtException

use of io.jsonwebtoken.JwtException in project sic by belluccifranco.

the class JwtInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    if (request.getMethod().equals("OPTIONS")) {
        return true;
    }
    final String authHeader = request.getHeader("Authorization");
    if (authHeader == null || !authHeader.startsWith("Bearer ")) {
        throw new UnauthorizedException(ResourceBundle.getBundle("Mensajes").getString("mensaje_error_token_vacio_invalido"));
    }
    // The part after "Bearer "
    final String token = authHeader.substring(7);
    Claims claims;
    try {
        claims = Jwts.parser().setSigningKey(secretkey).parseClaimsJws(token).getBody();
        request.setAttribute("claims", claims);
    } catch (JwtException ex) {
        throw new UnauthorizedException(ResourceBundle.getBundle("Mensajes").getString("mensaje_error_token_vacio_invalido"), ex);
    }
    long idUsuario = (int) claims.get("idUsuario");
    Usuario usuario = usuarioService.getUsuarioPorId(idUsuario);
    if (null == usuario || null == token) {
        throw new UnauthorizedException(ResourceBundle.getBundle("Mensajes").getString("mensaje_error_token_vacio_invalido"));
    } else if (!token.equalsIgnoreCase(usuario.getToken())) {
        throw new UnauthorizedException(ResourceBundle.getBundle("Mensajes").getString("mensaje_error_token_invalido"));
    }
    return true;
}
Also used : Claims(io.jsonwebtoken.Claims) Usuario(sic.modelo.Usuario) UnauthorizedException(sic.controller.UnauthorizedException) JwtException(io.jsonwebtoken.JwtException)

Example 3 with JwtException

use of io.jsonwebtoken.JwtException in project sic by belluccifranco.

the class AuthController method logout.

@PutMapping("/logout")
public void logout(HttpServletRequest request) {
    final String authHeader = request.getHeader("Authorization");
    if (authHeader == null || !authHeader.startsWith("Bearer ")) {
        throw new UnauthorizedException(ResourceBundle.getBundle("Mensajes").getString("mensaje_error_token_vacio_invalido"));
    }
    // The part after "Bearer "
    final String token = authHeader.substring(7);
    Claims claims;
    try {
        claims = Jwts.parser().setSigningKey(secretkey).parseClaimsJws(token).getBody();
        request.setAttribute("claims", claims);
    } catch (JwtException ex) {
        throw new UnauthorizedException(ResourceBundle.getBundle("Mensajes").getString("mensaje_error_token_vacio_invalido"), ex);
    }
    long idUsuario = (int) claims.get("idUsuario");
    Usuario usuario = usuarioService.getUsuarioPorId(idUsuario);
    usuario.setToken("");
    usuarioService.actualizar(usuario);
}
Also used : Claims(io.jsonwebtoken.Claims) Usuario(sic.modelo.Usuario) JwtException(io.jsonwebtoken.JwtException) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

JwtException (io.jsonwebtoken.JwtException)3 Claims (io.jsonwebtoken.Claims)2 Usuario (sic.modelo.Usuario)2 PutMapping (org.springframework.web.bind.annotation.PutMapping)1 UnauthorizedException (sic.controller.UnauthorizedException)1