use of io.cdap.cdap.security.auth.TokenState in project cdap by caskdata.
the class MockAccessTokenIdentityExtractor method extract.
@Override
public UserIdentityExtractionResponse extract(HttpRequest request) throws UserIdentityExtractionException {
String auth = request.headers().get(HttpHeaderNames.AUTHORIZATION);
String accessToken = null;
if (auth != null) {
int idx = auth.trim().indexOf(' ');
if (idx < 0) {
return new UserIdentityExtractionResponse(UserIdentityExtractionState.ERROR_MISSING_CREDENTIAL, "No access token found");
}
accessToken = auth.substring(idx + 1).trim();
}
if (accessToken == null || accessToken.length() == 0) {
return new UserIdentityExtractionResponse(UserIdentityExtractionState.ERROR_MISSING_CREDENTIAL, "No access token found");
}
TokenState state = validator.validate(accessToken);
if (!state.isValid()) {
return new UserIdentityExtractionResponse(UserIdentityExtractionState.ERROR_INVALID_TOKEN, String.format("Failed to validate access token with reason: %s", state));
}
UserIdentityPair pair = new UserIdentityPair(accessToken, new UserIdentity("dummy", UserIdentity.IdentifierType.EXTERNAL, new LinkedHashSet<>(), System.currentTimeMillis(), System.currentTimeMillis() + 100000));
return new UserIdentityExtractionResponse(pair);
}
Aggregations