Skip to content

Commit

Permalink
feat: Convert refresh token to infinite token if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jan 16, 2024
1 parent 360329e commit de1daf8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Sources/InfomaniakLogin/InfomaniakLoginError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,6 +39,8 @@ public enum InfomaniakLoginError: LocalizedError {
return "Invalid access token"
case .invalidUrl:
return "Invalid url"
case .noRefreshToken:
return "No refresh token"
}
}
}
14 changes: 12 additions & 2 deletions Sources/InfomaniakLogin/Networking/InfomaniakNetworkLogin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit de1daf8

Please sign in to comment.