All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlassian.usercontext.impl.UserContextImpl Maven / Gradle / Ivy

Go to download

Java library that implements the User Context spec for passing user context information between services

There is a newer version: 0.3.1
Show newest version
package com.atlassian.usercontext.impl;

import com.atlassian.asap.api.Jwt;
import com.atlassian.usercontext.api.AccountId;
import com.atlassian.usercontext.api.Impersonation;
import com.atlassian.usercontext.api.UserContext;
import com.atlassian.usercontext.api.UserContextClaims;

import java.io.Serializable;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * Implementation for the interface {@link UserContext}.
 */

public class UserContextImpl implements UserContext, Serializable {

    private static final long serialVersionUID = 1L;

    private final Jwt originalToken;
    private final String originalSignedJwt;
    private final UserContextClaims userContextClaims;

    UserContextImpl(Jwt token, String originalSignedJwt) {
        this.originalToken = requireNonNull(token);
        this.userContextClaims = new UserContextClaimsImpl(originalToken.getClaims());
        this.originalSignedJwt = requireNonNull(originalSignedJwt);
    }

    @Override
    public UserContextClaims getUserContextClaims() {
        return userContextClaims;
    }

    @Override
    public AccountId resolveAccountId(String contextRestriction) {

        Optional impersonatedUser = userContextClaims.getImpersonations().stream()
                .filter(imp -> imp.getContextRestriction().equals(contextRestriction))
                .map(Impersonation::getAccountId)
                .findFirst();

        return impersonatedUser.orElseGet(() ->
                userContextClaims.getContextRestriction().isEmpty() || userContextClaims.getContextRestriction().contains(contextRestriction) ?
                        userContextClaims.getAccountId() :
                        AccountId.UNIDENTIFIED);
    }

    @Override
    public Jwt getUserContextJwt() {
        return originalToken;
    }

    @Override
    public String getSignedJwt() {
        return originalSignedJwt;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy