|
31 | 31 | import org.eclipse.jetty.util.UrlEncoded;
|
32 | 32 | import org.openhab.core.auth.client.oauth2.AccessTokenRefreshListener;
|
33 | 33 | import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
|
| 34 | +import org.openhab.core.auth.client.oauth2.DeviceCodeResponseDTO; |
34 | 35 | import org.openhab.core.auth.client.oauth2.OAuthClientService;
|
35 | 36 | import org.openhab.core.auth.client.oauth2.OAuthException;
|
36 | 37 | import org.openhab.core.auth.client.oauth2.OAuthResponseException;
|
@@ -77,6 +78,7 @@ public class OAuthClientServiceImpl implements OAuthClientService {
|
77 | 78 | private PersistedParams persistedParams = new PersistedParams();
|
78 | 79 |
|
79 | 80 | private @Nullable Fields extraAuthFields = null;
|
| 81 | + private @Nullable OAuthConnectorRFC8628 oAuthConnectorRFC8628 = null; |
80 | 82 |
|
81 | 83 | private volatile boolean closed = false;
|
82 | 84 |
|
@@ -392,8 +394,16 @@ public void remove() throws OAuthException {
|
392 | 394 | public void close() {
|
393 | 395 | closed = true;
|
394 | 396 | storeHandler = null;
|
395 |
| - |
396 | 397 | 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; |
397 | 407 | }
|
398 | 408 |
|
399 | 409 | @Override
|
@@ -440,4 +450,34 @@ public OAuthClientService withGsonBuilder(GsonBuilder gsonBuilder) {
|
440 | 450 |
|
441 | 451 | return clientService;
|
442 | 452 | }
|
| 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 | + } |
443 | 483 | }
|
0 commit comments