|
1 | 1 | package yagura.model; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | | -import java.nio.charset.StandardCharsets; |
5 | | -import java.util.Base64; |
6 | | -import java.util.regex.Matcher; |
7 | | -import java.util.regex.Pattern; |
8 | 4 | import yagura.external.JsonUtil; |
9 | 5 |
|
10 | 6 | /** |
11 | 7 | * |
12 | 8 | * @author isayan |
13 | 9 | */ |
14 | | -public class JWTObject { |
| 10 | +public class JWTObject extends JWTToken { |
15 | 11 |
|
16 | | -private final static Pattern PTN_JWT = Pattern.compile("(e(?:[0-9a-zA-Z_-]){10,})\\.(e(?:[0-9a-zA-Z_-]){2,})\\.((?:[0-9a-zA-Z_-]){20,})"); |
17 | | - |
18 | | -public static boolean isJWTFormat(String value) { |
19 | | - Matcher m = PTN_JWT.matcher(value); |
20 | | - if (m.matches()) { |
21 | | - return true; |
22 | | - } |
23 | | - return false; |
24 | | -} |
25 | | - |
26 | | -public static boolean containsJWTFormat(String value) { |
27 | | - Matcher m = PTN_JWT.matcher(value); |
28 | | - if (m.find()) { |
29 | | - return true; |
30 | | - } |
31 | | - return false; |
32 | | -} |
33 | | - |
34 | | -public static String findJWTValue(String value) { |
35 | | - Matcher m = PTN_JWT.matcher(value); |
36 | | - if (m.find()) { |
37 | | - return m.group(0); |
38 | | - } |
39 | | - return null; |
40 | | -} |
41 | | - |
42 | | - |
43 | | -public static JWTObject parseJWTObject(String value, boolean matches) { |
44 | | - JWTObject jwt = new JWTObject(); |
45 | | - Matcher m = PTN_JWT.matcher(value); |
46 | | - boolean find = false; |
47 | | - if (matches) |
48 | | - find = m.matches(); |
49 | | - else |
50 | | - find = m.find(); |
51 | | - |
52 | | - if (find) { |
53 | | - String header = m.group(1); |
54 | | - String payload = m.group(2); |
55 | | - String signature = m.group(3); |
56 | | - JsonUtil.parse(decodeB64(header)); |
57 | | - JsonUtil.parse(decodeB64(payload)); |
58 | | - decodeB64(signature); |
59 | | - jwt.header = header; |
60 | | - jwt.payload = payload; |
61 | | - jwt.signature = signature; |
62 | | - } |
63 | | - return jwt; |
64 | | -} |
65 | | - |
66 | | - private String header; |
67 | | - private String payload; |
68 | | - private String signature; |
69 | | - |
70 | | - private static byte [] decodeB64Byte(String value) { |
71 | | - value = value.replace('-', '+'); |
72 | | - value = value.replace('_', '/'); |
73 | | - return Base64.getDecoder().decode(value); |
74 | | - } |
75 | | - |
76 | | - private static String decodeB64(String src) { |
77 | | - return new String(decodeB64Byte(src), StandardCharsets.UTF_8); |
78 | | - } |
79 | | - |
80 | | - private static String encodeB64Byte(byte [] src) { |
81 | | - byte[] encoded = Base64.getEncoder().withoutPadding().encode(src); |
82 | | - String value = new String(encoded, StandardCharsets.US_ASCII); |
83 | | - value = value.replace('+', '-'); |
84 | | - value = value.replace('/', '_'); |
85 | | - return value; |
| 12 | + public JWTObject(JWTToken token) { |
| 13 | + super(token); |
86 | 14 | } |
87 | 15 |
|
88 | | - private static String encodeB64(String src) { |
89 | | - return encodeB64Byte(src.getBytes(StandardCharsets.UTF_8)); |
90 | | - } |
91 | | - |
92 | | - /** |
93 | | - * @return the header |
94 | | - */ |
95 | | - public String getHeader() { |
96 | | - return header; |
97 | | - } |
98 | | - |
99 | | - /** |
| 16 | + /** |
100 | 17 | * @return the header |
101 | 18 | */ |
102 | 19 | public String getHeaderJSON(boolean pretty) { |
103 | 20 | try { |
104 | | - return JsonUtil.prettyJSON(decodeB64(header), pretty); |
| 21 | + return JsonUtil.prettyJSON(decodeB64(this.getHeader()), pretty); |
105 | 22 | } catch (IOException ex) { |
106 | 23 | return null; |
107 | 24 | } |
108 | 25 | } |
109 | | - |
110 | | - /** |
111 | | - * @return the payload |
112 | | - */ |
113 | | - public String getPayload() { |
114 | | - return payload; |
115 | | - } |
116 | 26 |
|
117 | 27 | /** |
118 | 28 | * @return the payload |
119 | 29 | */ |
120 | 30 | public String getPayloadJSON(boolean pretty) { |
121 | 31 | try { |
122 | | - return JsonUtil.prettyJSON(decodeB64(payload), pretty); |
| 32 | + return JsonUtil.prettyJSON(decodeB64(this.getPayload()), pretty); |
123 | 33 | } catch (IOException ex) { |
124 | 34 | return null; |
125 | 35 | } |
126 | 36 | } |
127 | 37 |
|
128 | | - /** |
129 | | - * @return the signature |
130 | | - */ |
131 | | - public String getSignature() { |
132 | | - return signature; |
133 | | - } |
134 | | - |
135 | 38 | /** |
136 | 39 | * @return the signature |
137 | 40 | */ |
138 | 41 | public byte [] getSignatureByte() { |
139 | | - return decodeB64Byte(signature); |
| 42 | + return decodeB64Byte(this.getSignature()); |
140 | 43 | } |
141 | 44 |
|
142 | 45 | } |
0 commit comments