Skip to content

Commit

Permalink
Merge pull request #26 from blaurockm/feature/bearer
Browse files Browse the repository at this point in the history
Support for Bearer-Authentication in addition to Basic-Authentication
  • Loading branch information
PhantomYdn authored Jul 15, 2024
2 parents a2924b0 + 012d0bc commit a4d4a77
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public class AuthorizationInterceptor implements Interceptor {

private String username;
private String password;
private boolean byPass;

private boolean basicAuth;

// https://github.com/OrienteerBAP/JNPM/commit/8fd783a32024dff44d1d56d654f8341f9ddbd7b3
private boolean bearerAuth;

public AuthorizationInterceptor() {
this(JNPMService.instance().getSettings());
}
Expand All @@ -27,18 +30,25 @@ public AuthorizationInterceptor(JNPMSettings settings) {
public AuthorizationInterceptor(String username, String password) {
this.username = username;
this.password = password;
this.byPass = (username == null || username.trim().isEmpty())
&& (password == null || password.trim().isEmpty());
this.basicAuth = (username != null && !username.trim().isEmpty())
&& (password != null && !password.trim().isEmpty());
this.bearerAuth = (username != null && !username.trim().isEmpty())
&& password == null;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if(!byPass) {
if(basicAuth) {
request = request.newBuilder()
.addHeader("Authorization", Credentials.basic(username, password))
.build();
}
if(bearerAuth) {
request = request.newBuilder()
.addHeader("Authorization", "Bearer " + username)
.build();
}
return chain.proceed(request);
}

Expand Down

0 comments on commit a4d4a77

Please sign in to comment.