-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathApplication+JWT.swift
58 lines (49 loc) · 1.49 KB
/
Application+JWT.swift
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import JWTKit
import NIOConcurrencyHelpers
import Vapor
extension Application {
public var jwt: JWT {
.init(_application: self)
}
public struct JWT: Sendable {
private final class Storage: Sendable {
private struct SendableBox: Sendable {
var keys: JWTKeyCollection
}
private let sendableBox: NIOLockedValueBox<SendableBox>
var keys: JWTKeyCollection {
get {
self.sendableBox.withLockedValue { box in
box.keys
}
}
set {
self.sendableBox.withLockedValue { box in
box.keys = newValue
}
}
}
init() {
let box = SendableBox(keys: .init())
self.sendableBox = .init(box)
}
}
private struct Key: StorageKey {
typealias Value = Storage
}
public let _application: Application
public var keys: JWTKeyCollection {
get { self.storage.keys }
set { self.storage.keys = newValue }
}
private var storage: Storage {
if let existing = self._application.storage[Key.self] {
return existing
} else {
let new = Storage()
self._application.storage[Key.self] = new
return new
}
}
}
}