Skip to content

Commit 2eec58a

Browse files
authored
OAuth RFC-8628 Device Code Grant Flow (openhab#4632)
Signed-off-by: Andrew Fiddian-Green <[email protected]>
1 parent 8de8b4d commit 2eec58a

File tree

9 files changed

+1209
-13
lines changed

9 files changed

+1209
-13
lines changed

bundles/org.openhab.core.auth.oauth2client/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthClientServiceImpl.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.eclipse.jetty.util.UrlEncoded;
3232
import org.openhab.core.auth.client.oauth2.AccessTokenRefreshListener;
3333
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
34+
import org.openhab.core.auth.client.oauth2.DeviceCodeResponseDTO;
3435
import org.openhab.core.auth.client.oauth2.OAuthClientService;
3536
import org.openhab.core.auth.client.oauth2.OAuthException;
3637
import org.openhab.core.auth.client.oauth2.OAuthResponseException;
@@ -77,6 +78,7 @@ public class OAuthClientServiceImpl implements OAuthClientService {
7778
private PersistedParams persistedParams = new PersistedParams();
7879

7980
private @Nullable Fields extraAuthFields = null;
81+
private @Nullable OAuthConnectorRFC8628 oAuthConnectorRFC8628 = null;
8082

8183
private volatile boolean closed = false;
8284

@@ -392,8 +394,16 @@ public void remove() throws OAuthException {
392394
public void close() {
393395
closed = true;
394396
storeHandler = null;
395-
396397
logger.debug("closing oauth client, handle: {}", handle);
398+
closeOAuthConnectorRFC8628();
399+
}
400+
401+
private synchronized void closeOAuthConnectorRFC8628() {
402+
OAuthConnectorRFC8628 connector = this.oAuthConnectorRFC8628;
403+
if (connector != null) {
404+
connector.close();
405+
}
406+
this.oAuthConnectorRFC8628 = null;
397407
}
398408

399409
@Override
@@ -440,4 +450,34 @@ public OAuthClientService withGsonBuilder(GsonBuilder gsonBuilder) {
440450

441451
return clientService;
442452
}
453+
454+
@Override
455+
public @Nullable DeviceCodeResponseDTO getDeviceCodeResponse() throws OAuthException {
456+
closeOAuthConnectorRFC8628();
457+
458+
if (persistedParams.tokenUrl == null) {
459+
throw new OAuthException("Missing access token request url");
460+
}
461+
if (persistedParams.authorizationUrl == null) {
462+
throw new OAuthException("Missing device code request url");
463+
}
464+
if (persistedParams.clientId == null) {
465+
throw new OAuthException("Missing client id");
466+
}
467+
if (persistedParams.scope == null) {
468+
throw new OAuthException("Missing scope");
469+
}
470+
471+
OAuthConnectorRFC8628 connector = new OAuthConnectorRFC8628(this, handle, storeHandler, httpClientFactory,
472+
gsonBuilder, persistedParams.tokenUrl, persistedParams.authorizationUrl, persistedParams.clientId,
473+
persistedParams.scope);
474+
475+
oAuthConnectorRFC8628 = connector;
476+
return connector.getDeviceCodeResponse();
477+
}
478+
479+
@Override
480+
public void notifyAccessTokenResponse(AccessTokenResponse atr) {
481+
accessTokenRefreshListeners.forEach(l -> l.onAccessTokenResponse(atr));
482+
}
443483
}

bundles/org.openhab.core.auth.oauth2client/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthConnector.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ public class OAuthConnector {
6262

6363
private static final String HTTP_CLIENT_CONSUMER_NAME = "OAuthConnector";
6464

65-
private final HttpClientFactory httpClientFactory;
65+
protected final HttpClientFactory httpClientFactory;
6666

6767
private final @Nullable Fields extraFields;
6868

6969
private final Logger logger = LoggerFactory.getLogger(OAuthConnector.class);
70-
private final Gson gson;
70+
71+
protected final Gson gson;
7172

7273
public OAuthConnector(HttpClientFactory httpClientFactory) {
7374
this(httpClientFactory, null, new GsonBuilder());
@@ -371,7 +372,7 @@ private AccessTokenResponse doRequest(final String grantType, HttpClient httpCli
371372
* @throws OAuthException If any exception is thrown while starting the http client.
372373
* @see org.openhab.core.io.net.http.ExtensibleTrustManager
373374
*/
374-
private HttpClient createHttpClient(String tokenUrl) throws OAuthException {
375+
protected HttpClient createHttpClient(String tokenUrl) throws OAuthException {
375376
HttpClient httpClient = httpClientFactory.createHttpClient(HTTP_CLIENT_CONSUMER_NAME);
376377
if (!httpClient.isStarted()) {
377378
try {
@@ -383,7 +384,7 @@ private HttpClient createHttpClient(String tokenUrl) throws OAuthException {
383384
return httpClient;
384385
}
385386

386-
private void shutdownQuietly(@Nullable HttpClient httpClient) {
387+
protected void shutdownQuietly(@Nullable HttpClient httpClient) {
387388
try {
388389
if (httpClient != null) {
389390
httpClient.stop();

0 commit comments

Comments
 (0)