capacitor-auth-manager 2.4.1 → 2.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +64 -1
- package/CapacitorAuthManager.podspec +9 -17
- package/README.md +505 -0
- package/android/build.gradle +6 -16
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/CapacitorAuthManager.java +1 -1
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/GoogleAuthProvider.java +2 -5
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/ProviderFactory.java +10 -110
- package/ios/Plugin/CapacitorAuthManager.swift +6 -15
- package/ios/Plugin/Plugin.swift +4 -2
- package/package.json +12 -24
- package/Readme.md +0 -203
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/FacebookAuthProvider.java +0 -389
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/MicrosoftAuthProvider.java +0 -455
- package/ios/Plugin/AppleAuthProvider.swift +0 -288
- package/ios/Plugin/FacebookAuthProvider.swift +0 -297
- package/ios/Plugin/GitHubAuthProvider.swift +0 -54
- package/ios/Plugin/LinkedInAuthProvider.swift +0 -63
- package/ios/Plugin/MicrosoftAuthProvider.swift +0 -315
- package/ios/Plugin/OAuthWebProvider.swift +0 -415
- package/ios/Plugin/SlackAuthProvider.swift +0 -108
|
@@ -1,415 +0,0 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
import Capacitor
|
|
3
|
-
import AuthenticationServices
|
|
4
|
-
|
|
5
|
-
class OAuthWebProvider: NSObject, BaseAuthProvider, ASWebAuthenticationPresentationContextProviding {
|
|
6
|
-
let provider: AuthProvider
|
|
7
|
-
let storage: AuthStorage
|
|
8
|
-
let logger: AuthLogger
|
|
9
|
-
|
|
10
|
-
private var clientId: String?
|
|
11
|
-
private var clientSecret: String?
|
|
12
|
-
private var redirectUri: String?
|
|
13
|
-
private var scopes: [String]
|
|
14
|
-
private var authUrl: String
|
|
15
|
-
private var tokenUrl: String
|
|
16
|
-
private var userInfoUrl: String
|
|
17
|
-
private var currentUser: AuthUser?
|
|
18
|
-
|
|
19
|
-
private var webAuthSession: ASWebAuthenticationSession?
|
|
20
|
-
|
|
21
|
-
init(
|
|
22
|
-
provider: AuthProvider,
|
|
23
|
-
config: AuthProviderConfig,
|
|
24
|
-
storage: AuthStorage,
|
|
25
|
-
logger: AuthLogger,
|
|
26
|
-
authUrl: String,
|
|
27
|
-
tokenUrl: String,
|
|
28
|
-
userInfoUrl: String,
|
|
29
|
-
defaultScopes: [String]
|
|
30
|
-
) {
|
|
31
|
-
self.provider = provider
|
|
32
|
-
self.storage = storage
|
|
33
|
-
self.logger = logger
|
|
34
|
-
self.authUrl = authUrl
|
|
35
|
-
self.tokenUrl = tokenUrl
|
|
36
|
-
self.userInfoUrl = userInfoUrl
|
|
37
|
-
self.scopes = defaultScopes
|
|
38
|
-
|
|
39
|
-
super.init()
|
|
40
|
-
|
|
41
|
-
if let clientId = config.options["clientId"] as? String {
|
|
42
|
-
self.clientId = clientId
|
|
43
|
-
}
|
|
44
|
-
if let clientSecret = config.options["clientSecret"] as? String {
|
|
45
|
-
self.clientSecret = clientSecret
|
|
46
|
-
}
|
|
47
|
-
if let redirectUri = config.options["redirectUri"] as? String {
|
|
48
|
-
self.redirectUri = redirectUri
|
|
49
|
-
}
|
|
50
|
-
if let scopes = config.options["scopes"] as? [String] {
|
|
51
|
-
self.scopes = scopes
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
func initialize(completion: @escaping (Error?) -> Void) {
|
|
56
|
-
logger.info("Initializing \(provider.rawValue) OAuth provider")
|
|
57
|
-
|
|
58
|
-
// Check for existing credential
|
|
59
|
-
if let credential = storage.getCredential(for: provider) {
|
|
60
|
-
if let accessToken = credential.accessToken {
|
|
61
|
-
fetchUserInfo(accessToken: accessToken) { [weak self] user, _ in
|
|
62
|
-
self?.currentUser = user
|
|
63
|
-
completion(nil)
|
|
64
|
-
}
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
completion(nil)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
func signIn(credentials: JSObject?, options: JSObject?, completion: @escaping (AuthResult?, Error?) -> Void) {
|
|
73
|
-
logger.info("Starting \(provider.rawValue) sign-in")
|
|
74
|
-
|
|
75
|
-
guard let clientId = clientId else {
|
|
76
|
-
completion(nil, createAuthError(code: .missingConfiguration, message: "Client ID is required"))
|
|
77
|
-
return
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
guard let redirectUri = redirectUri else {
|
|
81
|
-
completion(nil, createAuthError(code: .missingConfiguration, message: "Redirect URI is required"))
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let state = UUID().uuidString
|
|
86
|
-
let scopeString = scopes.joined(separator: " ")
|
|
87
|
-
|
|
88
|
-
var urlComponents = URLComponents(string: authUrl)!
|
|
89
|
-
urlComponents.queryItems = [
|
|
90
|
-
URLQueryItem(name: "client_id", value: clientId),
|
|
91
|
-
URLQueryItem(name: "redirect_uri", value: redirectUri),
|
|
92
|
-
URLQueryItem(name: "scope", value: scopeString),
|
|
93
|
-
URLQueryItem(name: "state", value: state),
|
|
94
|
-
URLQueryItem(name: "response_type", value: "code")
|
|
95
|
-
]
|
|
96
|
-
|
|
97
|
-
guard let authURL = urlComponents.url else {
|
|
98
|
-
completion(nil, createAuthError(code: .internalError, message: "Failed to construct auth URL"))
|
|
99
|
-
return
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
guard let callbackScheme = URL(string: redirectUri)?.scheme else {
|
|
103
|
-
completion(nil, createAuthError(code: .internalError, message: "Invalid redirect URI scheme"))
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
webAuthSession = ASWebAuthenticationSession(
|
|
108
|
-
url: authURL,
|
|
109
|
-
callbackURLScheme: callbackScheme
|
|
110
|
-
) { [weak self] callbackURL, error in
|
|
111
|
-
guard let self = self else { return }
|
|
112
|
-
|
|
113
|
-
if let error = error as? ASWebAuthenticationSessionError {
|
|
114
|
-
if error.code == .canceledLogin {
|
|
115
|
-
completion(nil, self.createAuthError(code: .userCancelled, message: "User cancelled sign-in"))
|
|
116
|
-
} else {
|
|
117
|
-
completion(nil, self.createAuthError(code: .internalError, message: error.localizedDescription))
|
|
118
|
-
}
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
guard let callbackURL = callbackURL else {
|
|
123
|
-
completion(nil, self.createAuthError(code: .internalError, message: "No callback URL"))
|
|
124
|
-
return
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
guard let code = URLComponents(url: callbackURL, resolvingAgainstBaseURL: false)?
|
|
128
|
-
.queryItems?.first(where: { $0.name == "code" })?.value else {
|
|
129
|
-
completion(nil, self.createAuthError(code: .internalError, message: "No authorization code"))
|
|
130
|
-
return
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
self.exchangeCodeForToken(code: code, redirectUri: redirectUri, completion: completion)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
webAuthSession?.presentationContextProvider = self
|
|
137
|
-
webAuthSession?.prefersEphemeralWebBrowserSession = false
|
|
138
|
-
webAuthSession?.start()
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private func exchangeCodeForToken(code: String, redirectUri: String, completion: @escaping (AuthResult?, Error?) -> Void) {
|
|
142
|
-
guard let clientId = clientId else {
|
|
143
|
-
completion(nil, createAuthError(code: .missingConfiguration, message: "Client ID is required"))
|
|
144
|
-
return
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
var request = URLRequest(url: URL(string: tokenUrl)!)
|
|
148
|
-
request.httpMethod = "POST"
|
|
149
|
-
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
150
|
-
request.setValue("application/json", forHTTPHeaderField: "Accept")
|
|
151
|
-
|
|
152
|
-
var body = "client_id=\(clientId)&code=\(code)&redirect_uri=\(redirectUri)&grant_type=authorization_code"
|
|
153
|
-
if let clientSecret = clientSecret {
|
|
154
|
-
body += "&client_secret=\(clientSecret)"
|
|
155
|
-
}
|
|
156
|
-
request.httpBody = body.data(using: .utf8)
|
|
157
|
-
|
|
158
|
-
URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
|
|
159
|
-
guard let self = self else { return }
|
|
160
|
-
|
|
161
|
-
if let error = error {
|
|
162
|
-
DispatchQueue.main.async {
|
|
163
|
-
completion(nil, self.createAuthError(code: .networkError, message: error.localizedDescription))
|
|
164
|
-
}
|
|
165
|
-
return
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
guard let data = data,
|
|
169
|
-
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
170
|
-
let accessToken = json["access_token"] as? String else {
|
|
171
|
-
DispatchQueue.main.async {
|
|
172
|
-
completion(nil, self.createAuthError(code: .internalError, message: "Failed to parse token response"))
|
|
173
|
-
}
|
|
174
|
-
return
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
let refreshToken = json["refresh_token"] as? String
|
|
178
|
-
let expiresIn = json["expires_in"] as? Int
|
|
179
|
-
let scope = json["scope"] as? String
|
|
180
|
-
|
|
181
|
-
self.fetchUserInfo(accessToken: accessToken) { [weak self] user, error in
|
|
182
|
-
guard let self = self else { return }
|
|
183
|
-
|
|
184
|
-
DispatchQueue.main.async {
|
|
185
|
-
if let error = error {
|
|
186
|
-
completion(nil, error)
|
|
187
|
-
return
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
guard let user = user else {
|
|
191
|
-
completion(nil, self.createAuthError(code: .internalError, message: "Failed to fetch user info"))
|
|
192
|
-
return
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
self.currentUser = user
|
|
196
|
-
|
|
197
|
-
let credential = AuthCredential(
|
|
198
|
-
providerId: self.provider.rawValue,
|
|
199
|
-
signInMethod: "oauth",
|
|
200
|
-
accessToken: accessToken,
|
|
201
|
-
idToken: nil,
|
|
202
|
-
refreshToken: refreshToken,
|
|
203
|
-
expiresAt: expiresIn != nil ? Int(Date().timeIntervalSince1970 * 1000) + (expiresIn! * 1000) : nil,
|
|
204
|
-
tokenType: "Bearer",
|
|
205
|
-
scope: scope,
|
|
206
|
-
rawNonce: nil
|
|
207
|
-
)
|
|
208
|
-
|
|
209
|
-
self.storage.saveCredential(credential, for: self.provider)
|
|
210
|
-
|
|
211
|
-
let result = AuthResult(
|
|
212
|
-
user: user,
|
|
213
|
-
credential: credential,
|
|
214
|
-
additionalUserInfo: AdditionalUserInfo(
|
|
215
|
-
isNewUser: false,
|
|
216
|
-
providerId: self.provider.rawValue,
|
|
217
|
-
profile: nil,
|
|
218
|
-
username: nil
|
|
219
|
-
),
|
|
220
|
-
operationType: "signIn"
|
|
221
|
-
)
|
|
222
|
-
|
|
223
|
-
self.logger.info("\(self.provider.rawValue) sign-in successful")
|
|
224
|
-
completion(result, nil)
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}.resume()
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
func fetchUserInfo(accessToken: String, completion: @escaping (AuthUser?, Error?) -> Void) {
|
|
231
|
-
var request = URLRequest(url: URL(string: userInfoUrl)!)
|
|
232
|
-
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
|
|
233
|
-
|
|
234
|
-
URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
|
|
235
|
-
guard let self = self else { return }
|
|
236
|
-
|
|
237
|
-
if let error = error {
|
|
238
|
-
completion(nil, self.createAuthError(code: .networkError, message: error.localizedDescription))
|
|
239
|
-
return
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
guard let data = data,
|
|
243
|
-
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
|
|
244
|
-
completion(nil, self.createAuthError(code: .internalError, message: "Failed to parse user info"))
|
|
245
|
-
return
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
let user = self.parseUserInfo(json: json)
|
|
249
|
-
completion(user, nil)
|
|
250
|
-
}.resume()
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
func parseUserInfo(json: [String: Any]) -> AuthUser {
|
|
254
|
-
// Override in subclasses for provider-specific parsing
|
|
255
|
-
let uid = (json["id"] as? Int).map { String($0) } ?? (json["id"] as? String) ?? ""
|
|
256
|
-
let email = json["email"] as? String
|
|
257
|
-
let name = json["name"] as? String ?? json["login"] as? String
|
|
258
|
-
let avatar = json["avatar_url"] as? String ?? json["picture"] as? String
|
|
259
|
-
|
|
260
|
-
return AuthUser(
|
|
261
|
-
uid: uid,
|
|
262
|
-
email: email,
|
|
263
|
-
emailVerified: email != nil,
|
|
264
|
-
displayName: name,
|
|
265
|
-
photoURL: avatar,
|
|
266
|
-
phoneNumber: nil,
|
|
267
|
-
isAnonymous: false,
|
|
268
|
-
tenantId: nil,
|
|
269
|
-
providerData: [
|
|
270
|
-
UserInfo(
|
|
271
|
-
providerId: provider.rawValue,
|
|
272
|
-
uid: uid,
|
|
273
|
-
displayName: name,
|
|
274
|
-
email: email,
|
|
275
|
-
phoneNumber: nil,
|
|
276
|
-
photoURL: avatar
|
|
277
|
-
)
|
|
278
|
-
],
|
|
279
|
-
metadata: UserMetadata(
|
|
280
|
-
creationTime: nil,
|
|
281
|
-
lastSignInTime: ISO8601DateFormatter().string(from: Date()),
|
|
282
|
-
lastRefreshTime: nil
|
|
283
|
-
),
|
|
284
|
-
refreshToken: nil,
|
|
285
|
-
customClaims: nil
|
|
286
|
-
)
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
func signOut(options: JSObject?, completion: @escaping (Error?) -> Void) {
|
|
290
|
-
logger.info("Signing out from \(provider.rawValue)")
|
|
291
|
-
currentUser = nil
|
|
292
|
-
storage.deleteCredential(for: provider)
|
|
293
|
-
completion(nil)
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
func getCurrentUser(completion: @escaping (AuthUser?, Error?) -> Void) {
|
|
297
|
-
completion(currentUser, nil)
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
func refreshToken(options: JSObject?, completion: @escaping (AuthResult?, Error?) -> Void) {
|
|
301
|
-
logger.info("Refreshing \(provider.rawValue) token")
|
|
302
|
-
|
|
303
|
-
guard let credential = storage.getCredential(for: provider),
|
|
304
|
-
let refreshToken = credential.refreshToken else {
|
|
305
|
-
completion(nil, createAuthError(code: .tokenExpired, message: "No refresh token available"))
|
|
306
|
-
return
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
guard let clientId = clientId else {
|
|
310
|
-
completion(nil, createAuthError(code: .missingConfiguration, message: "Client ID is required"))
|
|
311
|
-
return
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
var request = URLRequest(url: URL(string: tokenUrl)!)
|
|
315
|
-
request.httpMethod = "POST"
|
|
316
|
-
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
317
|
-
request.setValue("application/json", forHTTPHeaderField: "Accept")
|
|
318
|
-
|
|
319
|
-
var body = "client_id=\(clientId)&refresh_token=\(refreshToken)&grant_type=refresh_token"
|
|
320
|
-
if let clientSecret = clientSecret {
|
|
321
|
-
body += "&client_secret=\(clientSecret)"
|
|
322
|
-
}
|
|
323
|
-
request.httpBody = body.data(using: .utf8)
|
|
324
|
-
|
|
325
|
-
URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
|
|
326
|
-
guard let self = self else { return }
|
|
327
|
-
|
|
328
|
-
DispatchQueue.main.async {
|
|
329
|
-
if let error = error {
|
|
330
|
-
completion(nil, self.createAuthError(code: .networkError, message: error.localizedDescription))
|
|
331
|
-
return
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
guard let data = data,
|
|
335
|
-
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
336
|
-
let accessToken = json["access_token"] as? String else {
|
|
337
|
-
completion(nil, self.createAuthError(code: .internalError, message: "Failed to refresh token"))
|
|
338
|
-
return
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
let newRefreshToken = json["refresh_token"] as? String ?? refreshToken
|
|
342
|
-
let expiresIn = json["expires_in"] as? Int
|
|
343
|
-
|
|
344
|
-
let newCredential = AuthCredential(
|
|
345
|
-
providerId: self.provider.rawValue,
|
|
346
|
-
signInMethod: "oauth",
|
|
347
|
-
accessToken: accessToken,
|
|
348
|
-
idToken: nil,
|
|
349
|
-
refreshToken: newRefreshToken,
|
|
350
|
-
expiresAt: expiresIn != nil ? Int(Date().timeIntervalSince1970 * 1000) + (expiresIn! * 1000) : nil,
|
|
351
|
-
tokenType: "Bearer",
|
|
352
|
-
scope: credential.scope,
|
|
353
|
-
rawNonce: nil
|
|
354
|
-
)
|
|
355
|
-
|
|
356
|
-
self.storage.saveCredential(newCredential, for: self.provider)
|
|
357
|
-
|
|
358
|
-
guard let user = self.currentUser else {
|
|
359
|
-
completion(nil, self.createAuthError(code: .internalError, message: "No current user"))
|
|
360
|
-
return
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
let result = AuthResult(
|
|
364
|
-
user: user,
|
|
365
|
-
credential: newCredential,
|
|
366
|
-
additionalUserInfo: nil,
|
|
367
|
-
operationType: "refresh"
|
|
368
|
-
)
|
|
369
|
-
|
|
370
|
-
completion(result, nil)
|
|
371
|
-
}
|
|
372
|
-
}.resume()
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
func isSupported(completion: @escaping (Bool, Error?) -> Void) {
|
|
376
|
-
completion(true, nil)
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
func linkAccount(credentials: JSObject?, options: JSObject?, completion: @escaping (AuthResult?, Error?) -> Void) {
|
|
380
|
-
signIn(credentials: credentials, options: options, completion: completion)
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
func unlinkAccount(completion: @escaping (Error?) -> Void) {
|
|
384
|
-
signOut(options: nil, completion: completion)
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
func getIdToken(forceRefresh: Bool, completion: @escaping (String?, Error?) -> Void) {
|
|
388
|
-
if let credential = storage.getCredential(for: provider),
|
|
389
|
-
let accessToken = credential.accessToken {
|
|
390
|
-
if forceRefresh {
|
|
391
|
-
refreshToken(options: nil) { result, error in
|
|
392
|
-
if let result = result {
|
|
393
|
-
completion(result.credential.accessToken, nil)
|
|
394
|
-
} else {
|
|
395
|
-
completion(nil, error)
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
} else {
|
|
399
|
-
completion(accessToken, nil)
|
|
400
|
-
}
|
|
401
|
-
} else {
|
|
402
|
-
completion(nil, createAuthError(code: .invalidUserToken, message: "No token found"))
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
func revokeAccess(token: String?, completion: @escaping (Error?) -> Void) {
|
|
407
|
-
signOut(options: nil, completion: completion)
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
// MARK: - ASWebAuthenticationPresentationContextProviding
|
|
411
|
-
|
|
412
|
-
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
|
413
|
-
return UIApplication.shared.windows.first { $0.isKeyWindow }!
|
|
414
|
-
}
|
|
415
|
-
}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
import Capacitor
|
|
3
|
-
|
|
4
|
-
class SlackAuthProvider: OAuthWebProvider {
|
|
5
|
-
|
|
6
|
-
init(config: AuthProviderConfig, storage: AuthStorage, logger: AuthLogger) {
|
|
7
|
-
super.init(
|
|
8
|
-
provider: .slack,
|
|
9
|
-
config: config,
|
|
10
|
-
storage: storage,
|
|
11
|
-
logger: logger,
|
|
12
|
-
authUrl: "https://slack.com/oauth/v2/authorize",
|
|
13
|
-
tokenUrl: "https://slack.com/api/oauth.v2.access",
|
|
14
|
-
userInfoUrl: "https://slack.com/api/users.identity",
|
|
15
|
-
defaultScopes: ["openid", "profile", "email"]
|
|
16
|
-
)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
override func parseUserInfo(json: [String: Any]) -> AuthUser {
|
|
20
|
-
// Slack returns user info nested under "user" key
|
|
21
|
-
let userJson = json["user"] as? [String: Any] ?? json
|
|
22
|
-
|
|
23
|
-
let uid = userJson["id"] as? String ?? ""
|
|
24
|
-
let email = userJson["email"] as? String
|
|
25
|
-
let name = userJson["name"] as? String
|
|
26
|
-
let realName = userJson["real_name"] as? String ?? name
|
|
27
|
-
|
|
28
|
-
var avatarUrl: String?
|
|
29
|
-
if let image = userJson["image_512"] as? String {
|
|
30
|
-
avatarUrl = image
|
|
31
|
-
} else if let image = userJson["image_192"] as? String {
|
|
32
|
-
avatarUrl = image
|
|
33
|
-
} else if let image = userJson["image_72"] as? String {
|
|
34
|
-
avatarUrl = image
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Get team info if available
|
|
38
|
-
let team = json["team"] as? [String: Any]
|
|
39
|
-
let teamId = team?["id"] as? String
|
|
40
|
-
|
|
41
|
-
return AuthUser(
|
|
42
|
-
uid: uid,
|
|
43
|
-
email: email,
|
|
44
|
-
emailVerified: email != nil,
|
|
45
|
-
displayName: realName,
|
|
46
|
-
photoURL: avatarUrl,
|
|
47
|
-
phoneNumber: nil,
|
|
48
|
-
isAnonymous: false,
|
|
49
|
-
tenantId: teamId,
|
|
50
|
-
providerData: [
|
|
51
|
-
UserInfo(
|
|
52
|
-
providerId: provider.rawValue,
|
|
53
|
-
uid: uid,
|
|
54
|
-
displayName: realName,
|
|
55
|
-
email: email,
|
|
56
|
-
phoneNumber: nil,
|
|
57
|
-
photoURL: avatarUrl
|
|
58
|
-
)
|
|
59
|
-
],
|
|
60
|
-
metadata: UserMetadata(
|
|
61
|
-
creationTime: nil,
|
|
62
|
-
lastSignInTime: ISO8601DateFormatter().string(from: Date()),
|
|
63
|
-
lastRefreshTime: nil
|
|
64
|
-
),
|
|
65
|
-
refreshToken: nil,
|
|
66
|
-
customClaims: teamId != nil ? ["teamId": teamId as Any] : nil
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
override func fetchUserInfo(accessToken: String, completion: @escaping (AuthUser?, Error?) -> Void) {
|
|
71
|
-
// Slack requires the token as a query parameter, not in Authorization header
|
|
72
|
-
var urlComponents = URLComponents(string: "https://slack.com/api/users.identity")!
|
|
73
|
-
urlComponents.queryItems = [URLQueryItem(name: "token", value: accessToken)]
|
|
74
|
-
|
|
75
|
-
guard let url = urlComponents.url else {
|
|
76
|
-
completion(nil, createAuthError(code: .internalError, message: "Failed to construct URL"))
|
|
77
|
-
return
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
var request = URLRequest(url: url)
|
|
81
|
-
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
82
|
-
|
|
83
|
-
URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
|
|
84
|
-
guard let self = self else { return }
|
|
85
|
-
|
|
86
|
-
if let error = error {
|
|
87
|
-
completion(nil, self.createAuthError(code: .networkError, message: error.localizedDescription))
|
|
88
|
-
return
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
guard let data = data,
|
|
92
|
-
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
|
|
93
|
-
completion(nil, self.createAuthError(code: .internalError, message: "Failed to parse user info"))
|
|
94
|
-
return
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Check if response is OK
|
|
98
|
-
if let ok = json["ok"] as? Bool, !ok {
|
|
99
|
-
let errorMsg = json["error"] as? String ?? "Unknown error"
|
|
100
|
-
completion(nil, self.createAuthError(code: .internalError, message: errorMsg))
|
|
101
|
-
return
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let user = self.parseUserInfo(json: json)
|
|
105
|
-
completion(user, nil)
|
|
106
|
-
}.resume()
|
|
107
|
-
}
|
|
108
|
-
}
|