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

com.squareup.square.legacy.authentication.BearerAuthManager Maven / Gradle / Ivy

There is a newer version: 44.2.0.20250521
Show newest version
package com.squareup.square.legacy.authentication;

import com.squareup.square.legacy.BearerAuthCredentials;
import io.apimatic.core.authentication.HeaderAuth;
import java.util.Collections;

/**
 * Utility class for authorization and token management.
 */
public class BearerAuthManager extends HeaderAuth implements BearerAuthCredentials {

    /**
     * Private instance of the auth model containing the auth credentials.
     */
    private BearerAuthModel authModel;

    /**
     * Constructor.
     * @param authModel The instance of auth credentials.
     */
    public BearerAuthManager(BearerAuthModel authModel) {
        super(Collections.singletonMap("Authorization", applyBearerPrefix(authModel.getAccessToken())));
        this.authModel = authModel;
    }

    /**
     * applies bearer prefix to the access token.
     * @param accessToken The actual access token.
     * @return The access token with 'Bearer' as prefix.
     */
    private static String applyBearerPrefix(String accessToken) {
        if (accessToken == null || accessToken == "") {
            return null;
        }

        return "Bearer " + accessToken;
    }

    /**
     * String value for accessToken.
     * @return accessToken
     */
    public String getAccessToken() {
        return authModel.getAccessToken();
    }

    /**
     * Checks if provided credentials matched with existing ones.
     * @param accessToken String value for credentials.
     * @return true if credentials matched.
     */
    public boolean equals(String accessToken) {
        return accessToken.equals(getAccessToken());
    }

    /**
     * Converts this BearerAuthManager into string format.
     * @return String representation of this class
     */
    @Override
    public String toString() {
        return "BearerAuthManager [" + "accessToken=" + getAccessToken() + "]";
    }
    /**
     * Returns the error message if the auth credentials are not valid.
     * @return the auth specific error message.
     */
    @Override
    public String getErrorMessage() {
        String errorMessage = super.getErrorMessage();
        if (errorMessage == null) {
            return null;
        }

        return "BearerAuth - " + errorMessage;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy