Revision 48312
Added by Katerina Iatropoulou almost 7 years ago
JWTValidator.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.rest.security; |
|
2 |
|
|
3 |
import io.jsonwebtoken.*; |
|
4 |
import org.apache.log4j.Logger; |
|
5 |
|
|
6 |
import javax.xml.bind.DatatypeConverter; |
|
7 |
|
|
8 |
/** |
|
9 |
* Created by kiatrop on 10/4/2017. |
|
10 |
*/ |
|
11 |
public class JWTValidator { |
|
12 |
|
|
13 |
private static final Logger logger = Logger.getLogger(JWTValidator.class); |
|
14 |
|
|
15 |
public static boolean isValid(String token) { |
|
16 |
|
|
17 |
//String subject = "my-very-secret"; |
|
18 |
try { |
|
19 |
|
|
20 |
//This line will throw an exception if it is not a signed JWS (as expected) |
|
21 |
Claims claims = Jwts.parser() |
|
22 |
.setSigningKey(DatatypeConverter.parseBase64Binary("my-very-secret")) |
|
23 |
.parseClaimsJws(token).getBody(); |
|
24 |
|
|
25 |
logger.debug("Id: " + claims.get("userId")); |
|
26 |
logger.debug("Subject: " + claims.getSubject()); |
|
27 |
logger.debug("Fullname: " + claims.get("fullname")); |
|
28 |
logger.debug("Email: " + claims.get("email")); |
|
29 |
logger.debug("Role: " + claims.get("role")); |
|
30 |
logger.debug("Expiration: " + claims.getExpiration()); |
|
31 |
|
|
32 |
return true; |
|
33 |
|
|
34 |
} catch (SignatureException e) { |
|
35 |
logger.error("Either calculating a signature or verifying an existing signature of a JWT failed", e); |
|
36 |
return false; |
|
37 |
} catch(PrematureJwtException e) { |
|
38 |
logger.error("A JWT was accepted before it is allowed to be accessed and must be rejected", e); |
|
39 |
return false; |
|
40 |
} catch(ClaimJwtException e){ |
|
41 |
logger.error("After a validation of a JWT claim failed", e); |
|
42 |
return false; |
|
43 |
} catch(MalformedJwtException e){ |
|
44 |
logger.error("A JWT was not correctly constructed and should be rejected", e); |
|
45 |
return false; |
|
46 |
} catch(UnsupportedJwtException e){ |
|
47 |
logger.error("Receiving a JWT in a particular format/configuration that does not match the format expected by the application", e); |
|
48 |
return false; |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
// public static void main(String[] args) { |
|
53 |
// |
|
54 |
// // valid (isValid("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBcmdpcm8iLCJmdWxsbmFtZSI6IkFyZ2lybyBLb2tvZ2lhbmFraSIsInVzZXJJZCI6IjgiLCJlbWFpbCI6ImFyZ2lyb0BnbWFpbC5jb20iLCJyb2xlIjoyLCJleHAiOjE0OTIwMDEyMzN9.u2xTTyjF6_n60yiz-Pfj72TQIaVNRgxt7Qe8w_AxoTBulKuiq6ldzZHDvgcef2jsA12TFRgfIyfXhsTj-04P5A")) |
|
55 |
// // not valid "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBcmdpcm8iLCJmdWxsbmFtZSI6IkFyZ2lybyBLb2tvZ2lhbmFraSIsInVzZXJJZCI6IjgiLCJlbWFpbCI6ImFyZ2lyb0BnbWFpbC5jb20iLCJyb2xlIjoyLCJleHAiOjE0OTE5ODk2Njh9.JUiFGEverBVbo3eXlV2nX265cwNTYH1EJGZT0IcGyY31WUDHiGmtnTY-NwZsy0R93irVhAzVhTlmZI_DSRQ" |
|
56 |
// //if (isValid("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzYmEiLCJmdWxsbmFtZSI6InNvZmlhICIsInVzZXJJZCI6IjYiLCJlbWFpbCI6InNvZmllX21wbEBob3RtYWlsLmNvbSIsInJvbGUiOjEsImV4cCI6MTQ5Mjc3ODY2M30.LcgYcBuI0V8tsZZR_Pnnb5Kk0_GfblxNC6FH1CHT2q7u4j9hcETb1EYLIKPZdkErXJ_MSC1jHNUiS5qCzEQQ2w")) |
|
57 |
// |
|
58 |
// if(isValid("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBcmdpcm8iLCJmdWxsbmFtZSI6IkFyZ2lybyBLb2tvZ2lhbmFraSIsInVzZXJJZCI6IjgiLCJlbWFpbCI6ImFyZ2lyb0BnbWFpbC5jb20iLCJyb2xlIjoyLCJleHAiOjE0OTI3ODY4MTl9.F-cFGmSvZLAF70PCz9PE6jOwSg6aAiUW2lklkS-eKCxkjwbqPJLWlNWP10Czg4j1_iKDTvHR7wbhbRyJPoqvNQ")) |
|
59 |
// System.out.println("Valid"); |
|
60 |
// else |
|
61 |
// System.out.println("Not Valid"); |
|
62 |
// |
|
63 |
// } |
|
64 |
|
|
65 |
} |
Also available in: Unified diff
MERGE newClaimsAPI 42190:48301