1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
}
}