Skip to content

Commit

Permalink
feat: Token without accessType
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jan 16, 2024
1 parent e5ad694 commit 360329e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Sources/InfomaniakLogin/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
) {
Expand Down
5 changes: 4 additions & 1 deletion Sources/InfomaniakLogin/InfomaniakLogin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""))
}
Expand Down
22 changes: 14 additions & 8 deletions Sources/InfomaniakLogin/Model/ApiToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 360329e

Please sign in to comment.