import ATProtoKit import Darwin import Foundation import OSLog import Security import UIKit extension ATProtoBlueskyService { static func expiration(of jwt: String) -> Date? { let components = jwt.split(separator: ".") guard components.count > 1 else { return nil } var payload = String(components[1]).replacingOccurrences(of: "-", with: "+").replacingOccurrences( of: "_", with: "/") payload += String(repeating: "=", count: (4 - payload.count % 4) % 4) guard let data = Data(base64Encoded: payload), let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any], let expiry = object["exp"] as? TimeInterval else { return nil } return Date(timeIntervalSince1970: expiry) } static func pdsURL(from didDocument: Any?) -> URL? { guard let document = didDocument as? [String: Any], let services = document["service"] as? [[String: Any]] else { return nil } for service in services { let identifier = service["id"] as? String ?? "" let type = service["type"] as? String ?? "" guard identifier.hasSuffix("#atproto_pds"), type == "AtprotoPersonalDataServer", let endpoint = service["serviceEndpoint"] as? String, let url = URL(string: endpoint), url.scheme == "https", url.user == nil, url.password == nil, url.path.isEmpty || url.path == "/", url.query == nil, url.fragment == nil else { continue } return url } return nil } }