Skip to content

Commit 10383cd

Browse files
committed
refactor: Move OAuth error messages to constants and clean up imports
1 parent f7ae51f commit 10383cd

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.time.Duration;
66
import java.util.HashMap;
77
import java.util.Map;
8-
import java.util.Objects;
98
import java.util.concurrent.CompletableFuture;
109
import java.util.concurrent.TimeUnit;
1110
import java.util.logging.Logger;
@@ -17,8 +16,6 @@
1716
import static com.contentstack.cms.core.Util.API_KEY;
1817
import static com.contentstack.cms.core.Util.AUTHORIZATION;
1918
import static com.contentstack.cms.core.Util.BRANCH;
20-
import static com.contentstack.cms.core.Util.ILLEGAL_USER;
21-
import static com.contentstack.cms.core.Util.PLEASE_LOGIN;
2219
import com.contentstack.cms.models.Error;
2320
import com.contentstack.cms.models.LoginDetails;
2421
import com.contentstack.cms.models.OAuthConfig;
@@ -99,7 +96,7 @@ public class Contentstack {
9996
*/
10097
public User user() {
10198
if (!isOAuthConfigured() && this.authtoken == null) {
102-
throw new IllegalStateException("Please login or configure OAuth to access user");
99+
throw new IllegalStateException(Util.OAUTH_LOGIN_REQUIRED + " user");
103100
}
104101
user = new User(this.instance);
105102
return user;
@@ -293,7 +290,7 @@ Response<ResponseBody> logoutWithAuthtoken(String authtoken) throws IOException
293290
*/
294291
public Organization organization() {
295292
if (!isOAuthConfigured() && this.authtoken == null) {
296-
throw new IllegalStateException("Please login or configure OAuth to access organization");
293+
throw new IllegalStateException(Util.OAUTH_LOGIN_REQUIRED + " organization");
297294
}
298295

299296
// If using OAuth, get organization from tokens
@@ -328,10 +325,10 @@ public Organization organization() {
328325
*/
329326
public Organization organization(@NotNull String organizationUid) {
330327
if (!isOAuthConfigured() && this.authtoken == null) {
331-
throw new IllegalStateException("Please login or configure OAuth to access organization");
328+
throw new IllegalStateException(Util.OAUTH_LOGIN_REQUIRED + " organization");
332329
}
333330
if (organizationUid.isEmpty()) {
334-
throw new IllegalStateException("organizationUid can not be empty");
331+
throw new IllegalStateException(Util.OAUTH_ORG_EMPTY);
335332
}
336333
return new Organization(this.instance, organizationUid);
337334
}
@@ -355,7 +352,7 @@ public Organization organization(@NotNull String organizationUid) {
355352
*/
356353
public Stack stack() {
357354
if (!isOAuthConfigured() && this.authtoken == null) {
358-
throw new IllegalStateException("Please login or configure OAuth to access stack");
355+
throw new IllegalStateException(Util.OAUTH_LOGIN_REQUIRED + " stack");
359356
}
360357
return new Stack(this.instance);
361358
}
@@ -380,7 +377,7 @@ public Stack stack() {
380377
*/
381378
public Stack stack(@NotNull Map<String, Object> header) {
382379
if (!isOAuthConfigured() && this.authtoken == null && !header.containsKey(AUTHORIZATION)) {
383-
throw new IllegalStateException("Please login or configure OAuth to access stack");
380+
throw new IllegalStateException(Util.OAUTH_LOGIN_REQUIRED + " stack");
384381
}
385382
return new Stack(this.instance, header);
386383
}
@@ -476,7 +473,7 @@ public Stack stack(@NotNull String apiKey, @NotNull String managementToken, @Not
476473
*/
477474
public String getOAuthAuthorizationUrl() {
478475
if (!isOAuthConfigured()) {
479-
throw new IllegalStateException("OAuth is not configured. Use Builder.setOAuth() or Builder.setOAuthWithPKCE()");
476+
throw new IllegalStateException(Util.OAUTH_CONFIG_MISSING);
480477
}
481478
return oauthHandler.authorize();
482479
}
@@ -489,7 +486,7 @@ public String getOAuthAuthorizationUrl() {
489486
*/
490487
public CompletableFuture<OAuthTokens> exchangeOAuthCode(String code) {
491488
if (!isOAuthConfigured()) {
492-
throw new IllegalStateException("OAuth is not configured. Use Builder.setOAuth() or Builder.setOAuthWithPKCE()");
489+
throw new IllegalStateException(Util.OAUTH_CONFIG_MISSING);
493490
}
494491
return oauthHandler.exchangeCodeForToken(code);
495492
}
@@ -501,7 +498,7 @@ public CompletableFuture<OAuthTokens> exchangeOAuthCode(String code) {
501498
*/
502499
public CompletableFuture<OAuthTokens> refreshOAuthToken() {
503500
if (!isOAuthConfigured()) {
504-
throw new IllegalStateException("OAuth is not configured. Use Builder.setOAuth() or Builder.setOAuthWithPKCE()");
501+
throw new IllegalStateException(Util.OAUTH_CONFIG_MISSING);
505502
}
506503
return oauthHandler.refreshAccessToken();
507504
}
@@ -545,7 +542,7 @@ public OAuthHandler getOAuthHandler() {
545542
*/
546543
public CompletableFuture<Void> oauthLogout(boolean revokeAuthorization) {
547544
if (!isOAuthConfigured()) {
548-
throw new IllegalStateException("OAuth is not configured. Use Builder.setOAuth() or Builder.setOAuthWithPKCE()");
545+
throw new IllegalStateException(Util.OAUTH_CONFIG_MISSING);
549546
}
550547
return oauthHandler.logout(revokeAuthorization);
551548
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ public class Util {
5151
public static final String ERROR_INSTALLATION = "installation uid is required";
5252
public static final String MISSING_ORG_ID = "organization uid is required";
5353

54+
// OAuth Constants
55+
public static final String OAUTH_APP_HOST = "app.contentstack.com";
56+
public static final String OAUTH_API_HOST = "developerhub-api.contentstack.com";
57+
public static final String OAUTH_TOKEN_ENDPOINT = "/token";
58+
public static final String OAUTH_AUTHORIZE_ENDPOINT = "/#!/apps/%s/authorize";
59+
60+
// OAuth Error Messages
61+
public static final String OAUTH_NO_TOKENS = "No OAuth tokens available. Please authenticate first.";
62+
public static final String OAUTH_NO_REFRESH_TOKEN = "No refresh token available";
63+
public static final String OAUTH_EMPTY_CODE = "Authorization code cannot be null or empty";
64+
public static final String OAUTH_CONFIG_MISSING = "OAuth is not configured. Use Builder.setOAuth() or Builder.setOAuthWithPKCE()";
65+
public static final String OAUTH_REFRESH_FAILED = "Failed to refresh access token";
66+
public static final String OAUTH_REVOKE_FAILED = "Failed to revoke authorization";
67+
public static final String OAUTH_STATUS_FAILED = "Failed to get authorization status";
68+
public static final String OAUTH_LOGIN_REQUIRED = "Please login or configure OAuth to access";
69+
public static final String OAUTH_ORG_EMPTY = "organizationUid can not be empty";
70+
5471
// The code `Util() throws IllegalAccessException` is a constructor for the
5572
// `Util` class that throws an
5673
// `IllegalAccessException` when called. This constructor is marked as private,

src/main/java/com/contentstack/cms/models/OAuthConfig.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.Arrays;
99
import java.util.List;
1010

11+
import com.contentstack.cms.core.Util;
12+
1113
/**
1214
* Configuration class for OAuth 2.0 authentication
1315
*/
@@ -67,7 +69,7 @@ public String getFormattedAuthorizationEndpoint() {
6769
return authEndpoint;
6870
}
6971

70-
String hostname = "app.contentstack.com";
72+
String hostname = Util.OAUTH_APP_HOST;
7173

7274
// Transform hostname if needed
7375
if (hostname.contains("contentstack")) {
@@ -76,7 +78,7 @@ public String getFormattedAuthorizationEndpoint() {
7678
.replaceAll("\\.io$", ".com"); // *.io -> *.com
7779
}
7880

79-
return "https://" + hostname + "/#!/apps/" + appId + "/authorize";
81+
return "https://" + hostname + String.format(Util.OAUTH_AUTHORIZE_ENDPOINT, appId);
8082
}
8183

8284
/**
@@ -89,7 +91,7 @@ public String getTokenEndpoint() {
8991
return tokenEndpoint;
9092
}
9193

92-
String hostname = "developerhub-api.contentstack.com";
94+
String hostname = Util.OAUTH_API_HOST;
9395

9496
// Transform hostname if needed
9597
if (hostname.contains("contentstack")) {
@@ -98,7 +100,7 @@ public String getTokenEndpoint() {
98100
.replaceAll("\\.io$", ".com"); // *.io -> *.com
99101
}
100102

101-
return "https://" + hostname + "/token";
103+
return "https://" + hostname + Util.OAUTH_TOKEN_ENDPOINT;
102104
}
103105

104106
/**

src/main/java/com/contentstack/cms/oauth/OAuthHandler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Date;
1111
import java.util.concurrent.CompletableFuture;
1212

13+
import com.contentstack.cms.core.Util;
1314
import com.contentstack.cms.models.OAuthConfig;
1415
import com.contentstack.cms.models.OAuthTokens;
1516
import com.google.gson.Gson;
@@ -154,7 +155,7 @@ public String authorize() {
154155
*/
155156
public CompletableFuture<OAuthTokens> exchangeCodeForToken(String code) {
156157
if (code == null || code.trim().isEmpty()) {
157-
return CompletableFuture.failedFuture(new IllegalArgumentException("Authorization code cannot be null or empty"));
158+
return CompletableFuture.failedFuture(new IllegalArgumentException(Util.OAUTH_EMPTY_CODE));
158159
}
159160
return CompletableFuture.supplyAsync(() -> {
160161
try {
@@ -210,11 +211,11 @@ public CompletableFuture<OAuthTokens> refreshAccessToken() {
210211
// Check if we have tokens and refresh token
211212
if (tokens == null) {
212213
return CompletableFuture.failedFuture(
213-
new IllegalStateException("No tokens available"));
214+
new IllegalStateException(Util.OAUTH_NO_TOKENS));
214215
}
215216
if (!tokens.hasRefreshToken()) {
216217
return CompletableFuture.failedFuture(
217-
new IllegalStateException("No refresh token available"));
218+
new IllegalStateException(Util.OAUTH_NO_REFRESH_TOKEN));
218219
}
219220

220221
// Check if token is actually expired
@@ -249,7 +250,7 @@ public CompletableFuture<OAuthTokens> refreshAccessToken() {
249250
return newTokens;
250251
} catch (IOException | RuntimeException e) {
251252

252-
throw new RuntimeException("Failed to refresh tokens", e);
253+
throw new RuntimeException(Util.OAUTH_REFRESH_FAILED, e);
253254
}
254255
});
255256
}
@@ -343,7 +344,7 @@ public CompletableFuture<OAuthTokens> getOauthAppAuthorization() {
343344

344345
if (!response.isSuccessful()) {
345346
String error = responseBody != null ? responseBody.string() : "Unknown error";
346-
throw new RuntimeException("Failed to get authorization status: " + error);
347+
throw new RuntimeException(Util.OAUTH_STATUS_FAILED + ": " + error);
347348
}
348349

349350
String body = responseBody != null ? responseBody.string() : "{}";
@@ -386,7 +387,7 @@ public CompletableFuture<Void> revokeOauthAppAuthorization() {
386387

387388
if (!response.isSuccessful()) {
388389
String error = responseBody != null ? responseBody.string() : "Unknown error";
389-
throw new RuntimeException("Failed to revoke authorization: " + error);
390+
throw new RuntimeException(Util.OAUTH_REVOKE_FAILED + ": " + error);
390391
}
391392
} finally {
392393
if (responseBody != null) {

src/main/java/com/contentstack/cms/oauth/OAuthInterceptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private Response executeRequest(Chain chain, Request request, int retryCount) th
6464

6565
// Ensure we have tokens
6666
if (oauthHandler == null || oauthHandler.getTokens() == null) {
67-
throw new IOException("No OAuth tokens available. Please authenticate first.");
67+
throw new IOException(Util.OAUTH_NO_TOKENS);
6868
}
6969

7070
// Check if we need to refresh the token before making the request
@@ -81,7 +81,7 @@ private Response executeRequest(Chain chain, Request request, int retryCount) th
8181
.build();
8282
} catch (InterruptedException | ExecutionException | TimeoutException e) {
8383

84-
throw new IOException("Failed to refresh access token", e);
84+
throw new IOException(Util.OAUTH_REFRESH_FAILED, e);
8585
}
8686
}
8787
}
@@ -110,7 +110,7 @@ private Response executeRequest(Chain chain, Request request, int retryCount) th
110110

111111
return executeRequest(chain, request, retryCount + 1);
112112
} catch (InterruptedException | ExecutionException | TimeoutException e) {
113-
throw new IOException("Failed to refresh access token after 401", e);
113+
throw new IOException(Util.OAUTH_REFRESH_FAILED + " after 401", e);
114114
}
115115
}
116116
}

0 commit comments

Comments
 (0)