import Foundation import JSONCodable /// Token Secret open class TokenSecret: Codable { enum CodingKeys: String, CodingKey { case id = "$id" case createdAt = "$createdAt" case userId = "userId" case secret = "secret" case expire = "expire" case phrase = "phrase" } /// Token ID. public let id: String /// Token creation date in ISO 8601 format. public let createdAt: String /// User ID. public let userId: String /// Token secret key. Exchange it for a session with the create session endpoint. public let secret: String /// Token expiration date in ISO 8601 format. public let expire: String /// Security phrase of a token. Empty if security phrase was not requested when creating a token. It includes randomly generated phrase which is also sent in the external resource such as email. public let phrase: String init( id: String, createdAt: String, userId: String, secret: String, expire: String, phrase: String ) { self.id = id self.createdAt = createdAt self.userId = userId self.secret = secret self.expire = expire self.phrase = phrase } public required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.id = try container.contains(.id) ? container.decode(String.self, forKey: .id) : "" self.createdAt = try container.contains(.createdAt) ? container.decode(String.self, forKey: .createdAt) : "" self.userId = try container.contains(.userId) ? container.decode(String.self, forKey: .userId) : "" self.secret = try container.contains(.secret) ? container.decode(String.self, forKey: .secret) : "" self.expire = try container.contains(.expire) ? container.decode(String.self, forKey: .expire) : "" self.phrase = try container.contains(.phrase) ? container.decode(String.self, forKey: .phrase) : "" } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(createdAt, forKey: .createdAt) try container.encode(userId, forKey: .userId) try container.encode(secret, forKey: .secret) try container.encode(expire, forKey: .expire) try container.encode(phrase, forKey: .phrase) } public func toMap() -> [String: Any] { return [ "$id": id as Any, "$createdAt": createdAt as Any, "userId": userId as Any, "secret": secret as Any, "expire": expire as Any, "phrase": phrase as Any, ] } public static func from(map: [String: Any]) -> TokenSecret { let defaults: [String: Any] = [ "$id": "", "$createdAt": "", "userId": "", "secret": "", "expire": "", "phrase": "", ] let map = defaults.merging(map) { _, new in new } return TokenSecret( id: map["$id"] as! String, createdAt: map["$createdAt"] as! String, userId: map["userId"] as! String, secret: map["secret"] as! String, expire: map["expire"] as! String, phrase: map["phrase"] as! String ) } }