-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAuthenticationServiceImpl.java
More file actions
302 lines (263 loc) · 11.1 KB
/
AuthenticationServiceImpl.java
File metadata and controls
302 lines (263 loc) · 11.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.descope.sdk.auth.impl;
import static com.descope.literals.AppConstants.PERMISSIONS_CLAIM_KEY;
import static com.descope.literals.AppConstants.ROLES_CLAIM_KEY;
import static com.descope.literals.Routes.AuthEndPoints.EXCHANGE_ACCESS_KEY_LINK;
import static com.descope.literals.Routes.AuthEndPoints.HISTORY_LINK;
import static com.descope.literals.Routes.AuthEndPoints.LOG_OUT_ALL_LINK;
import static com.descope.literals.Routes.AuthEndPoints.LOG_OUT_LINK;
import static com.descope.literals.Routes.AuthEndPoints.ME_LINK;
import static com.descope.literals.Routes.AuthEndPoints.TENANT_SELECT_LINK;
import static com.descope.utils.CollectionUtils.mapOf;
import com.descope.exception.DescopeException;
import com.descope.exception.ServerCommonException;
import com.descope.model.auth.AccessKeyLoginOptions;
import com.descope.model.auth.AuthenticationInfo;
import com.descope.model.auth.ExchangeTokenRequest;
import com.descope.model.client.Client;
import com.descope.model.jwt.Token;
import com.descope.model.jwt.response.JWTResponse;
import com.descope.model.user.response.UserHistoryResponse;
import com.descope.model.user.response.UserResponse;
import com.descope.proxy.ApiProxy;
import com.descope.utils.DPoPUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
class AuthenticationServiceImpl extends AuthenticationsBase {
AuthenticationServiceImpl(Client client) {
super(client);
}
@Override
public Token validateSessionWithToken(String sessionToken) throws DescopeException {
if (StringUtils.isBlank(sessionToken)) {
throw ServerCommonException.invalidArgument("sessionToken");
}
return validateJWT(sessionToken);
}
@Override
public Token refreshSessionWithToken(String refreshToken) throws DescopeException {
if (StringUtils.isBlank(refreshToken)) {
throw ServerCommonException.missingArguments("refresh token");
}
return refreshSession(refreshToken).getToken();
}
@Override
public AuthenticationInfo refreshSessionWithTokenAuthenticationInfo(String refreshToken) throws DescopeException {
if (StringUtils.isBlank(refreshToken)) {
throw ServerCommonException.missingArguments("refresh token");
}
AuthenticationInfo authInfo = refreshSession(refreshToken);
if (authInfo.getRefreshToken() == null) { // refresh token is not returned because jwt rotation is not enabled
authInfo.setRefreshToken(validateAndCreateToken(refreshToken));
}
return authInfo;
}
@Override
public Token validateAndRefreshSessionWithTokens(String sessionToken, String refreshToken)
throws DescopeException {
if (StringUtils.isAllBlank(sessionToken, refreshToken)) {
throw ServerCommonException.missingArguments("Both sessionToken and refreshToken are empty");
} else if (StringUtils.isNotBlank(sessionToken)) {
try {
return validateSessionWithToken(sessionToken);
} catch (Exception e) {
if (StringUtils.isNotBlank(refreshToken)) {
return refreshSessionWithToken(refreshToken);
}
throw e;
}
} else {
return refreshSessionWithToken(refreshToken);
}
}
@Override
public AuthenticationInfo validateAndRefreshSessionWithTokensAuthenticationInfo(
String sessionToken, String refreshToken) throws DescopeException {
if (StringUtils.isAllBlank(sessionToken, refreshToken)) {
throw ServerCommonException.missingArguments("Both sessionToken and refreshToken are empty");
} else if (StringUtils.isNotBlank(sessionToken)) {
try {
Token refresh = validateAndCreateToken(refreshToken);
return new AuthenticationInfo(validateSessionWithToken(sessionToken), refresh, null, null, null);
} catch (Exception e) {
if (StringUtils.isNotBlank(refreshToken)) {
return refreshSessionWithTokenAuthenticationInfo(refreshToken);
}
throw e;
}
} else {
return refreshSessionWithTokenAuthenticationInfo(refreshToken);
}
}
@Override
public Token exchangeAccessKey(String accessKey) throws DescopeException {
return exchangeAccessKey(accessKey, null);
}
@Override
public Token exchangeAccessKey(String accessKey, AccessKeyLoginOptions loginOptions) throws DescopeException {
if (StringUtils.isBlank(accessKey)) {
throw ServerCommonException.invalidArgument("accessKey");
}
ApiProxy apiProxy = getApiProxy(accessKey);
URI exchangeAccessKeyLinkURL = composeExchangeAccessKeyLinkURL();
JWTResponse jwtResponse = apiProxy.post(exchangeAccessKeyLinkURL,
mapOf("loginOptions", loginOptions), JWTResponse.class);
AuthenticationInfo authenticationInfo = getAuthenticationInfo(jwtResponse);
return authenticationInfo.getToken();
}
@Override
public boolean validatePermissions(Token token, List<String> permissions)
throws DescopeException {
return validatePermissions(token, "", permissions);
}
@Override
public boolean validatePermissions(Token token, String tenant, List<String> permissions)
throws DescopeException {
if (StringUtils.isNotBlank(tenant) && !isTenantAssociated(token, tenant)) {
return false;
}
List<String> grantedPermissions = getPermissions(token, tenant);
return CollectionUtils.isSubCollection(permissions, grantedPermissions);
}
@Override
public List<String> getMatchedPermissions(Token token, List<String> permissions) throws DescopeException {
return getMatchedPermissions(token, "", permissions);
}
@Override
public List<String> getMatchedPermissions(Token token, String tenant, List<String> permissions)
throws DescopeException {
if (CollectionUtils.isEmpty(permissions) || StringUtils.isNotBlank(tenant) && !isTenantAssociated(token, tenant)) {
return Collections.emptyList();
}
List<String> grantedPermissions = getPermissions(token, tenant);
Collection<String> intersection = CollectionUtils.intersection(permissions, grantedPermissions);
return new ArrayList<>(intersection);
}
@Override
public boolean validateRoles(Token token, List<String> roles) throws DescopeException {
return validateRoles(token, "", roles);
}
@Override
public boolean validateRoles(Token token, String tenant, List<String> roles)
throws DescopeException {
if (StringUtils.isNotBlank(tenant) && !isTenantAssociated(token, tenant)) {
return false;
}
List<String> grantedRoles = getRoles(token, tenant);
return CollectionUtils.isSubCollection(roles, grantedRoles);
}
@Override
public List<String> getMatchedRoles(Token token, List<String> roles) throws DescopeException {
return getMatchedRoles(token, "", roles);
}
@Override
public List<String> getMatchedRoles(Token token, String tenant, List<String> roles) throws DescopeException {
if (CollectionUtils.isEmpty(roles) || StringUtils.isNotBlank(tenant) && !isTenantAssociated(token, tenant)) {
return Collections.emptyList();
}
List<String> grantedRoles = getRoles(token, tenant);
Collection<String> intersection = CollectionUtils.intersection(roles, grantedRoles);
return new ArrayList<>(intersection);
}
@Override
public List<String> getRoles(Token token, String tenant) throws DescopeException {
return getAuthorizationClaimItems(token, tenant, ROLES_CLAIM_KEY);
}
@Override
public List<String> getRoles(Token token) throws DescopeException {
return getAuthorizationClaimItems(token, "", ROLES_CLAIM_KEY);
}
@Override
public List<String> getPermissions(Token token, String tenant) throws DescopeException {
return getAuthorizationClaimItems(token, tenant, PERMISSIONS_CLAIM_KEY);
}
@Override
public List<String> getPermissions(Token token) throws DescopeException {
return getAuthorizationClaimItems(token, "", PERMISSIONS_CLAIM_KEY);
}
@Override
public void logout(String refreshToken) throws DescopeException {
if (StringUtils.isBlank(refreshToken)) {
throw ServerCommonException.missingArguments("refresh token");
}
ApiProxy apiProxy = getApiProxy(refreshToken);
URI logOutURL = composeLogOutLinkURL();
apiProxy.post(logOutURL, null, JWTResponse.class);
}
@Override
public void logoutAll(String refreshToken) throws DescopeException {
if (StringUtils.isBlank(refreshToken)) {
throw ServerCommonException.missingArguments("refresh token");
}
ApiProxy apiProxy = getApiProxy(refreshToken);
URI logOutAllURL = composeLogOutAllLinkURL();
apiProxy.post(logOutAllURL, null, JWTResponse.class);
}
@Override
public UserResponse me(String refreshToken) throws DescopeException {
if (StringUtils.isBlank(refreshToken)) {
throw ServerCommonException.missingArguments("refresh token");
}
validateJWT(refreshToken); // Will make sure token is still valid
ApiProxy apiProxy = getApiProxy(refreshToken);
return apiProxy.get(getUri(ME_LINK), UserResponse.class);
}
@Override
public List<UserHistoryResponse> history(String refreshToken) throws DescopeException {
if (StringUtils.isBlank(refreshToken)) {
throw ServerCommonException.missingArguments("refresh token");
}
validateJWT(refreshToken); // Will make sure token is still valid
ApiProxy apiProxy = getApiProxy(refreshToken);
return apiProxy.getArray(getUri(HISTORY_LINK), new TypeReference<List<UserHistoryResponse>>() {
});
}
@Override
public AuthenticationInfo selectTenant(String tenantId, String refreshToken) throws DescopeException {
if (StringUtils.isBlank(tenantId)) {
throw ServerCommonException.missingArguments("tenant ID");
}
if (StringUtils.isBlank(refreshToken)) {
throw ServerCommonException.missingArguments("refresh token");
}
ApiProxy apiProxy = getApiProxy(refreshToken);
Map<String, String> body = Collections.singletonMap("tenant", tenantId);
JWTResponse jwtResponse = apiProxy.post(composeSelectTenantURL(), body, JWTResponse.class);
return getAuthenticationInfo(jwtResponse);
}
@Override
public void validateDPoP(String sessionToken, String dpopProof, String method, String requestUrl)
throws DescopeException {
if (StringUtils.isBlank(sessionToken)) {
throw ServerCommonException.invalidArgument("sessionToken");
}
DPoPUtils.validateDPoPProof(dpopProof, method, requestUrl, sessionToken);
}
AuthenticationInfo exchangeToken(String code, URI url) {
if (StringUtils.isBlank(code)) {
throw ServerCommonException.invalidArgument("Code");
}
ExchangeTokenRequest request = new ExchangeTokenRequest(code);
ApiProxy apiProxy = getApiProxy();
JWTResponse jwtResponse = apiProxy.post(url, request, JWTResponse.class);
return getAuthenticationInfo(jwtResponse);
}
private URI composeExchangeAccessKeyLinkURL() {
return getUri(EXCHANGE_ACCESS_KEY_LINK);
}
private URI composeSelectTenantURL() {
return getUri(TENANT_SELECT_LINK);
}
private URI composeLogOutLinkURL() {
return getUri(LOG_OUT_LINK);
}
private URI composeLogOutAllLinkURL() {
return getUri(LOG_OUT_ALL_LINK);
}
}