Skip to content

Commit 2353b18

Browse files
authored
Merge pull request #184 from contentstack/fix/oauth-impl
Fix oauth implementation
2 parents b5feb78 + 3d91e99 commit 2353b18

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

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

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -740,14 +740,6 @@ public Builder setOAuthConfig(OAuthConfig config) {
740740
return this;
741741
}
742742

743-
/**
744-
* Configures OAuth with client credentials (traditional flow)
745-
* @param appId Application ID
746-
* @param clientId Client ID
747-
* @param clientSecret Client secret
748-
* @param redirectUri Redirect URI
749-
* @return Builder instance
750-
*/
751743
private TokenCallback tokenCallback;
752744

753745
/**
@@ -760,64 +752,51 @@ public Builder setTokenCallback(TokenCallback callback) {
760752
return this;
761753
}
762754

763-
public Builder setOAuth(String appId, String clientId, String clientSecret, String redirectUri) {
764-
// Use the builder's hostname (which defaults to Util.HOST if not set)
765-
return setOAuth(appId, clientId, clientSecret, redirectUri, this.hostname);
766-
}
767-
768755
/**
769-
* Configures OAuth with client credentials and specific host
756+
* Configures OAuth authentication with PKCE flow (no client secret)
770757
* @param appId Application ID
771758
* @param clientId Client ID
772-
* @param clientSecret Client secret
773759
* @param redirectUri Redirect URI
774-
* @param host API host (e.g. "api.contentstack.io", "eu-api.contentstack.com")
775760
* @return Builder instance
776761
*/
777-
public Builder setOAuth(String appId, String clientId, String clientSecret, String redirectUri, String host) {
778-
OAuthConfig.OAuthConfigBuilder builder = OAuthConfig.builder()
779-
.appId(appId)
780-
.clientId(clientId)
781-
.clientSecret(clientSecret)
782-
.redirectUri(redirectUri)
783-
.host(host);
784-
785-
// Add token callback if set
786-
if (this.tokenCallback != null) {
787-
builder.tokenCallback(this.tokenCallback);
788-
}
789-
790-
this.oauthConfig = builder.build();
791-
return this;
762+
public Builder setOAuth(String appId, String clientId, String redirectUri) {
763+
// Use the builder's hostname (which defaults to Util.HOST if not set)
764+
return setOAuth(appId, clientId, redirectUri, this.hostname);
792765
}
793766

794767
/**
795-
* Configures OAuth with PKCE (no client secret)
768+
* Configures OAuth authentication with PKCE flow (no client secret) and specific host
796769
* @param appId Application ID
797770
* @param clientId Client ID
798771
* @param redirectUri Redirect URI
772+
* @param host API host (e.g. "api.contentstack.io", "eu-api.contentstack.com")
799773
* @return Builder instance
800774
*/
801-
public Builder setOAuthWithPKCE(String appId, String clientId, String redirectUri) {
802-
// Use the builder's hostname (which defaults to Util.HOST if not set)
803-
return setOAuthWithPKCE(appId, clientId, redirectUri, this.hostname);
775+
public Builder setOAuth(String appId, String clientId, String redirectUri, String host) {
776+
return setOAuth(appId, clientId, redirectUri, host, null);
804777
}
805778

806779
/**
807-
* Configures OAuth with PKCE (no client secret) and specific host
780+
* Configures OAuth authentication with optional client secret. PKCE flow is used when clientSecret is not provided.
808781
* @param appId Application ID
809782
* @param clientId Client ID
810783
* @param redirectUri Redirect URI
811784
* @param host API host (e.g. "api.contentstack.io", "eu-api.contentstack.com")
785+
* @param clientSecret Optional client secret. If not provided, PKCE flow will be used
812786
* @return Builder instance
813787
*/
814-
public Builder setOAuthWithPKCE(String appId, String clientId, String redirectUri, String host) {
788+
public Builder setOAuth(String appId, String clientId, String redirectUri, String host, String clientSecret) {
815789
OAuthConfig.OAuthConfigBuilder builder = OAuthConfig.builder()
816790
.appId(appId)
817791
.clientId(clientId)
818792
.redirectUri(redirectUri)
819793
.host(host);
820794

795+
// Only set clientSecret if provided (otherwise PKCE flow will be used)
796+
if (clientSecret != null && !clientSecret.trim().isEmpty()) {
797+
builder.clientSecret(clientSecret);
798+
}
799+
821800
// Add token callback if set
822801
if (this.tokenCallback != null) {
823802
builder.tokenCallback(this.tokenCallback);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class Util {
6161
public static final String OAUTH_NO_TOKENS = "No OAuth tokens available. Please authenticate first.";
6262
public static final String OAUTH_NO_REFRESH_TOKEN = "No refresh token available";
6363
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()";
64+
public static final String OAUTH_CONFIG_MISSING = "OAuth is not configured. Use Builder.setOAuth() with or without clientSecret for PKCE flow";
6565
public static final String OAUTH_REFRESH_FAILED = "Failed to refresh access token";
6666
public static final String OAUTH_REVOKE_FAILED = "Failed to revoke authorization";
6767
public static final String OAUTH_STATUS_FAILED = "Failed to get authorization status";

src/test/java/com/contentstack/cms/oauth/OAuthTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ public void setup() {
8080

8181
// Create Contentstack clients
8282
pkceClient = new Contentstack.Builder()
83-
.setOAuthWithPKCE(TEST_APP_ID, TEST_CLIENT_ID, TEST_REDIRECT_URI)
83+
.setOAuth(TEST_APP_ID, TEST_CLIENT_ID, TEST_REDIRECT_URI)
8484
.build();
8585

8686
clientSecretClient = new Contentstack.Builder()
87-
.setOAuth(TEST_APP_ID, TEST_CLIENT_ID, TEST_CLIENT_SECRET, TEST_REDIRECT_URI)
87+
.setOAuth(TEST_APP_ID, TEST_CLIENT_ID, TEST_REDIRECT_URI, Util.HOST, TEST_CLIENT_SECRET)
8888
.build();
8989
}
9090

@@ -113,7 +113,7 @@ public void testInvalidConfigurations() {
113113
// Test invalid app ID
114114
try {
115115
new Contentstack.Builder()
116-
.setOAuthWithPKCE("", TEST_CLIENT_ID, TEST_REDIRECT_URI)
116+
.setOAuth("", TEST_CLIENT_ID, TEST_REDIRECT_URI)
117117
.build();
118118
fail("Should throw exception for empty app ID");
119119
} catch (IllegalArgumentException e) {
@@ -282,7 +282,7 @@ public void testHostStorage() {
282282

283283
// Test host storage via Contentstack.Builder
284284
Contentstack client = new Contentstack.Builder()
285-
.setOAuth(TEST_APP_ID, TEST_CLIENT_ID, TEST_CLIENT_SECRET, TEST_REDIRECT_URI, testHost)
285+
.setOAuth(TEST_APP_ID, TEST_CLIENT_ID, TEST_REDIRECT_URI, testHost, TEST_CLIENT_SECRET)
286286
.build();
287287

288288
String authUrl = client.getOAuthAuthorizationUrl();
@@ -291,7 +291,7 @@ public void testHostStorage() {
291291

292292
// Test host storage via PKCE builder
293293
client = new Contentstack.Builder()
294-
.setOAuthWithPKCE(TEST_APP_ID, TEST_CLIENT_ID, TEST_REDIRECT_URI, testHost)
294+
.setOAuth(TEST_APP_ID, TEST_CLIENT_ID, TEST_REDIRECT_URI, testHost)
295295
.build();
296296

297297
authUrl = client.getOAuthAuthorizationUrl();

0 commit comments

Comments
 (0)