-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathJWT+Google.swift
190 lines (169 loc) · 6.32 KB
/
JWT+Google.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import NIOConcurrencyHelpers
import Vapor
extension Request.JWT {
public var google: Google {
.init(_jwt: self)
}
public struct Google: Sendable {
public let _jwt: Request.JWT
public func verify(
applicationIdentifier: String? = nil,
gSuiteDomainName: String? = nil
) async throws -> GoogleIdentityToken {
guard let token = self._jwt._request.headers.bearerAuthorization?.token else {
self._jwt._request.logger.error("Request is missing JWT bearer header.")
throw Abort(.unauthorized)
}
return try await self.verify(token, applicationIdentifier: applicationIdentifier, gSuiteDomainName: gSuiteDomainName)
}
public func verify(
_ message: String,
applicationIdentifier: String? = nil,
gSuiteDomainName: String? = nil
) async throws -> GoogleIdentityToken {
try await self.verify([UInt8](message.utf8), applicationIdentifier: applicationIdentifier, gSuiteDomainName: gSuiteDomainName)
}
public func verify(
_ message: some DataProtocol & Sendable,
applicationIdentifier: String? = nil,
gSuiteDomainName: String? = nil
) async throws -> GoogleIdentityToken {
let keys = try await self._jwt._request.application.jwt.google.keys(on: self._jwt._request)
let token = try await keys.verify(message, as: GoogleIdentityToken.self)
if let applicationIdentifier = applicationIdentifier ?? self._jwt._request.application.jwt.google.applicationIdentifier {
try token.audience.verifyIntendedAudience(includes: applicationIdentifier)
}
if let gSuiteDomainName = gSuiteDomainName ?? self._jwt._request.application.jwt.google.gSuiteDomainName {
guard let hd = token.hostedDomain, hd.value == gSuiteDomainName else {
throw JWTError.claimVerificationFailure(
failedClaim: token.hostedDomain,
reason: "Hosted domain claim does not match gSuite domain name"
)
}
}
return token
}
}
}
extension Application.JWT {
public var google: Google {
.init(_jwt: self)
}
public struct Google: Sendable {
public let _jwt: Application.JWT
public func keys(on request: Request) async throws -> JWTKeyCollection {
try await .init().add(jwks: jwks.get(on: request).get())
}
public var jwks: EndpointCache<JWKS> {
self.storage.jwks
}
public var jwksEndpoint: URI {
get {
self.storage.jwksEndpoint
}
nonmutating set {
self.storage.jwksEndpoint = newValue
self.storage.jwks = .init(uri: newValue)
}
}
public var applicationIdentifier: String? {
get {
self.storage.applicationIdentifier
}
nonmutating set {
self.storage.applicationIdentifier = newValue
}
}
public var gSuiteDomainName: String? {
get {
self.storage.gSuiteDomainName
}
nonmutating set {
self.storage.gSuiteDomainName = newValue
}
}
private struct Key: StorageKey, LockKey {
typealias Value = Storage
}
private final class Storage: Sendable {
private struct SendableBox: Sendable {
var jwks: EndpointCache<JWKS>
var jwksEndpoint: URI
var applicationIdentifier: String? = nil
var gSuiteDomainName: String? = nil
}
private let sendableBox: NIOLockedValueBox<SendableBox>
var jwks: EndpointCache<JWKS> {
get {
self.sendableBox.withLockedValue { box in
box.jwks
}
}
set {
self.sendableBox.withLockedValue { box in
box.jwks = newValue
}
}
}
var applicationIdentifier: String? {
get {
self.sendableBox.withLockedValue { box in
box.applicationIdentifier
}
}
set {
self.sendableBox.withLockedValue { box in
box.applicationIdentifier = newValue
}
}
}
var gSuiteDomainName: String? {
get {
self.sendableBox.withLockedValue { box in
box.gSuiteDomainName
}
}
set {
self.sendableBox.withLockedValue { box in
box.gSuiteDomainName = newValue
}
}
}
var jwksEndpoint: URI {
get {
self.sendableBox.withLockedValue { box in
box.jwksEndpoint
}
}
set {
self.sendableBox.withLockedValue { box in
box.jwksEndpoint = newValue
}
}
}
init() {
let jwksEndpoint: URI = "https://www.googleapis.com/oauth2/v3/certs"
let box = SendableBox(
jwks: .init(uri: jwksEndpoint),
jwksEndpoint: jwksEndpoint
)
self.sendableBox = .init(box)
}
}
private var storage: Storage {
if let existing = self._jwt._application.storage[Key.self] {
return existing
} else {
let lock = self._jwt._application.locks.lock(for: Key.self)
lock.lock()
defer { lock.unlock() }
if let existing = self._jwt._application.storage[Key.self] {
return existing
}
let new = Storage()
self._jwt._application.storage[Key.self] = new
return new
}
}
}
}