Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</parent>

<artifactId>coze-api</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>

<scm>
<connection>scm:git:git://github.com/coze-dev/coze-java.git</connection>
Expand Down
18 changes: 15 additions & 3 deletions api/src/main/java/com/coze/openapi/service/auth/OAuthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.coze.openapi.service.utils.Utils;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.jsonwebtoken.lang.Strings;
import io.reactivex.Single;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
Expand All @@ -45,6 +46,7 @@ public abstract class OAuthClient {
protected final String clientSecret;
protected final String clientID;
protected final String baseURL;
protected final String wwwURL;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Validate wwwURL construction

While the URL domain separation is correct, the current implementation using simple string replacement could be fragile.

Consider adding validation and using URL parsing:

-    this.wwwURL = Strings.replace(baseURL, "api.", "www.");
+    try {
+        java.net.URL url = new java.net.URL(baseURL);
+        String host = url.getHost().replace("api.", "www.");
+        this.wwwURL = new java.net.URL(url.getProtocol(), host, url.getPort(), "").toString();
+    } catch (java.net.MalformedURLException e) {
+        throw new IllegalArgumentException("Invalid base URL: " + baseURL, e);
+    }

Also applies to: 59-59

protected final CozeAuthAPI api;
protected final ExecutorService executorService;
protected final String hostName;
Expand All @@ -54,6 +56,11 @@ protected OAuthClient(OAuthBuilder<?> builder) {
this.clientSecret = builder.clientSecret;
this.clientID = builder.clientID;
this.baseURL = builder.baseURL;
if (builder.wwwURL != null) {
this.wwwURL = builder.wwwURL;
}else {
this.wwwURL = Strings.replace(baseURL, "api.", "www.");
}
if (this.baseURL != null && !this.baseURL.isEmpty()) {
try {
java.net.URL url = new java.net.URL(this.baseURL);
Expand Down Expand Up @@ -121,10 +128,9 @@ private String _getOAuthURL(
params.put("code_challenge_method", codeChallengeMethod);
}

String uri = baseURL + "/api/permission/oauth2/authorize";
String uri = wwwURL + "/api/permission/oauth2/authorize";
if (workspaceID != null) {
uri =
baseURL + String.format("/api/permission/oauth2/workspace_id/%s/authorize", workspaceID);
uri = wwwURL + String.format("/api/permission/oauth2/workspace_id/%s/authorize", workspaceID);
}

String queryString =
Expand Down Expand Up @@ -251,6 +257,7 @@ public abstract static class OAuthBuilder<T extends OAuthBuilder<T>> {
protected String clientID;
protected String clientSecret;
protected String baseURL;
protected String wwwURL;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Add test coverage for wwwURL builder methods

The codebase analysis confirms that wwwURL builder functionality lacks test coverage. Existing OAuth client tests demonstrate thorough testing patterns for other builder properties, but wwwURL-specific tests are missing. Please add test cases to verify:

  • Setting custom wwwURL
  • Fallback to baseURL conversion
  • Invalid URL handling
🔗 Analysis chain

Add test coverage for wwwURL builder methods.

The new builder methods for wwwURL are not covered by tests. Please add test cases to verify:

  • Setting custom wwwURL
  • Fallback to baseURL conversion
  • Invalid URL handling

Also applies to: 285-289

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for existing test coverage of wwwURL functionality

# Search for test files that might contain wwwURL-related tests
echo "Searching for existing test files..."
fd -e java -e kt test

# Search for wwwURL usage in test files
echo -e "\nSearching for wwwURL test coverage..."
rg -l "wwwURL" $(fd -e java -e kt test)

Length of output: 4911


Script:

#!/bin/bash
# Check for builder pattern usage in auth tests
echo "Checking builder usage in auth tests..."
rg -A 5 -B 5 "\.builder\(\)" api/src/test/java/com/coze/openapi/service/auth/

# Check for any indirect wwwURL testing through setters
echo -e "\nChecking for indirect wwwURL testing..."
rg -A 5 -B 5 "\.setWww" api/src/test/java/com/coze/openapi/service/auth/

Length of output: 338


Script:

#!/bin/bash
# Check test structure and initialization patterns
echo "Analyzing OAuthClient test implementations..."
rg -l "OAuthClient" api/src/test/java/com/coze/openapi/service/auth/ | xargs cat

# Broader search for builder-related patterns
echo -e "\nChecking for builder patterns..."
rg "new\s+.*Builder|builder\s*\(\)" api/src/test/java/com/coze/openapi/service/auth/

Length of output: 30942

protected int readTimeout;
protected int connectTimeout;
protected OkHttpClient client;
Expand All @@ -276,6 +283,11 @@ public T baseURL(String baseURL) {
return self();
}

public T wwwURL(String wwwURL) {
this.wwwURL = wwwURL;
return self();
}

public T readTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return self();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Response intercept(Chain chain) throws IOException {
return chain.proceed(request);
}

public static final String VERSION = "0.2.1";
public static final String VERSION = "0.2.2";
private static final ObjectMapper objectMapper = new ObjectMapper();

/** 获取操作系统版本 */
Expand Down
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<dependency>
<groupId>com.coze</groupId>
<artifactId>coze-api</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
</dependencies>
</project>
4 changes: 2 additions & 2 deletions example/src/main/java/example/auth/WebOAuthExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public static void main(String[] args) {
.build();

// Generate the authorization link and direct the user to open it.
String oauthURL = oauth.getOAuthURL(redirectURI, null);
String oauthURL = oauth.getOAuthURL(redirectURI, "state");
System.out.println(oauthURL);

/*
* The space permissions for which the Access Token is granted can be specified. As following codes:
* */
oauthURL = oauth.getOAuthURL(redirectURI, null, "workspaceID");
oauthURL = oauth.getOAuthURL(redirectURI, "state", "workspaceID");
System.out.println(oauthURL);

/*
Expand Down
Loading