Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAuth RFC-8628 Device Code Grant Flow (deadline 2025-03-15) #4632

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.jetty.util.UrlEncoded;
import org.openhab.core.auth.client.oauth2.AccessTokenRefreshListener;
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
import org.openhab.core.auth.client.oauth2.DeviceCodeResponse;
import org.openhab.core.auth.client.oauth2.OAuthClientService;
import org.openhab.core.auth.client.oauth2.OAuthException;
import org.openhab.core.auth.client.oauth2.OAuthResponseException;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class OAuthClientServiceImpl implements OAuthClientService {
private PersistedParams persistedParams = new PersistedParams();

private @Nullable Fields extraAuthFields = null;
private @Nullable OAuthConnectorRFC8628 oAuthConnectorRFC8628 = null;

private volatile boolean closed = false;

Expand Down Expand Up @@ -392,8 +394,16 @@ public void remove() throws OAuthException {
public void close() {
closed = true;
storeHandler = null;

logger.debug("closing oauth client, handle: {}", handle);
closeOAuthConnectorRFC8628();
}

private synchronized void closeOAuthConnectorRFC8628() {
OAuthConnectorRFC8628 connector = this.oAuthConnectorRFC8628;
if (connector != null) {
connector.close();
}
this.oAuthConnectorRFC8628 = null;
}

@Override
Expand Down Expand Up @@ -440,4 +450,34 @@ public OAuthClientService withGsonBuilder(GsonBuilder gsonBuilder) {

return clientService;
}

@Override
public @Nullable DeviceCodeResponse getDeviceCodeResponse() throws OAuthException {
closeOAuthConnectorRFC8628();

if (persistedParams.tokenUrl == null) {
throw new OAuthException("Missing access token request url");
}
if (persistedParams.authorizationUrl == null) {
throw new OAuthException("Missing device code request url");
}
if (persistedParams.clientId == null) {
throw new OAuthException("Missing client id");
}
if (persistedParams.scope == null) {
throw new OAuthException("Missing scope");
}

OAuthConnectorRFC8628 connector = new OAuthConnectorRFC8628(this, handle, storeHandler, httpClientFactory,
gsonBuilder, persistedParams.tokenUrl, persistedParams.authorizationUrl, persistedParams.clientId,
persistedParams.scope);

oAuthConnectorRFC8628 = connector;
return connector.getDeviceCodeResponse();
}

@Override
public void notifyAccessTokenResponse(AccessTokenResponse atr) {
accessTokenRefreshListeners.forEach(l -> l.onAccessTokenResponse(atr));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public class OAuthConnector {

private static final String HTTP_CLIENT_CONSUMER_NAME = "OAuthConnector";

private final HttpClientFactory httpClientFactory;
protected final HttpClientFactory httpClientFactory;

private final @Nullable Fields extraFields;

private final Logger logger = LoggerFactory.getLogger(OAuthConnector.class);
private final Gson gson;

protected final Gson gson;

public OAuthConnector(HttpClientFactory httpClientFactory) {
this(httpClientFactory, null, new GsonBuilder());
Expand Down Expand Up @@ -371,7 +372,7 @@ private AccessTokenResponse doRequest(final String grantType, HttpClient httpCli
* @throws OAuthException If any exception is thrown while starting the http client.
* @see org.openhab.core.io.net.http.ExtensibleTrustManager
*/
private HttpClient createHttpClient(String tokenUrl) throws OAuthException {
protected HttpClient createHttpClient(String tokenUrl) throws OAuthException {
HttpClient httpClient = httpClientFactory.createHttpClient(HTTP_CLIENT_CONSUMER_NAME);
if (!httpClient.isStarted()) {
try {
Expand All @@ -383,7 +384,7 @@ private HttpClient createHttpClient(String tokenUrl) throws OAuthException {
return httpClient;
}

private void shutdownQuietly(@Nullable HttpClient httpClient) {
protected void shutdownQuietly(@Nullable HttpClient httpClient) {
try {
if (httpClient != null) {
httpClient.stop();
Expand Down
Loading