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

com.atlassian.usercontext.api.AuthenticationRequest 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.api;

import javax.annotation.Nullable;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * Represents a request to generate a user context token for an authentication method that is not
 * handled by the edge authenticator.
 *
 * A reason for generating a user context is required
 * @see UserContextAuthenticator
 * @see Reason
 */
public class AuthenticationRequest {

    private final Reason reason;
    private final AccountId authenticatedAccountId;
    @Nullable
    private final String authenticatedContextRestriction;
    @Nullable
    private final AccountId impersonatedAccountId;
    @Nullable
    private final String impersonatedContextRestriction;


    private AuthenticationRequest(Builder builder) {
        reason = requireNonNull(builder.reason);
        authenticatedAccountId = requireNonNull(builder.authenticatedAccountId);
        authenticatedContextRestriction = builder.authenticatedContextRestriction;
        impersonatedAccountId = builder.impersonatedAccountId;
        impersonatedContextRestriction = builder.impersonatedContextRestriction;
    }

    public Reason getReason() {
        return reason;
    }

    public AccountId getAuthenticatedAccountId() {
        return authenticatedAccountId;
    }

    public Optional getAuthenticatedContextRestriction() {
        return Optional.ofNullable(authenticatedContextRestriction);
    }

    public Optional getImpersonatedAccountId() {
        return Optional.ofNullable(impersonatedAccountId);
    }

    public Optional getImpersonatedContextRestriction() {
        return Optional.ofNullable(impersonatedContextRestriction);
    }

    public static Builder builderFor(Reason reason) {
        return new Builder(reason);
    }

    public static class Builder {

        private final Reason reason;

        private AccountId authenticatedAccountId;
        private String authenticatedContextRestriction;
        private AccountId impersonatedAccountId;
        private String impersonatedContextRestriction;

        private Builder(Reason reason) {

            this.reason = reason;
        }

        public AuthenticationRequest build() {
            return new AuthenticationRequest(this);
        }

        public Builder authenticatingAs(AccountId accountId) {
            requireNonNull(accountId);
            this.authenticatedAccountId = accountId;
            return this;
        }

        public Builder restrictedTo(String contextRestriction) {
            requireNonNull(contextRestriction);
            this.authenticatedContextRestriction = contextRestriction;
            return this;
        }

        public Builder impersonatingAs(AccountId accountId, String contextRestriction) {
            requireNonNull(accountId);
            requireNonNull(contextRestriction);
            this.impersonatedAccountId = accountId;
            this.impersonatedContextRestriction = contextRestriction;
            return this;
        }
    }

    /**
     * The reason for generating a user context token outside of the edge authenticator.
     *
     * The set of reasons should diminish over time as more authentication methods are replaced or moved to the
     * edge authenticator.
     */
    public enum Reason {
        ASYNC_PROCESSING,
        NOTIFICATIONS,
        CONNECT_AP_REQUEST,
        CONNECT_JWT,
        CONNECT_RFC7523,
        APPLINKS_3LO,
        APPLINKS_2LO,
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy