Skip to content

Commit 56f38d1

Browse files
author
Brett Chaldecott
committed
fix: add proper resource management for HTTP connection in createPortalUrl
- Wrap HTTP connection operations in try-finally block - Ensure connection.disconnect() is called to prevent resource leaks - Improves connection lifecycle management in portal URL generation
1 parent 5610c3c commit 56f38d1

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

kinde-core/src/main/java/com/kinde/session/KindeClientSessionImpl.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,29 @@ public AuthorizationUrl generatePortalUrl(String domain, String returnUrl, Strin
210210
log.info("Generating portal URL: {}", url);
211211

212212
HttpURLConnection conn = openConnection(url);
213-
conn.setRequestMethod("GET");
214-
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
215-
conn.setRequestProperty("Accept", "application/json");
216-
217-
int responseCode = conn.getResponseCode();
218-
if (responseCode != 200) {
219-
log.error("Failed to fetch profile URL: {} {}", responseCode, conn.getResponseMessage());
220-
throw new KindeClientSessionException("Failed to fetch profile URL: " + responseCode + " " + conn.getResponseMessage());
221-
}
213+
try {
214+
conn.setRequestMethod("GET");
215+
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
216+
conn.setRequestProperty("Accept", "application/json");
217+
218+
int responseCode = conn.getResponseCode();
219+
if (responseCode != 200) {
220+
log.error("Failed to fetch profile URL: {} {}", responseCode, conn.getResponseMessage());
221+
throw new KindeClientSessionException("Failed to fetch profile URL: " + responseCode + " " + conn.getResponseMessage());
222+
}
222223

223-
String responseBody;
224-
try (InputStream is = conn.getInputStream();
225-
Scanner s = new Scanner(is).useDelimiter("\\A")) {
226-
responseBody = s.hasNext() ? s.next() : "";
227-
}
224+
String responseBody;
225+
try (InputStream is = conn.getInputStream();
226+
Scanner s = new Scanner(is).useDelimiter("\\A")) {
227+
responseBody = s.hasNext() ? s.next() : "";
228+
}
228229

229-
String portalUrl = getPortalUrl(responseBody)
230-
.orElseThrow(() -> new KindeClientSessionException("Failed to extract portal URL from response"));
231-
return new AuthorizationUrl(new URL(portalUrl), null);
230+
String portalUrl = getPortalUrl(responseBody)
231+
.orElseThrow(() -> new KindeClientSessionException("Failed to extract portal URL from response"));
232+
return new AuthorizationUrl(new URL(portalUrl), null);
233+
} finally {
234+
conn.disconnect();
235+
}
232236
}
233237

234238
private static Optional<String> getPortalUrl(String responseBody) {

0 commit comments

Comments
 (0)