From 012d0bcd0dcb800a87abf98e384a8fca090b4571 Mon Sep 17 00:00:00 2001 From: Markus Blaurock Date: Mon, 15 Jul 2024 07:45:35 +0200 Subject: [PATCH] simplest version that works --- .../jnpm/AuthorizationInterceptor.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/jnpm/src/main/java/org/orienteer/jnpm/AuthorizationInterceptor.java b/jnpm/src/main/java/org/orienteer/jnpm/AuthorizationInterceptor.java index 886d1af..30c29de 100644 --- a/jnpm/src/main/java/org/orienteer/jnpm/AuthorizationInterceptor.java +++ b/jnpm/src/main/java/org/orienteer/jnpm/AuthorizationInterceptor.java @@ -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()); } @@ -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); }