Skip to content

Commit 8931a43

Browse files
dbeaver/pro#9031 oracle entra id desktop (#274)
* dbeaver/pro#9031: fix OAuth login * dbeaver/pro#9031: fix code review * dbeaver/pro#9031: fix CPU usage build up --------- Co-authored-by: kseniaguzeeva <112612526+kseniaguzeeva@users.noreply.github.com>
1 parent 7ee5a29 commit 8931a43

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

modules/org.jkiss.utils/src/org/jkiss/utils/oauth/code/OAuthCodeHandler.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public class OAuthCodeHandler implements IOAuthHandler {
7272
protected int timeout;
7373
@Nullable
7474
protected String state;
75+
76+
@Nullable
77+
protected final String scope;
78+
7579
@Nullable
7680
protected String codeChallenge;
7781

@@ -93,6 +97,29 @@ public OAuthCodeHandler(
9397
@NotNull String callbackEndpoint,
9498
@NotNull String redirectUri,
9599
int callbackPort
100+
) {
101+
this(clientId, secretId, authUrl, tokenURL, callbackEndpoint, redirectUri, callbackPort, null);
102+
}
103+
104+
/**
105+
* Constructs an OAuthHandler with required parameters.
106+
*
107+
* @param clientId the OAuth client ID
108+
* @param secretId the OAuth client secret (nullable for PKCE-only flows)
109+
* @param authUrl the authorization endpoint URL
110+
* @param tokenURL the token exchange endpoint URL
111+
* @param callbackPort the port on which the temporary server will listen for the callback
112+
* @param scope requested scope
113+
*/
114+
public OAuthCodeHandler(
115+
@NotNull String clientId,
116+
@Nullable String secretId,
117+
@NotNull String authUrl,
118+
@NotNull String tokenURL,
119+
@NotNull String callbackEndpoint,
120+
@NotNull String redirectUri,
121+
int callbackPort,
122+
@Nullable String scope
96123
) {
97124
this.clientId = clientId;
98125
this.secretId = secretId;
@@ -101,9 +128,9 @@ public OAuthCodeHandler(
101128
this.callbackEndpoint = callbackEndpoint;
102129
this.callbackPort = callbackPort;
103130
this.redirectUri = redirectUri;
131+
this.scope = scope;
104132
}
105133

106-
107134
/**
108135
* Sets the timeout (in seconds) to wait for the OAuth callback response.
109136
*
@@ -277,6 +304,9 @@ protected String buildAuthUrl() throws IOException {
277304
if (codeChallenge != null) {
278305
builder.withCodeChallenge(codeChallenge);
279306
}
307+
if (CommonUtils.isNotEmpty(scope)) {
308+
builder.withScope(scope);
309+
}
280310
return builder.build();
281311
}
282312

@@ -298,6 +328,8 @@ public static class OAuthCodeHandlerBuilder<T extends OAuthCodeHandler> {
298328
protected String redirectUri;
299329
protected int callbackPort = 0;
300330

331+
protected String scope;
332+
301333
protected int timeout = OAuthConstants.AUTH_DEFAULT_SSO_TIMEOUT;
302334
protected String callbackEndpoint = OAuthConstants.DEFAULT_CALLBACK_ENDPOINT;
303335
protected String state;
@@ -343,6 +375,12 @@ public OAuthCodeHandlerBuilder<T> withRedirectUri(@NotNull String redirectUri) {
343375
return this;
344376
}
345377

378+
@NotNull
379+
public OAuthCodeHandlerBuilder<T> withScope(@NotNull String scope) {
380+
this.scope = scope;
381+
return this;
382+
}
383+
346384
@NotNull
347385
public T build() {
348386
if (CommonUtils.isEmpty(clientId)) {
@@ -370,7 +408,7 @@ public T build() {
370408
@NotNull
371409
protected T createOAuthCodeHandler() {
372410
//noinspection unchecked
373-
return (T) new OAuthCodeHandler(clientId, secretId, authUrl, tokenURL, callbackEndpoint, redirectUri, callbackPort);
411+
return (T) new OAuthCodeHandler(clientId, secretId, authUrl, tokenURL, callbackEndpoint, redirectUri, callbackPort, scope);
374412
}
375413
}
376414
}

modules/org.jkiss.utils/src/org/jkiss/utils/oauth/code/OAuthCodeResponseHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class OAuthCodeResponseHandler implements IOAuthCodeResponseHandler {
4646
@NotNull
4747
private final String callbackEndpoint;
4848

49+
private volatile boolean notShutDown;
50+
4951
/**
5052
* Creates a new instance of the response handler.
5153
*
@@ -83,6 +85,7 @@ public void initServer() throws IOException {
8385
*/
8486
@Override
8587
public Future<String> requestCode() {
88+
notShutDown = true;
8689
return executor.submit(() -> {
8790
AtomicReference<String> result = new AtomicReference<>();
8891
AtomicBoolean hasErrors = new AtomicBoolean(false);
@@ -109,7 +112,7 @@ public Future<String> requestCode() {
109112
exchange.close();
110113
}
111114
);
112-
while (result.get() == null) {
115+
while (result.get() == null && notShutDown) {
113116
Thread.onSpinWait();
114117
}
115118
httpServer.removeContext(callbackEndpoint);
@@ -146,6 +149,7 @@ public void close() throws IOException {
146149
httpServer.stop(0);
147150
}
148151
executor.shutdown();
152+
notShutDown = false;
149153
}
150154

151155
@NotNull

0 commit comments

Comments
 (0)