github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/feign/auth/HttpBearerAuth.mustache (about)

     1  package {{invokerPackage}}.auth;
     2  
     3  import feign.RequestInterceptor;
     4  import feign.RequestTemplate;
     5  
     6  /**
     7   * An interceptor that adds the request header needed to use HTTP bearer authentication.
     8   */
     9  public class HttpBearerAuth implements RequestInterceptor {
    10    private final String scheme;
    11    private String bearerToken;
    12  
    13    public HttpBearerAuth(String scheme) {
    14      this.scheme = scheme;
    15    }
    16  
    17    /**
    18     * Gets the token, which together with the scheme, will be sent as the value of the Authorization header.
    19     */
    20    public String getBearerToken() {
    21      return bearerToken;
    22    }
    23  
    24    /**
    25     * Sets the token, which together with the scheme, will be sent as the value of the Authorization header.
    26     */
    27    public void setBearerToken(String bearerToken) {
    28      this.bearerToken = bearerToken;
    29    }
    30  
    31    @Override
    32    public void apply(RequestTemplate template) {
    33      if(bearerToken == null) {
    34        return;
    35      }
    36  
    37      template.header("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken);
    38    }
    39  
    40    private static String upperCaseBearer(String scheme) {
    41      return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
    42    }
    43  }