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

com.atlassian.usercontext.impl.ImpersonationImpl 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.usercontext.api.AccountId;
import com.atlassian.usercontext.api.Impersonation;

import javax.annotation.Nullable;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonValue;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * Represents an impersonated session, where a user may impersonate another user
 * (e.g. when a site administrator uses the 'login as user' feature).
 */
public class ImpersonationImpl implements Impersonation {

    private static final String EXPIRY_FIELD = "exp";

    static final String IMPERSONATION_ID_FIELD = "iid";
    static final String CONTEXT_RESTRICTION_FIELD = "contextRestriction";
    static final String ACCOUNT_ID_FIELD = "accountId";

    private final String impersonationId;
    private final AccountId accountId;
    private final String contextRestriction;
    private final Instant expiry; // Can be null, representing no expiry

    private ImpersonationImpl(String impersonationId,
                              AccountId accountId,
                              String contextRestriction,
                              @Nullable Instant expiry) {
        this.impersonationId = requireNonNull(impersonationId);
        this.accountId = requireNonNull(accountId);
        this.contextRestriction = requireNonNull(contextRestriction);
        this.expiry = expiry;
    }

    static ImpersonationImpl fromJsonValue(JsonValue jsonValue) {
        if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) {
            throw new IllegalArgumentException("JsonValue must be an object");
        }

        JsonObject jsonObject = (JsonObject) jsonValue;

        // Can be null, representing no expiry
        JsonNumber expiry = jsonObject.getJsonNumber(EXPIRY_FIELD);

        return new ImpersonationImpl(
                jsonObject.getString(IMPERSONATION_ID_FIELD),
                AccountId.of(jsonObject.getString(ACCOUNT_ID_FIELD)),
                jsonObject.getString(CONTEXT_RESTRICTION_FIELD),
                expiry == null ? null : Instant.ofEpochSecond(expiry.longValue())
        );
    }

    /**
     * Unique Id for impersonation
     *
     * @return the impersonation id
     */
    public String getImpersonationId() {
        return impersonationId;
    }

    /**
     * The native user Id for the user that can be impersonated
     *
     * @return the native atlassian account id of the user being impersonated
     */
    @Override
    public AccountId getAccountId() {
        return accountId;
    }

    /**
     * The site that the impersonation is enabled for. This currently is a non-overlapping
     * list, so consumers are expected to choose the session with the contextRestriction most
     * relevant to their context (e.g. their cloudId). In future we may support a wildcard
     * contextRestriction (e.g. for org admins to impersonate managed users); if this occurs,
     * consumers should choose the session with the most specialised contextRestriction.
     *
     * @return The cloudId of the site that the impersonation is enabled for
     */
    @Override
    public String getContextRestriction() {
        return contextRestriction;
    }

    /**
     * When the impersonation expires
     *
     * @return an instant representing when the impersonation expires
     */
    @Override
    public Optional getExpiry() {
        return Optional.ofNullable(expiry);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ImpersonationImpl that = (ImpersonationImpl) o;
        return expiry == that.expiry &&
                Objects.equals(impersonationId, that.impersonationId) &&
                Objects.equals(accountId, that.accountId) &&
                Objects.equals(contextRestriction, that.contextRestriction);
    }

    @Override
    public int hashCode() {
        return Objects.hash(impersonationId, accountId, contextRestriction, expiry);
    }

    @Override
    public String toString() {
        return "Impersonation{" +
                "impersonationId='" + impersonationId + '\'' +
                ", accountId=" + accountId +
                ", contextRestriction='" + contextRestriction + '\'' +
                ", expiry=" + expiry +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy