Repository navigation
Expand file tree
/
Copy pathCustomProvider.swift
More file actions
561 lines (474 loc) · 18.6 KB
/
Copy pathCustomProvider.swift
File metadata and controls
561 lines (474 loc) · 18.6 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//
// CustomProvider.swift
// Auth
//
// Created by Guilherme Souza on 03/11/26.
//
public import Foundation
public import Helpers
/// The protocol a custom provider speaks.
public struct CustomProviderType: RawRepresentable, Codable, Hashable, Sendable,
ExpressibleByStringLiteral
{
/// The protocol name as the server sends it, such as `"oidc"`.
public let rawValue: String
/// Creates a ``CustomProviderType`` from a raw string value.
public init(rawValue: String) {
self.rawValue = rawValue
}
/// Creates a ``CustomProviderType`` from a string literal.
public init(stringLiteral value: String) {
self.init(rawValue: value)
}
/// A plain OAuth 2.0 provider. You give its authorization, token, and userinfo URLs.
public static let oauth2: CustomProviderType = "oauth2"
/// An OpenID Connect provider. The server reads its endpoints from the discovery document.
public static let oidc: CustomProviderType = "oidc"
}
/// A custom OAuth 2.0 or OIDC provider registered in Supabase Auth.
///
/// Users sign in with it through ``AuthClient/oauthSignInURL(_:options:)``, passing
/// ``identifier`` as the provider.
public struct CustomProvider: Decodable, Hashable, Sendable, Identifiable {
/// The provider's unique ID.
public var id: UUID
/// Whether the provider uses OAuth 2.0 or OIDC.
public var providerType: CustomProviderType
/// The name used to sign in with the provider. It starts with `custom:`, such as
/// `custom:acme`.
public var identifier: String
/// The name shown to people.
public var name: String
/// The OAuth client ID.
public var clientID: String
/// More client IDs that the server accepts when it checks an ID token.
public var acceptableClientIDs: [String]
/// The scopes the server requests at the provider.
public var scopes: [String]
/// The provider claims that the server copies as they are into the user's `custom_claims`.
public var customClaimsAllowlist: [String]
/// Whether the server uses PKCE with the provider.
public var pkceEnabled: Bool
/// How the provider's claims map to Supabase user attributes.
public var attributeMapping: JSONObject
/// Extra query parameters the server adds to the authorization request.
public var authorizationParams: [String: String]
/// Whether users can sign in with the provider.
public var enabled: Bool
/// Whether a user can sign in when the provider sends no email address.
public var emailOptional: Bool
/// The OIDC issuer. OIDC providers only.
public var issuer: String?
/// The URL of the OIDC discovery document, when it is not under ``issuer``.
public var discoveryURL: URL?
/// Whether the server skips the ID token nonce check. OIDC providers only.
public var skipNonceCheck: Bool
/// The authorization endpoint. OAuth 2.0 providers only.
public var authorizationURL: URL?
/// The token endpoint. OAuth 2.0 providers only.
public var tokenURL: URL?
/// The userinfo endpoint. OAuth 2.0 providers only.
public var userinfoURL: URL?
/// The JSON Web Key Set URL used to verify ID tokens.
public var jwksURI: URL?
/// The discovery document the server fetched. OIDC providers only.
public var discoveryDocument: OIDCDiscoveryDocument?
/// When the provider was created.
public var createdAt: Date
/// When the provider was last updated.
public var updatedAt: Date
/// Creates a ``CustomProvider``.
public init(
id: UUID,
providerType: CustomProviderType,
identifier: String,
name: String,
clientID: String,
acceptableClientIDs: [String],
scopes: [String],
customClaimsAllowlist: [String],
pkceEnabled: Bool,
attributeMapping: JSONObject,
authorizationParams: [String: String],
enabled: Bool,
emailOptional: Bool,
issuer: String? = nil,
discoveryURL: URL? = nil,
skipNonceCheck: Bool,
authorizationURL: URL? = nil,
tokenURL: URL? = nil,
userinfoURL: URL? = nil,
jwksURI: URL? = nil,
discoveryDocument: OIDCDiscoveryDocument? = nil,
createdAt: Date,
updatedAt: Date
) {
self.id = id
self.providerType = providerType
self.identifier = identifier
self.name = name
self.clientID = clientID
self.acceptableClientIDs = acceptableClientIDs
self.scopes = scopes
self.customClaimsAllowlist = customClaimsAllowlist
self.pkceEnabled = pkceEnabled
self.attributeMapping = attributeMapping
self.authorizationParams = authorizationParams
self.enabled = enabled
self.emailOptional = emailOptional
self.issuer = issuer
self.discoveryURL = discoveryURL
self.skipNonceCheck = skipNonceCheck
self.authorizationURL = authorizationURL
self.tokenURL = tokenURL
self.userinfoURL = userinfoURL
self.jwksURI = jwksURI
self.discoveryDocument = discoveryDocument
self.createdAt = createdAt
self.updatedAt = updatedAt
}
private enum CodingKeys: String, CodingKey {
case id
case providerType = "provider_type"
case identifier
case name
case clientID = "client_id"
case acceptableClientIDs = "acceptable_client_ids"
case scopes
case customClaimsAllowlist = "custom_claims_allowlist"
case pkceEnabled = "pkce_enabled"
case attributeMapping = "attribute_mapping"
case authorizationParams = "authorization_params"
case enabled
case emailOptional = "email_optional"
case issuer
case discoveryURL = "discovery_url"
case skipNonceCheck = "skip_nonce_check"
case authorizationURL = "authorization_url"
case tokenURL = "token_url"
case userinfoURL = "userinfo_url"
case jwksURI = "jwks_uri"
case discoveryDocument = "discovery_document"
case createdAt = "created_at"
case updatedAt = "updated_at"
}
/// Decodes a provider. A list, a map, or a flag that the server sends as `null` or omits
/// becomes empty or `false`.
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
providerType = try container.decode(CustomProviderType.self, forKey: .providerType)
identifier = try container.decode(String.self, forKey: .identifier)
name = try container.decode(String.self, forKey: .name)
clientID = try container.decode(String.self, forKey: .clientID)
// The server sends `null` for an empty list or map.
acceptableClientIDs =
try container.decodeIfPresent([String].self, forKey: .acceptableClientIDs) ?? []
scopes = try container.decodeIfPresent([String].self, forKey: .scopes) ?? []
customClaimsAllowlist =
try container.decodeIfPresent([String].self, forKey: .customClaimsAllowlist) ?? []
pkceEnabled = try container.decodeIfPresent(Bool.self, forKey: .pkceEnabled) ?? false
attributeMapping =
try container.decodeIfPresent(JSONObject.self, forKey: .attributeMapping) ?? [:]
authorizationParams =
try container.decodeIfPresent([String: String].self, forKey: .authorizationParams) ?? [:]
enabled = try container.decodeIfPresent(Bool.self, forKey: .enabled) ?? false
emailOptional = try container.decodeIfPresent(Bool.self, forKey: .emailOptional) ?? false
issuer = try container.decodeIfPresent(String.self, forKey: .issuer)
discoveryURL = try container.decodeIfPresent(URL.self, forKey: .discoveryURL)
skipNonceCheck = try container.decodeIfPresent(Bool.self, forKey: .skipNonceCheck) ?? false
authorizationURL = try container.decodeIfPresent(URL.self, forKey: .authorizationURL)
tokenURL = try container.decodeIfPresent(URL.self, forKey: .tokenURL)
userinfoURL = try container.decodeIfPresent(URL.self, forKey: .userinfoURL)
jwksURI = try container.decodeIfPresent(URL.self, forKey: .jwksURI)
discoveryDocument = try container.decodeIfPresent(
OIDCDiscoveryDocument.self, forKey: .discoveryDocument)
createdAt = try container.decode(Date.self, forKey: .createdAt)
updatedAt = try container.decode(Date.self, forKey: .updatedAt)
}
}
/// The OIDC discovery document the server fetched for a ``CustomProvider``.
public struct OIDCDiscoveryDocument: Decodable, Hashable, Sendable {
/// The issuer identifier.
public var issuer: String
/// The authorization endpoint.
public var authorizationEndpoint: URL
/// The token endpoint.
public var tokenEndpoint: URL
/// The userinfo endpoint.
public var userinfoEndpoint: URL?
/// The JSON Web Key Set URL.
public var jwksURI: URL
/// The scopes the provider supports.
public var scopesSupported: [String]?
/// The response types the provider supports.
public var responseTypesSupported: [String]?
/// The grant types the provider supports.
public var grantTypesSupported: [String]?
/// The subject identifier types the provider supports.
public var subjectTypesSupported: [String]?
/// Creates an ``OIDCDiscoveryDocument``.
public init(
issuer: String,
authorizationEndpoint: URL,
tokenEndpoint: URL,
userinfoEndpoint: URL? = nil,
jwksURI: URL,
scopesSupported: [String]? = nil,
responseTypesSupported: [String]? = nil,
grantTypesSupported: [String]? = nil,
subjectTypesSupported: [String]? = nil
) {
self.issuer = issuer
self.authorizationEndpoint = authorizationEndpoint
self.tokenEndpoint = tokenEndpoint
self.userinfoEndpoint = userinfoEndpoint
self.jwksURI = jwksURI
self.scopesSupported = scopesSupported
self.responseTypesSupported = responseTypesSupported
self.grantTypesSupported = grantTypesSupported
self.subjectTypesSupported = subjectTypesSupported
}
private enum CodingKeys: String, CodingKey {
case issuer
case authorizationEndpoint = "authorization_endpoint"
case tokenEndpoint = "token_endpoint"
case userinfoEndpoint = "userinfo_endpoint"
case jwksURI = "jwks_uri"
case scopesSupported = "scopes_supported"
case responseTypesSupported = "response_types_supported"
case grantTypesSupported = "grant_types_supported"
case subjectTypesSupported = "subject_types_supported"
}
}
/// The settings for a new custom provider, passed to ``AuthAdminCustomProviders/create(_:)``.
///
/// Use ``oauth2(identifier:name:clientID:clientSecret:authorizationURL:tokenURL:userinfoURL:)``
/// or ``oidc(identifier:name:clientID:clientSecret:issuer:)`` to set the required fields, then
/// set the optional ones.
public struct CreateCustomProvider: Encodable, Hashable, Sendable {
/// Whether the provider uses OAuth 2.0 or OIDC.
public var providerType: CustomProviderType
/// The name used to sign in with the provider. It must start with `custom:`.
public var identifier: String
/// The name shown to people.
public var name: String
/// The OAuth client ID.
public var clientID: String
/// The OAuth client secret. The server stores it and never returns it.
public var clientSecret: String
/// More client IDs that the server accepts when it checks an ID token.
public var acceptableClientIDs: [String]?
/// The scopes to request at the provider.
public var scopes: [String]?
/// The provider claims to copy as they are into the user's `custom_claims`.
public var customClaimsAllowlist: [String]?
/// Whether to use PKCE with the provider.
public var pkceEnabled: Bool?
/// How the provider's claims map to Supabase user attributes.
public var attributeMapping: JSONObject?
/// Extra query parameters to add to the authorization request.
public var authorizationParams: [String: String]?
/// Whether users can sign in with the provider.
public var enabled: Bool?
/// Whether a user can sign in when the provider sends no email address.
public var emailOptional: Bool?
/// The OIDC issuer. Required for OIDC providers.
public var issuer: String?
/// The URL of the OIDC discovery document, when it is not under ``issuer``.
public var discoveryURL: URL?
/// Whether to skip the ID token nonce check. OIDC providers only.
public var skipNonceCheck: Bool?
/// The authorization endpoint. Required for OAuth 2.0 providers.
public var authorizationURL: URL?
/// The token endpoint. Required for OAuth 2.0 providers.
public var tokenURL: URL?
/// The userinfo endpoint. Required for OAuth 2.0 providers.
public var userinfoURL: URL?
/// The JSON Web Key Set URL used to verify ID tokens.
public var jwksURI: URL?
/// Creates the settings for a new provider. Prefer
/// ``oauth2(identifier:name:clientID:clientSecret:authorizationURL:tokenURL:userinfoURL:)`` or
/// ``oidc(identifier:name:clientID:clientSecret:issuer:)``.
public init(
providerType: CustomProviderType,
identifier: String,
name: String,
clientID: String,
clientSecret: String
) {
self.providerType = providerType
self.identifier = identifier
self.name = name
self.clientID = clientID
self.clientSecret = clientSecret
}
/// The settings for an OAuth 2.0 provider.
public static func oauth2(
identifier: String,
name: String,
clientID: String,
clientSecret: String,
authorizationURL: URL,
tokenURL: URL,
userinfoURL: URL
) -> CreateCustomProvider {
var provider = CreateCustomProvider(
providerType: .oauth2,
identifier: identifier,
name: name,
clientID: clientID,
clientSecret: clientSecret
)
provider.authorizationURL = authorizationURL
provider.tokenURL = tokenURL
provider.userinfoURL = userinfoURL
return provider
}
/// The settings for an OIDC provider.
///
/// The server fetches the discovery document under `issuer` before it creates the provider,
/// and fails with a `validation_failed` error when it cannot.
public static func oidc(
identifier: String,
name: String,
clientID: String,
clientSecret: String,
issuer: String
) -> CreateCustomProvider {
var provider = CreateCustomProvider(
providerType: .oidc,
identifier: identifier,
name: name,
clientID: clientID,
clientSecret: clientSecret
)
provider.issuer = issuer
return provider
}
private enum CodingKeys: String, CodingKey {
case providerType = "provider_type"
case identifier
case name
case clientID = "client_id"
case clientSecret = "client_secret"
case acceptableClientIDs = "acceptable_client_ids"
case scopes
case customClaimsAllowlist = "custom_claims_allowlist"
case pkceEnabled = "pkce_enabled"
case attributeMapping = "attribute_mapping"
case authorizationParams = "authorization_params"
case enabled
case emailOptional = "email_optional"
case issuer
case discoveryURL = "discovery_url"
case skipNonceCheck = "skip_nonce_check"
case authorizationURL = "authorization_url"
case tokenURL = "token_url"
case userinfoURL = "userinfo_url"
case jwksURI = "jwks_uri"
}
}
/// The changes to a custom provider, passed to
/// ``AuthAdminCustomProviders/update(identifier:_:)``.
///
/// The server changes only the fields you set. A field left `nil` keeps its stored value. The
/// provider type and identifier cannot change.
public struct UpdateCustomProvider: Encodable, Hashable, Sendable {
/// The name shown to people.
public var name: String?
/// The OAuth client ID.
public var clientID: String?
/// A new OAuth client secret.
public var clientSecret: String?
/// More client IDs that the server accepts when it checks an ID token.
public var acceptableClientIDs: [String]?
/// The scopes to request at the provider.
public var scopes: [String]?
/// The provider claims to copy as they are into the user's `custom_claims`.
public var customClaimsAllowlist: [String]?
/// Whether to use PKCE with the provider.
public var pkceEnabled: Bool?
/// How the provider's claims map to Supabase user attributes.
public var attributeMapping: JSONObject?
/// Extra query parameters to add to the authorization request.
public var authorizationParams: [String: String]?
/// Whether users can sign in with the provider.
public var enabled: Bool?
/// Whether a user can sign in when the provider sends no email address.
public var emailOptional: Bool?
/// The OIDC issuer. A change makes the server fetch the discovery document again.
public var issuer: String?
/// The URL of the OIDC discovery document. A change makes the server fetch it again.
public var discoveryURL: URL?
/// Whether to skip the ID token nonce check. OIDC providers only.
public var skipNonceCheck: Bool?
/// The authorization endpoint. OAuth 2.0 providers only.
public var authorizationURL: URL?
/// The token endpoint. OAuth 2.0 providers only.
public var tokenURL: URL?
/// The userinfo endpoint. OAuth 2.0 providers only.
public var userinfoURL: URL?
/// The JSON Web Key Set URL used to verify ID tokens.
public var jwksURI: URL?
/// Creates an empty set of changes. Set the fields to change.
public init(
name: String? = nil,
clientID: String? = nil,
clientSecret: String? = nil,
acceptableClientIDs: [String]? = nil,
scopes: [String]? = nil,
customClaimsAllowlist: [String]? = nil,
pkceEnabled: Bool? = nil,
attributeMapping: JSONObject? = nil,
authorizationParams: [String: String]? = nil,
enabled: Bool? = nil,
emailOptional: Bool? = nil,
issuer: String? = nil,
discoveryURL: URL? = nil,
skipNonceCheck: Bool? = nil,
authorizationURL: URL? = nil,
tokenURL: URL? = nil,
userinfoURL: URL? = nil,
jwksURI: URL? = nil
) {
self.name = name
self.clientID = clientID
self.clientSecret = clientSecret
self.acceptableClientIDs = acceptableClientIDs
self.scopes = scopes
self.customClaimsAllowlist = customClaimsAllowlist
self.pkceEnabled = pkceEnabled
self.attributeMapping = attributeMapping
self.authorizationParams = authorizationParams
self.enabled = enabled
self.emailOptional = emailOptional
self.issuer = issuer
self.discoveryURL = discoveryURL
self.skipNonceCheck = skipNonceCheck
self.authorizationURL = authorizationURL
self.tokenURL = tokenURL
self.userinfoURL = userinfoURL
self.jwksURI = jwksURI
}
private enum CodingKeys: String, CodingKey {
case name
case clientID = "client_id"
case clientSecret = "client_secret"
case acceptableClientIDs = "acceptable_client_ids"
case scopes
case customClaimsAllowlist = "custom_claims_allowlist"
case pkceEnabled = "pkce_enabled"
case attributeMapping = "attribute_mapping"
case authorizationParams = "authorization_params"
case enabled
case emailOptional = "email_optional"
case issuer
case discoveryURL = "discovery_url"
case skipNonceCheck = "skip_nonce_check"
case authorizationURL = "authorization_url"
case tokenURL = "token_url"
case userinfoURL = "userinfo_url"
case jwksURI = "jwks_uri"
}
}