[nest] Remove the access token when the thing is removed (#14940)

Related to #14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-05-13 19:06:41 +02:00 committed by GitHub
parent 8a9ac6eccc
commit c10f4f4051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 4 deletions

View File

@ -144,7 +144,9 @@ public class PubSubAPI {
private final Logger logger = LoggerFactory.getLogger(PubSubAPI.class);
private final HttpClient httpClient;
private final OAuthFactory oAuthFactory;
private final OAuthClientService oAuthService;
private final String oAuthServiceHandleId;
private final String projectId;
private final ScheduledThreadPoolExecutor scheduler;
private final Map<String, Set<PubSubSubscriptionListener>> subscriptionListeners = new HashMap<>();
@ -153,14 +155,21 @@ public class PubSubAPI {
String clientId, String clientSecret) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.projectId = projectId;
this.oAuthService = oAuthFactory.createOAuthClientService(String.format(PUBSUB_HANDLE_FORMAT, ownerId),
TOKEN_URL, AUTH_URL, clientId, clientSecret, PUBSUB_SCOPE, false);
this.oAuthFactory = oAuthFactory;
this.oAuthServiceHandleId = String.format(PUBSUB_HANDLE_FORMAT, ownerId);
this.oAuthService = oAuthFactory.createOAuthClientService(oAuthServiceHandleId, TOKEN_URL, AUTH_URL, clientId,
clientSecret, PUBSUB_SCOPE, false);
scheduler = new ScheduledThreadPoolExecutor(3, new NamedThreadFactory(ownerId, true));
}
public void dispose() {
subscriptionListeners.clear();
scheduler.shutdownNow();
oAuthFactory.ungetOAuthService(oAuthServiceHandleId);
}
public void deleteOAuthServiceAndAccessToken() {
oAuthFactory.deleteServiceAndAccessToken(oAuthServiceHandleId);
}
public void authorizeClient(String authorizationCode) throws InvalidPubSubAuthorizationCodeException, IOException {

View File

@ -84,7 +84,9 @@ public class SDMAPI {
private final Logger logger = LoggerFactory.getLogger(SDMAPI.class);
private final HttpClient httpClient;
private final OAuthFactory oAuthFactory;
private final OAuthClientService oAuthService;
private final String oAuthServiceHandleId;
private final String projectId;
private final Set<SDMAPIRequestListener> requestListeners = ConcurrentHashMap.newKeySet();
@ -92,13 +94,20 @@ public class SDMAPI {
public SDMAPI(HttpClientFactory httpClientFactory, OAuthFactory oAuthFactory, String ownerId, String projectId,
String clientId, String clientSecret) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.oAuthService = oAuthFactory.createOAuthClientService(String.format(SDM_HANDLE_FORMAT, ownerId), TOKEN_URL,
AUTH_URL, clientId, clientSecret, SDM_SCOPE, false);
this.oAuthFactory = oAuthFactory;
this.oAuthServiceHandleId = String.format(SDM_HANDLE_FORMAT, ownerId);
this.oAuthService = oAuthFactory.createOAuthClientService(oAuthServiceHandleId, TOKEN_URL, AUTH_URL, clientId,
clientSecret, SDM_SCOPE, false);
this.projectId = projectId;
}
public void dispose() {
requestListeners.clear();
oAuthFactory.ungetOAuthService(oAuthServiceHandleId);
}
public void deleteOAuthServiceAndAccessToken() {
oAuthFactory.deleteServiceAndAccessToken(oAuthServiceHandleId);
}
public void authorizeClient(String authorizationCode) throws InvalidSDMAuthorizationCodeException, IOException {

View File

@ -263,6 +263,19 @@ public class SDMAccountHandler extends BaseBridgeHandler {
}
}
@Override
public void handleRemoval() {
PubSubAPI localPubSubAPI = pubSubAPI;
if (localPubSubAPI != null) {
localPubSubAPI.deleteOAuthServiceAndAccessToken();
}
SDMAPI localSDMAPI = sdmAPI;
if (localSDMAPI != null) {
localSDMAPI.deleteOAuthServiceAndAccessToken();
}
super.handleRemoval();
}
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return List.of(SDMDiscoveryService.class);