Skip to content

Commit 9544846

Browse files
committed
fix: Update OAuth implementation
1 parent b5ef724 commit 9544846

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,42 @@ public boolean isPkceEnabled() {
5959
* @return The authorization endpoint URL
6060
*/
6161
public String getFormattedAuthorizationEndpoint() {
62-
return authEndpoint != null ? authEndpoint : "https://app.contentstack.com/#!/apps/oauth/authorize";
62+
if (authEndpoint != null) {
63+
return authEndpoint;
64+
}
65+
66+
// Transform hostname similar to JS SDK
67+
String hostname = "app.contentstack.com";
68+
69+
// Handle environment-specific transformations
70+
if (hostname.endsWith("io")) {
71+
hostname = hostname.replace("io", "com");
72+
}
73+
if (hostname.startsWith("api")) {
74+
hostname = hostname.replace("api", "app");
75+
}
76+
77+
return "https://" + hostname + "/#!/apps/oauth/authorize";
6378
}
6479

6580
/**
6681
* Gets the formatted token endpoint URL
6782
* @return The token endpoint URL
6883
*/
6984
public String getTokenEndpoint() {
70-
return tokenEndpoint != null ? tokenEndpoint : "https://app.contentstack.com/apps/oauth/token";
85+
if (tokenEndpoint != null) {
86+
return tokenEndpoint;
87+
}
88+
89+
// Transform for developer hub
90+
String hostname = "developerhub-api.contentstack.com";
91+
92+
// Handle environment-specific transformations
93+
hostname = hostname
94+
.replaceAll("^dev\\d+", "dev") // Replace dev1, dev2, etc. with dev
95+
.replace("io", "com");
96+
97+
return "https://" + hostname + "/apps/oauth/token";
7198
}
7299

73100
/**

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ public String authorize() {
121121
.append("?app_id=").append(URLEncoder.encode(config.getAppId(), "UTF-8"))
122122
.append("&response_type=").append(config.getResponseType())
123123
.append("&client_id=").append(URLEncoder.encode(config.getClientId(), "UTF-8"))
124-
.append("&redirect_uri=").append(URLEncoder.encode(config.getRedirectUri(), "UTF-8"))
125-
.append("&state=").append(URLEncoder.encode(this.state, "UTF-8"));
124+
.append("&redirect_uri=").append(URLEncoder.encode(config.getRedirectUri(), "UTF-8"));
125+
126+
// Add state for CSRF protection (always needed)
127+
if (this.state != null) {
128+
urlBuilder.append("&state=").append(URLEncoder.encode(this.state, "UTF-8"));
129+
}
126130

127131
// Add PKCE parameters if enabled
128132
if (config.isPkceEnabled()) {

0 commit comments

Comments
 (0)