diff --git a/ios/MullvadREST/RESTAccessTokenManager.swift b/ios/MullvadREST/RESTAccessTokenManager.swift index 8b374b88d57a..8c7306e6160b 100644 --- a/ios/MullvadREST/RESTAccessTokenManager.swift +++ b/ios/MullvadREST/RESTAccessTokenManager.swift @@ -62,5 +62,14 @@ extension REST { return operation } + + func invalidateToken(for accountNumber: String) { + operationQueue.addOperation(AsyncBlockOperation(dispatchQueue: dispatchQueue) { [weak self] in + guard let self else { + return + } + self.tokens.removeValue(forKey: accountNumber) + }) + } } } diff --git a/ios/MullvadREST/RESTAccountsProxy.swift b/ios/MullvadREST/RESTAccountsProxy.swift index 29e41e027e15..155306156386 100644 --- a/ios/MullvadREST/RESTAccountsProxy.swift +++ b/ios/MullvadREST/RESTAccountsProxy.swift @@ -94,6 +94,7 @@ extension REST { retryStrategy: RetryStrategy, completion: @escaping CompletionHandler ) -> Cancellable { + let accessTokenProvider = createAuthorizationProvider(accountNumber: accountNumber) let requestHandler = AnyRequestHandler(createURLRequest: { endpoint, authorization in var requestBuilder = try self.requestFactory.createRequestBuilder( endpoint: endpoint, @@ -104,13 +105,14 @@ extension REST { requestBuilder.addValue(accountNumber, forHTTPHeaderField: "Mullvad-Account-Number") return requestBuilder.getRequest() - }, authorizationProvider: createAuthorizationProvider(accountNumber: accountNumber)) + }, authorizationProvider: accessTokenProvider) let responseHandler = AnyResponseHandler { response, data -> ResponseHandlerResult in let statusCode = HTTPStatus(rawValue: response.statusCode) switch statusCode { case let statusCode where statusCode.isSuccess: + accessTokenProvider.invalidateToken(for: accountNumber) return .success(()) default: return .unhandledResponse( diff --git a/ios/MullvadREST/RESTAuthorization.swift b/ios/MullvadREST/RESTAuthorization.swift index 7e81b1f8cb90..919add956c89 100644 --- a/ios/MullvadREST/RESTAuthorization.swift +++ b/ios/MullvadREST/RESTAuthorization.swift @@ -13,6 +13,7 @@ import Operations protocol RESTAuthorizationProvider { func getAuthorization(completion: @escaping (Result) -> Void) -> Cancellable + func invalidateToken(for accountNumber: String) } extension REST { @@ -37,6 +38,10 @@ extension REST { }) } } + + func invalidateToken(for accountNumber: String) { + accessTokenManager.invalidateToken(for: accountNumber) + } } }