forked from vapor/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJWT+FirebaseAuth.swift
168 lines (149 loc) · 5.73 KB
/
JWT+FirebaseAuth.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
import NIOConcurrencyHelpers
import Vapor
extension Request.JWT {
public var firebaseAuth: FirebaseAuth {
.init(_jwt: self)
}
public struct FirebaseAuth: Sendable {
public let _jwt: Request.JWT
public func verify(
applicationIdentifier: String? = nil
) async throws -> FirebaseAuthIdentityToken {
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)
}
public func verify(
_ message: String,
applicationIdentifier: String? = nil
) async throws -> FirebaseAuthIdentityToken {
try await self.verify([UInt8](message.utf8), applicationIdentifier: applicationIdentifier)
}
public func verify(
_ message: some DataProtocol & Sendable,
applicationIdentifier: String? = nil
) async throws -> FirebaseAuthIdentityToken {
let keys = try await self._jwt._request.application.jwt.firebaseAuth.keys(on: self._jwt._request)
let token = try await keys.verify(message, as: FirebaseAuthIdentityToken.self)
if let applicationIdentifier = applicationIdentifier ?? self._jwt._request.application.jwt.firebaseAuth.applicationIdentifier {
try token.audience.verifyIntendedAudience(includes: applicationIdentifier)
guard token.audience.value.first == applicationIdentifier else {
throw JWTError.claimVerificationFailure(
failedClaim: token.audience,
reason: "Audience claim does not match expected value"
)
}
guard token.issuer.value == "https://securetoken.google.com/\(applicationIdentifier)" else {
throw JWTError.claimVerificationFailure(
failedClaim: token.issuer,
reason: "Issuer claim does not match expected value"
)
}
}
return token
}
}
}
extension Application.JWT {
public var firebaseAuth: FirebaseAuth {
.init(_jwt: self)
}
public struct FirebaseAuth: 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
}
}
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
}
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 jwksEndpoint: URI {
get {
self.sendableBox.withLockedValue { box in
box.jwksEndpoint
}
}
set {
self.sendableBox.withLockedValue { box in
box.jwksEndpoint = newValue
}
}
}
init() {
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
}
}
}
}