From de1daf8251ca0d929be4743706c0e8091c147eeb Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Wed, 10 Jan 2024 13:41:38 +0100 Subject: [PATCH] feat: Convert refresh token to infinite token if needed --- Sources/InfomaniakLogin/InfomaniakLoginError.swift | 3 +++ .../Networking/InfomaniakNetworkLogin.swift | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Sources/InfomaniakLogin/InfomaniakLoginError.swift b/Sources/InfomaniakLogin/InfomaniakLoginError.swift index cbadd7d..ea853b8 100644 --- a/Sources/InfomaniakLogin/InfomaniakLoginError.swift +++ b/Sources/InfomaniakLogin/InfomaniakLoginError.swift @@ -25,6 +25,7 @@ public enum InfomaniakLoginError: LocalizedError { case navigationCancelled(HTTPStatusCode?, URL?) case invalidAccessToken(AccessToken?) case invalidUrl + case noRefreshToken public var errorDescription: String? { switch self { @@ -38,6 +39,8 @@ public enum InfomaniakLoginError: LocalizedError { return "Invalid access token" case .invalidUrl: return "Invalid url" + case .noRefreshToken: + return "No refresh token" } } } diff --git a/Sources/InfomaniakLogin/Networking/InfomaniakNetworkLogin.swift b/Sources/InfomaniakLogin/Networking/InfomaniakNetworkLogin.swift index cd761a5..5a86a42 100644 --- a/Sources/InfomaniakLogin/Networking/InfomaniakNetworkLogin.swift +++ b/Sources/InfomaniakLogin/Networking/InfomaniakNetworkLogin.swift @@ -57,13 +57,23 @@ public class InfomaniakNetworkLogin: InfomaniakNetworkLoginable { } public func refreshToken(token: ApiToken, completion: @escaping (ApiToken?, Error?) -> Void) { + guard let refreshToken = token.refreshToken else { + completion(nil, InfomaniakLoginError.noRefreshToken) + return + } + var request = URLRequest(url: tokenApiURL) - let parameterDictionary: [String: Any] = [ + var parameterDictionary: [String: Any] = [ "grant_type": "refresh_token", "client_id": config.clientId, - "refresh_token": token.refreshToken + "refresh_token": refreshToken ] + + if config.accessType == .none { + parameterDictionary["duration"] = "infinite" + } + request.httpMethod = "POST" request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.httpBody = parameterDictionary.percentEncoded()