-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathParseFacebook.swift
240 lines (215 loc) · 10.5 KB
/
ParseFacebook.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
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
//
// ParseFacebook.swift
// ParseSwift
//
// Created by Abdulaziz Alhomaidhi on 3/18/21.
// Copyright © 2021 Parse Community. All rights reserved.
import Foundation
// swiftlint:disable line_length
/**
Provides utility functions for working with Facebook User Authentication and `ParseUser`'s.
Be sure your Parse Server is configured for [sign in with Facebook](https://docs.parseplatform.org/parse-server/guide/#facebook-authdata).
For information on acquiring Facebook sign-in credentials to use with `ParseFacebook`, refer to [Facebook's Documentation](https://developers.facebook.com/docs/facebook-login/limited-login).
*/
public struct ParseFacebook<AuthenticatedUser: ParseUser>: ParseAuthentication {
/// Authentication keys required for Facebook authentication.
enum AuthenticationKeys: String, Codable {
case id
case authenticationToken = "token"
case accessToken = "access_token"
case expirationDate = "expiration_date"
/// Properly makes an authData dictionary with the required keys.
/// - parameter userId: Required id for the user.
/// - parameter authenticationToken: Required identity token for Facebook limited login.
/// - parameter accessToken: Required identity token for Facebook graph API.
/// - parameter expiresIn: Optional expiration in seconds for Facebook login.
/// - returns: authData dictionary.
func makeDictionary(userId: String,
accessToken: String?,
authenticationToken: String?,
expiresIn: Int? = nil) -> [String: String] {
var returnDictionary = [AuthenticationKeys.id.rawValue: userId]
if let expiresIn = expiresIn,
let expirationDate = Calendar.current.date(byAdding: .second,
value: expiresIn,
to: Date()) {
let dateString = ParseCoding.dateFormatter.string(from: expirationDate)
returnDictionary[AuthenticationKeys.expirationDate.rawValue] = dateString
}
if let accessToken = accessToken {
returnDictionary[AuthenticationKeys.accessToken.rawValue] = accessToken
} else if let authenticationToken = authenticationToken {
returnDictionary[AuthenticationKeys.authenticationToken.rawValue] = authenticationToken
}
return returnDictionary
}
/// Verifies all mandatory keys are in authData.
/// - parameter authData: Dictionary containing key/values.
/// - returns: **true** if all the mandatory keys are present, **false** otherwise.
func verifyMandatoryKeys(authData: [String: String]) -> Bool {
guard authData[AuthenticationKeys.id.rawValue] != nil else {
return false
}
if authData[AuthenticationKeys.accessToken.rawValue] != nil ||
authData[AuthenticationKeys.authenticationToken.rawValue] != nil {
return true
}
return false
}
}
public static var __type: String { // swiftlint:disable:this identifier_name
"facebook"
}
public init() { }
}
// MARK: Login
public extension ParseFacebook {
/**
Login a `ParseUser` *asynchronously* using Facebook authentication for limited login.
- parameter userId: The `Facebook userId` from **FacebookSDK**.
- parameter authenticationToken: The `authenticationToken` from **FacebookSDK**.
- parameter expiresIn: Optional expiration in seconds for Facebook login.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: The block to execute.
*/
func login(userId: String,
authenticationToken: String,
expiresIn: Int? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
let facebookAuthData = AuthenticationKeys.id
.makeDictionary(userId: userId, accessToken: nil,
authenticationToken: authenticationToken,
expiresIn: expiresIn)
login(authData: facebookAuthData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
/**
Login a `ParseUser` *asynchronously* using Facebook authentication for graph API login.
- parameter userId: The `Facebook userId` from **FacebookSDK**.
- parameter accessToken: The `accessToken` from **FacebookSDK**.
- parameter expiresIn: Optional expiration in seconds for Facebook login.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: The block to execute.
*/
func login(userId: String,
accessToken: String,
expiresIn: Int? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
let facebookAuthData = AuthenticationKeys.id
.makeDictionary(userId: userId,
accessToken: accessToken,
authenticationToken: nil,
expiresIn: expiresIn)
login(authData: facebookAuthData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
func login(authData: [String: String],
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else {
callbackQueue.async {
completion(.failure(.init(code: .unknownError,
message: "Should have authData in consisting of keys \"id\", \"expirationDate\" and \"authenticationToken\" or \"accessToken\".")))
}
return
}
AuthenticatedUser.login(Self.__type,
authData: authData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
}
// MARK: Link
public extension ParseFacebook {
/**
Link the *current* `ParseUser` *asynchronously* using Facebook authentication for limited login.
- parameter userId: The **id** from **FacebookSDK**.
- parameter authenticationToken: The `authenticationToken` from **FacebookSDK**.
- parameter expiresIn: Optional expiration in seconds for Facebook login.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: The block to execute.
*/
func link(userId: String,
authenticationToken: String,
expiresIn: Int? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
let facebookAuthData = AuthenticationKeys.id
.makeDictionary(userId: userId,
accessToken: nil,
authenticationToken: authenticationToken,
expiresIn: expiresIn)
link(authData: facebookAuthData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
/**
Link the *current* `ParseUser` *asynchronously* using Facebook authentication for graph API login.
- parameter userId: The **id** from **FacebookSDK**.
- parameter accessToken: The `accessToken` from **FacebookSDK**.
- parameter expiresIn: Optional expiration in seconds for Facebook login.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: The block to execute.
*/
func link(userId: String,
accessToken: String,
expiresIn: Int? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
let facebookAuthData = AuthenticationKeys.id
.makeDictionary(userId: userId,
accessToken: accessToken,
authenticationToken: nil,
expiresIn: expiresIn)
link(authData: facebookAuthData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
func link(authData: [String: String],
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else {
callbackQueue.async {
completion(.failure(.init(code: .unknownError,
message: "Should have authData in consisting of keys \"id\", \"expirationDate\" and \"authenticationToken\" or \"accessToken\".")))
}
return
}
AuthenticatedUser.link(Self.__type,
authData: authData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
}
// MARK: 3rd Party Authentication - ParseFacebook
public extension ParseUser {
/// A facebook `ParseUser`.
static var facebook: ParseFacebook<Self> {
ParseFacebook<Self>()
}
/// An facebook `ParseUser`.
var facebook: ParseFacebook<Self> {
Self.facebook
}
}
// swiftlint:enable line_length