com.atlassian.usercontext.impl.UserContextImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atlassian-user-context Show documentation
Show all versions of atlassian-user-context Show documentation
Java library that implements the User Context spec for passing user context information between services
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;
}
}