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

com.backbase.oss.boat.bay.client.auth.HttpBearerAuth Maven / Gradle / Ivy

There is a newer version: 0.17.37
Show newest version
package com.backbase.oss.boat.bay.client.auth;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * An interceptor that adds the request header needed to use HTTP bearer authentication.
 */
public class HttpBearerAuth implements RequestInterceptor {
  private final String scheme;
  private String bearerToken;

  public HttpBearerAuth(String scheme) {
    this.scheme = scheme;
  }

  /**
   * Gets the token, which together with the scheme, will be sent as the value of the Authorization header.
   */
  public String getBearerToken() {
    return bearerToken;
  }

  /**
   * Sets the token, which together with the scheme, will be sent as the value of the Authorization header.
   */
  public void setBearerToken(String bearerToken) {
    this.bearerToken = bearerToken;
  }

  @Override
  public void apply(RequestTemplate template) {
    if(bearerToken == null) {
      return;
    }

    template.header("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken);
  }

  private static String upperCaseBearer(String scheme) {
    return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy