1
|
package eu.dnetlib.openaire.rest.security;
|
2
|
|
3
|
import javax.xml.bind.DatatypeConverter;
|
4
|
|
5
|
import io.jsonwebtoken.*;
|
6
|
|
7
|
import java.io.UnsupportedEncodingException;
|
8
|
import java.util.Date;
|
9
|
|
10
|
/**
|
11
|
* Created by kiatrop on 10/4/2017.
|
12
|
*/
|
13
|
public class JWTValidator {
|
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
|
System.out.println("Id: " + claims.get("userId"));
|
26
|
System.out.println("Subject: " + claims.getSubject());
|
27
|
System.out.println("Fullname: " + claims.get("fullname"));
|
28
|
System.out.println("Email: " + claims.get("email"));
|
29
|
System.out.println("Role: " + claims.get("role"));
|
30
|
System.out.println("Expiration: " + claims.getExpiration());
|
31
|
|
32
|
Jws jwtClaims = Jwts.parser().setSigningKey("my-very-secret").parseClaimsJws(token);
|
33
|
|
34
|
return true;
|
35
|
|
36
|
} catch (SignatureException e) {
|
37
|
System.out.println("Either calculating a signature or verifying an existing signature of a JWT failed");
|
38
|
return false;
|
39
|
} catch(PrematureJwtException e) {
|
40
|
System.out.println("A JWT was accepted before it is allowed to be accessed and must be rejected");
|
41
|
return false;
|
42
|
} catch(ClaimJwtException e){
|
43
|
System.out.println("After a validation of a JWT claim failed");
|
44
|
return false;
|
45
|
} catch(MalformedJwtException e){
|
46
|
System.out.println("A JWT was not correctly constructed and should be rejected");
|
47
|
return false;
|
48
|
} catch(UnsupportedJwtException e){
|
49
|
System.out.println("Receiving a JWT in a particular format/configuration that does not match the format expected by the application");
|
50
|
return false;
|
51
|
}
|
52
|
}
|
53
|
|
54
|
public static void main(String[] args) {
|
55
|
|
56
|
// valid (isValid("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBcmdpcm8iLCJmdWxsbmFtZSI6IkFyZ2lybyBLb2tvZ2lhbmFraSIsInVzZXJJZCI6IjgiLCJlbWFpbCI6ImFyZ2lyb0BnbWFpbC5jb20iLCJyb2xlIjoyLCJleHAiOjE0OTIwMDEyMzN9.u2xTTyjF6_n60yiz-Pfj72TQIaVNRgxt7Qe8w_AxoTBulKuiq6ldzZHDvgcef2jsA12TFRgfIyfXhsTj-04P5A"))
|
57
|
// not valid "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBcmdpcm8iLCJmdWxsbmFtZSI6IkFyZ2lybyBLb2tvZ2lhbmFraSIsInVzZXJJZCI6IjgiLCJlbWFpbCI6ImFyZ2lyb0BnbWFpbC5jb20iLCJyb2xlIjoyLCJleHAiOjE0OTE5ODk2Njh9.JUiFGEverBVbo3eXlV2nX265cwNTYH1EJGZT0IcGyY31WUDHiGmtnTY-NwZsy0R93irVhAzVhTlmZI_DSRQ"
|
58
|
if (isValid("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBcmdpcm8iLCJmdWxsbmFtZSI6IkFyZ2lybyBLb2tvZ2lhbmFraSIsInVzZXJJZCI6IjgiLCJlbWFpbCI6ImFyZ2lyb0BnbWFpbC5jb20iLCJyb2xlIjoyLCJleHAiOjE0OTIwMDc4ODl9.xGad45dRe_lwOJYuD_zZXHYWqeOQkXuRFWdBbkGDK2bMGRDdkcfLoEpUopm9OB2GVgkJHmFp0tbMkBSPSTBwhw"))
|
59
|
|
60
|
System.out.println("Valid");
|
61
|
else
|
62
|
System.out.println("Not Valid");
|
63
|
|
64
|
}
|
65
|
}
|