OAuthResponseImpl.java
6.79 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* org.apache.commons.codec.binary.Base64
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.aam.client;
import com.adobe.cq.aam.client.spi.OAuthResponse;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.commons.codec.binary.Base64;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OAuthResponseImpl
implements OAuthResponse {
private static final int SIXTY_SECONDS = 60;
private static final long ONE_SECOND = 1000;
private static final Logger LOGGER = LoggerFactory.getLogger(OAuthResponseImpl.class);
private String basicAuth;
private String refreshToken;
private String accessToken;
private JSONObject jsonObject = new JSONObject();
private Calendar expires = Calendar.getInstance();
private boolean invalid = false;
public OAuthResponseImpl(String clientId, String clientSecret) {
this.setBasicAuth(clientId, clientSecret);
}
public OAuthResponseImpl(Node n, String clientId, String clientSecret, CryptoSupport cryptoSupport) throws RepositoryException, CryptoException {
this.setBasicAuth(clientId, clientSecret);
if (n.hasProperty("oauthAccessToken")) {
this.accessToken = this.unprotect(cryptoSupport, n.getProperty("oauthAccessToken").getString());
} else {
LOGGER.warn("OAuth Config node {} has no {}", (Object)n, (Object)"oauthAccessToken");
}
if (n.hasProperty("oauthRefreshToken")) {
this.refreshToken = this.unprotect(cryptoSupport, n.getProperty("oauthRefreshToken").getString());
} else {
LOGGER.warn("OAuth Config node {} has no {}", (Object)n, (Object)"oauthAccessToken");
this.refreshToken = null;
}
if (n.hasProperty("oauthExpiresEpoch")) {
this.expires.setTimeInMillis(n.getProperty("oauthExpiresEpoch").getLong());
} else {
LOGGER.warn("OAuth Config node {} has no {}", (Object)n, (Object)"oauthExpiresEpoch");
this.expires = Calendar.getInstance();
this.expires.setTimeInMillis(System.currentTimeMillis() - 1000);
}
}
public OAuthResponseImpl(String errorStatus) throws JSONException {
this.setErrorStatus(errorStatus);
this.invalid = true;
}
private void check() {
if (this.invalid) {
throw new IllegalStateException("Auth Token is invalid and may not be used.");
}
}
private void setBasicAuth(String clientId, String clientSecret) {
this.check();
Base64 b64 = new Base64();
try {
this.basicAuth = new String(b64.encode((clientId + ":" + clientSecret).getBytes("UTF-8")), "UTF-8");
}
catch (UnsupportedEncodingException e) {
this.basicAuth = null;
}
}
public String getBasicAuth() {
this.check();
return "Basic " + this.basicAuth;
}
public String getAccessToken() {
this.check();
return this.accessToken;
}
public String getRefreshToken() {
this.check();
return this.refreshToken;
}
public void update(JSONObject updateJsonObject, CryptoSupport cryptoSupport) throws JSONException, CryptoException {
this.check();
this.jsonObject = new JSONObject();
this.accessToken = updateJsonObject.getString("access_token");
this.jsonObject.put("access_token", (Object)this.protect(cryptoSupport, this.accessToken));
if (updateJsonObject.has("refresh_token")) {
this.refreshToken = updateJsonObject.getString("refresh_token");
this.jsonObject.put("refresh_token", (Object)this.protect(cryptoSupport, this.refreshToken));
} else {
this.refreshToken = null;
}
this.expires.setTimeInMillis(System.currentTimeMillis() + 1000 * (updateJsonObject.getLong("expires_in") - 60));
this.jsonObject.put("expires_in_date", (Object)this.expires.getTime());
this.jsonObject.put("expires_in_epoch", this.expires.getTimeInMillis());
this.jsonObject.put("expires_in", updateJsonObject.getLong("expires_in"));
LOGGER.debug("OAuth Update: Accesss Token {} Refresh Token {} expires {} ", new Object[]{this.accessToken, this.refreshToken, this.expires.getTime()});
LOGGER.debug("Update Json Object was {} ", (Object)updateJsonObject.toString(2));
}
public String getBearerAuth() {
this.check();
return "Bearer " + this.getAccessToken();
}
public boolean hasExpired() {
this.check();
return this.accessToken == null || this.expires.getTimeInMillis() < System.currentTimeMillis();
}
public void save(Node n, CryptoSupport cryptoSupport) throws RepositoryException, CryptoException {
this.check();
n.setProperty("oauthAccessToken", this.protect(cryptoSupport, this.accessToken));
if (this.refreshToken != null) {
n.setProperty("oauthRefreshToken", this.protect(cryptoSupport, this.refreshToken));
} else if (n.hasProperty("oauthRefreshToken")) {
n.getProperties("oauthRefreshToken").remove();
}
n.setProperty("oauthExpiresEpoch", this.expires.getTimeInMillis());
n.setProperty("oauthExpiresDate", this.expires);
}
@Override
public String toJson() {
return this.jsonObject.toString();
}
private String unprotect(CryptoSupport cryptoSupport, String protectedValue) throws CryptoException {
if (cryptoSupport.isProtected(protectedValue)) {
return cryptoSupport.unprotect(protectedValue);
}
return protectedValue;
}
private String protect(CryptoSupport cryptoSupport, String value) throws CryptoException {
if (value != null && !cryptoSupport.isProtected(value)) {
return cryptoSupport.protect(value);
}
return value;
}
public void setErrorStatus(String status) throws JSONException {
if (!this.jsonObject.has("status_codes")) {
this.jsonObject.put("status_codes", (Object)new JSONObject());
}
this.jsonObject.getJSONObject("status_codes").put(status, true);
}
}