-
Notifications
You must be signed in to change notification settings - Fork 195
/
Token.java
175 lines (155 loc) · 4.8 KB
/
Token.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.auth;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Calendar;
import java.util.TimeZone;
import javax.json.Json;
import javax.json.JsonObject;
/**
* JSON Token.
*
* <p>
* All implementations of this interface must be immutable and thread-safe.
*
* @since 1.4
*/
public interface Token {
/**
* JSON output.
*
* @return The Token in JSON notation.
*/
JsonObject json();
/**
* Base64 encoded JSON output.
*
* @return The Token in JSON notation, Base64-encoded.
*/
byte[] encoded();
/**
* JSON Object Signing and Encryption Header.
* @since 1.4
*/
final class Jose implements Token {
/**
* The header short for algorithm.
*/
public static final String ALGORITHM = "alg";
/**
* The header short for token type.
*/
public static final String TYP = "typ";
/**
* JOSE object.
*/
private final JsonObject joseo;
/**
* JSON Object Signing and Encryption Header.
* @param bitlength Of encryption bits.
*/
public Jose(final int bitlength) {
this.joseo = Json.createObjectBuilder()
.add(Jose.ALGORITHM, String.format("HS%s", bitlength))
.add(Jose.TYP, "JWT")
.build();
}
@Override
public JsonObject json() {
return this.joseo;
}
@Override
public byte[] encoded() {
return Base64.getEncoder().encode(
this.joseo.toString().getBytes(Charset.defaultCharset())
);
}
}
/**
* JSON Web Token.
* @since 1.4
*/
@SuppressWarnings
(
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
)
final class Jwt implements Token {
/**
* The header short for subject.
*/
public static final String SUBJECT = "sub";
/**
* The header short for issuing time.
*/
public static final String ISSUED = "iat";
/**
* The header short for expiration.
*/
public static final String EXPIRATION = "exp";
/**
* The header short for expiration.
*/
private static final String ISOFORMAT = "%tFT%<tRZ";
/**
* JWT object.
*/
private final JsonObject jwto;
/**
* Time of lifespan start.
*/
private final Calendar now;
/**
* Time of lifespan end.
*/
private final Calendar exp;
/**
* JSON Web Token.
* @param idt Identity
* @param age Lifetime of token.
*/
public Jwt(final Identity idt, final long age) {
this.now = Calendar.getInstance(TimeZone.getTimeZone("Z"));
this.exp = Calendar.getInstance(TimeZone.getTimeZone("Z"));
// @checkstyle MagicNumber (1 line)
this.exp.setTimeInMillis(this.now.getTimeInMillis() + (age * 1000));
this.jwto = Json.createObjectBuilder()
.add(Jwt.ISSUED, String.format(Jwt.ISOFORMAT, this.now))
.add(Jwt.EXPIRATION, String.format(Jwt.ISOFORMAT, this.exp))
.add(Jwt.SUBJECT, idt.urn())
.build();
}
@Override
public JsonObject json() {
return this.jwto;
}
@Override
public byte[] encoded() {
return Base64.getEncoder().encode(
this.jwto.toString().getBytes(Charset.defaultCharset())
);
}
}
}