Search in sources :

Example 1 with TokenState

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UserIdentityExtractionResponse(io.cdap.cdap.security.auth.UserIdentityExtractionResponse) UserIdentity(io.cdap.cdap.security.auth.UserIdentity) UserIdentityPair(io.cdap.cdap.security.auth.UserIdentityPair) TokenState(io.cdap.cdap.security.auth.TokenState)

Aggregations

TokenState (io.cdap.cdap.security.auth.TokenState)1 UserIdentity (io.cdap.cdap.security.auth.UserIdentity)1 UserIdentityExtractionResponse (io.cdap.cdap.security.auth.UserIdentityExtractionResponse)1 UserIdentityPair (io.cdap.cdap.security.auth.UserIdentityPair)1 LinkedHashSet (java.util.LinkedHashSet)1