37 lines
982 B
Swift
37 lines
982 B
Swift
import Foundation
|
|
|
|
public struct ExpiringUserId: LosslessStringConvertible, Codable, Sendable {
|
|
|
|
static let expirationInterval: TimeInterval = 60.0 * 60.0
|
|
let id: UUID
|
|
let expiration: Date
|
|
|
|
init (user: any ManagedUser) {
|
|
id = user.id
|
|
expiration = Date (timeIntervalSinceNow: ExpiringUserId.expirationInterval)
|
|
}
|
|
|
|
public init? (_ description: String) {
|
|
if let data = Data (base64Encoded: description) {
|
|
do {
|
|
self = try JSONDecoder().decode (ExpiringUserId.self, from: data)
|
|
if expiration.timeIntervalSinceNow < 0.0 {
|
|
return nil
|
|
}
|
|
} catch {
|
|
return nil
|
|
}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
public var description: String {
|
|
do {
|
|
return try JSONEncoder().encode (self).base64EncodedString()
|
|
} catch {
|
|
return error.localizedDescription
|
|
}
|
|
}
|
|
}
|