JwsBuilderImpl.java
5.88 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* org.apache.oltu.commons.json.CustomizableEntity
* org.apache.oltu.jose.jws.JWS
* org.apache.oltu.jose.jws.JWS$Builder
* org.apache.oltu.jose.jws.io.JWSWriter
* org.apache.oltu.jose.jws.signature.SignatureMethod
* org.apache.oltu.jose.jws.signature.SigningKey
* org.apache.oltu.oauth2.jwt.ClaimsSet
* org.apache.oltu.oauth2.jwt.JWT
* org.apache.oltu.oauth2.jwt.JWT$Builder
* org.apache.oltu.oauth2.jwt.io.JWTClaimsSetWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.oauth.jwt.impl;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.adobe.granite.oauth.jwt.JwsBuilder;
import com.adobe.granite.oauth.jwt.impl.HMACSignatureMethodsImpl;
import com.adobe.granite.oauth.jwt.impl.PrivateKey;
import com.adobe.granite.oauth.jwt.impl.RSASignatureMethodImpl;
import com.adobe.granite.oauth.jwt.impl.SymmetricKey;
import java.security.Key;
import javax.crypto.spec.SecretKeySpec;
import org.apache.oltu.commons.json.CustomizableEntity;
import org.apache.oltu.jose.jws.JWS;
import org.apache.oltu.jose.jws.io.JWSWriter;
import org.apache.oltu.jose.jws.signature.SignatureMethod;
import org.apache.oltu.jose.jws.signature.SigningKey;
import org.apache.oltu.oauth2.jwt.ClaimsSet;
import org.apache.oltu.oauth2.jwt.JWT;
import org.apache.oltu.oauth2.jwt.io.JWTClaimsSetWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JwsBuilderImpl
implements JwsBuilder {
private final Logger logger = LoggerFactory.getLogger(JwsBuilderImpl.class);
private static final String TYPE = "JWT";
private static final String SCOPE = "scope";
private static final long DEFAULT_LEEWAY = 20;
private final JWT.Builder jwtBuilder = new JWT.Builder();
private CryptoSupport cryptoSupport;
private long expiresIn;
private long iat = -1;
private SignatureMethod signatureMethod;
private SigningKey signingKey;
public JwsBuilderImpl(String algorithm, Key signingKey, CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
this.signingKey = this.getSigningKey(algorithm, signingKey);
this.signatureMethod = this.getSignatureMethod(algorithm);
}
public String build() throws CryptoException {
try {
this.setTimes();
JWT jwt = this.jwtBuilder.build();
String payload = new JWTClaimsSetWriter().write((CustomizableEntity)jwt.getClaimsSet());
JWS jws = new JWS.Builder().setType("JWT").setPayload(payload).sign(this.signatureMethod, this.signingKey).build();
return new JWSWriter().write((Object)jws);
}
catch (RuntimeException e) {
throw new CryptoException(e.getMessage());
}
}
public JwsBuilder setIssuer(String iss) {
this.jwtBuilder.setClaimsSetIssuer(iss);
return this;
}
public JwsBuilder setExpiresIn(long expiresIn) {
this.expiresIn = expiresIn;
return this;
}
public JwsBuilder setSubject(String sub) {
this.jwtBuilder.setClaimsSetSubject(sub);
return this;
}
public JwsBuilder setAudience(String aud) {
this.jwtBuilder.setClaimsSetAudience(aud);
return this;
}
public JwsBuilder setScope(String scope) {
this.jwtBuilder.setClaimsSetCustomField("scope", (Object)scope);
return this;
}
public JwsBuilder setIssuedAt(long iat) {
this.iat = iat;
return this;
}
public JwsBuilder setCustomClaimsSetField(String key, Object value) {
this.jwtBuilder.setClaimsSetCustomField(key, value);
return this;
}
private void setTimes() {
long currentTime = System.currentTimeMillis() / 1000;
if (this.iat == -1) {
this.logger.debug("iat not set, using current time");
this.iat = currentTime;
} else if (this.iat > currentTime + 20) {
this.logger.info("the set iat time is too far away in the future, using current time");
this.iat = currentTime;
}
this.jwtBuilder.setClaimsSetIssuedAt(this.iat);
this.jwtBuilder.setClaimsSetExpirationTime(this.iat + this.expiresIn);
}
private SignatureMethod getSignatureMethod(String algorithm) {
Object signatureMethod2;
Object signatureMethod2;
if ("HS256".equals(algorithm)) {
signatureMethod2 = new HMACSignatureMethodsImpl(this.cryptoSupport);
} else if ("RS256".equals(algorithm) || "RS384".equals(algorithm) || "RS512".equals(algorithm)) {
signatureMethod2 = new RSASignatureMethodImpl(this.cryptoSupport, algorithm);
} else {
throw new UnsupportedOperationException("algorithm " + algorithm + " not supported");
}
return signatureMethod2;
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
private SigningKey getSigningKey(String algorithm, Key key) {
if ("HS256".equals(algorithm)) {
if (key == null) {
return new SymmetricKey(null);
}
if (!(key instanceof SecretKeySpec)) throw new IllegalArgumentException("The given algorithm " + algorithm + " is not compatible with the given key " + key);
return new SymmetricKey(key.getEncoded());
}
if (!"RS256".equals(algorithm) && !"RS384".equals(algorithm)) {
if (!"RS512".equals(algorithm)) throw new IllegalArgumentException("Invalid algorithm " + algorithm + " and/or key " + key);
}
if (!(key instanceof java.security.PrivateKey)) throw new IllegalArgumentException("The given algorithm " + algorithm + " is not compatible with the given key " + key);
return new PrivateKey((java.security.PrivateKey)key);
}
}