Java.libraries.retrofit.auth.HttpBearerAuth.mustache Maven / Gradle / Ivy
package {{invokerPackage}}.auth;
import java.io.IOException;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
public class HttpBearerAuth implements Interceptor {
private final String scheme;
private String bearerToken;
public HttpBearerAuth(String scheme) {
this.scheme = scheme;
}
public String getBearerToken() {
return bearerToken;
}
public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// If the request already have an authorization (eg. Basic auth), do nothing
if (request.header("Authorization") == null && bearerToken != null) {
request = request.newBuilder()
.addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken)
.build();
}
return chain.proceed(request);
}
private static String upperCaseBearer(String scheme) {
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
}
}