Skip to content

Commit 0d1a8c7

Browse files
fix: attach Builder authtoken to AuthInterceptor and prevent duplicate authtoken headers
Two related authentication bugs: 1. Contentstack.Builder.setAuthtoken() stored the token on the client but never wired it into AuthInterceptor - only login() did. Clients built via setAuthtoken() silently sent no authtoken header at all; requests only worked when an authorization (management token) header masked it. 2. AuthInterceptor used addHeader(), so requests that already carried an explicit authtoken (e.g. via @HeaderMap) received a duplicate header and were rejected as unauthenticated. The client-level token is now attached only when the request does not already have one.
1 parent d3da24d commit 0d1a8c7

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/main/java/com/contentstack/cms/Contentstack.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,8 +1059,12 @@ private OkHttpClient httpClient(Contentstack contentstack, Boolean retryOnFailur
10591059
// Add interceptor to handle OAuth, token refresh, and retries
10601060
builder.addInterceptor(this.oauthInterceptor);
10611061
} else {
1062-
this.authInterceptor = contentstack.interceptor = new AuthInterceptor();
1063-
1062+
// Wire the Builder's authtoken into the interceptor so that
1063+
// clients built via setAuthtoken(..) actually authenticate.
1064+
// (Previously only login() set it, so setAuthtoken-built clients
1065+
// silently sent no authtoken header at all.)
1066+
this.authInterceptor = contentstack.interceptor = new AuthInterceptor(this.authtoken);
1067+
10641068
// Configure early access if needed
10651069
if (this.earlyAccess != null) {
10661070
this.authInterceptor.setEarlyAccess(this.earlyAccess);

src/main/java/com/contentstack/cms/core/AuthInterceptor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public Response intercept(Chain chain) throws IOException {
8686
request.header(Util.CONTENT_TYPE, Util.CONTENT_TYPE_VALUE);
8787
}
8888

89-
if (this.authtoken != null) {
89+
// Attach the client-level authtoken only when the request doesn't already
90+
// carry one (e.g. via @HeaderMap) - addHeader would APPEND a duplicate
91+
// authtoken header and the API rejects the request as unauthenticated.
92+
if (this.authtoken != null && originalRequest.header(Util.AUTHTOKEN) == null) {
9093
request.addHeader(Util.AUTHTOKEN, this.authtoken);
9194
}
9295

0 commit comments

Comments
 (0)