From 360329e6940615cd2bc389d72f2bd8506f0c2442 Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Wed, 10 Jan 2024 11:50:39 +0100 Subject: [PATCH] feat: Token without accessType --- Sources/InfomaniakLogin/Config.swift | 4 ++-- Sources/InfomaniakLogin/InfomaniakLogin.swift | 5 ++++- Sources/InfomaniakLogin/Model/ApiToken.swift | 22 ++++++++++++------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Sources/InfomaniakLogin/Config.swift b/Sources/InfomaniakLogin/Config.swift index 373ca7e..07a7e8b 100644 --- a/Sources/InfomaniakLogin/Config.swift +++ b/Sources/InfomaniakLogin/Config.swift @@ -31,7 +31,7 @@ public extension InfomaniakLogin { let loginURL: URL let redirectURI: String let responseType: ResponseType - let accessType: AccessType + let accessType: AccessType? let hashMode: String let hashModeShort: String @@ -50,7 +50,7 @@ public extension InfomaniakLogin { loginURL: URL = URL(string: "https://login.infomaniak.com/")!, redirectURI: String = "\(Bundle.main.bundleIdentifier ?? "")://oauth2redirect", responseType: ResponseType = .code, - accessType: AccessType = .offline, + accessType: AccessType? = .offline, hashMode: String = "SHA-256", hashModeShort: String = "S256" ) { diff --git a/Sources/InfomaniakLogin/InfomaniakLogin.swift b/Sources/InfomaniakLogin/InfomaniakLogin.swift index 9c168d6..71b846f 100644 --- a/Sources/InfomaniakLogin/InfomaniakLogin.swift +++ b/Sources/InfomaniakLogin/InfomaniakLogin.swift @@ -218,13 +218,16 @@ public class InfomaniakLogin: InfomaniakLoginable, InfomaniakTokenable { urlComponents?.path = "/authorize" urlComponents?.queryItems = [ URLQueryItem(name: "response_type", value: config.responseType.rawValue), - URLQueryItem(name: "access_type", value: config.accessType.rawValue), URLQueryItem(name: "client_id", value: config.clientId), URLQueryItem(name: "redirect_uri", value: config.redirectURI), URLQueryItem(name: "code_challenge_method", value: codeChallengeMethod), URLQueryItem(name: "code_challenge", value: codeChallenge) ] + if let accessType = config.accessType?.rawValue { + urlComponents?.queryItems?.append(URLQueryItem(name: "access_type", value: accessType)) + } + if hideCreateAccountButton { urlComponents?.queryItems?.append(URLQueryItem(name: "hide_create_account", value: "")) } diff --git a/Sources/InfomaniakLogin/Model/ApiToken.swift b/Sources/InfomaniakLogin/Model/ApiToken.swift index 9941ddc..19041d0 100644 --- a/Sources/InfomaniakLogin/Model/ApiToken.swift +++ b/Sources/InfomaniakLogin/Model/ApiToken.swift @@ -18,12 +18,12 @@ import Foundation public class ApiToken: NSObject, Codable { public let accessToken: String - public let expiresIn: Int - public let refreshToken: String + public let refreshToken: String? public let scope: String public let tokenType: String public let userId: Int - public let expirationDate: Date + public let expiresIn: Int? + public let expirationDate: Date? enum CodingKeys: String, CodingKey { case accessToken = "access_token" @@ -38,14 +38,19 @@ public class ApiToken: NSObject, Codable { public required init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) accessToken = try values.decode(String.self, forKey: .accessToken) - expiresIn = try values.decode(Int.self, forKey: .expiresIn) - refreshToken = try values.decode(String.self, forKey: .refreshToken) + let maybeExpiresIn = try values.decodeIfPresent(Int.self, forKey: .expiresIn) + expiresIn = maybeExpiresIn + refreshToken = try values.decodeIfPresent(String.self, forKey: .refreshToken) scope = try values.decode(String.self, forKey: .scope) tokenType = try values.decode(String.self, forKey: .tokenType) userId = try values.decode(Int.self, forKey: .userId) - let newExpirationDate = Date().addingTimeInterval(TimeInterval(Double(expiresIn))) - expirationDate = try values.decodeIfPresent(Date.self, forKey: .expirationDate) ?? newExpirationDate + if let maybeExpiresIn { + let newExpirationDate = Date().addingTimeInterval(TimeInterval(Double(maybeExpiresIn))) + expirationDate = try values.decodeIfPresent(Date.self, forKey: .expirationDate) ?? newExpirationDate + } else { + expirationDate = nil + } } public init( @@ -75,7 +80,8 @@ public extension ApiToken { } var truncatedRefreshToken: String { - truncateToken(refreshToken) + guard let refreshToken else { return "" } + return truncateToken(refreshToken) } internal func truncateToken(_ token: String) -> String {