-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathParseApple.swift
170 lines (152 loc) · 6.86 KB
/
ParseApple.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
//
// ParseApple.swift
// ParseSwift
//
// Created by Corey Baker on 1/14/21.
// Copyright © 2021 Parse Community. All rights reserved.
//
import Foundation
// swiftlint:disable line_length
/**
Provides utility functions for working with Apple User Authentication and `ParseUser`'s.
Be sure your Parse Server is configured for [sign in with Apple](https://docs.parseplatform.org/parse-server/guide/#configuring-parse-server-for-sign-in-with-apple).
For information on acquiring Apple sign-in credentials to use with `ParseApple`, refer to [Apple's Documentation](https://developer.apple.com/documentation/authenticationservices/implementing_user_authentication_with_sign_in_with_apple).
*/
public struct ParseApple<AuthenticatedUser: ParseUser>: ParseAuthentication {
/// Authentication keys required for Apple authentication.
enum AuthenticationKeys: String, Codable {
case id
case token
/// Properly makes an authData dictionary with the required keys.
/// - parameter user: Required id for the user.
/// - parameter identityToken: Required identity token for the user.
/// - returns: authData dictionary.
/// - throws: `ParseError` if the **identityToken** cannot be converted
/// to a string.
func makeDictionary(user: String,
identityToken: Data) throws -> [String: String] {
guard let identityTokenString = String(data: identityToken, encoding: .utf8) else {
throw ParseError(code: .unknownError, message: "Could not convert identityToken to String")
}
return [AuthenticationKeys.id.rawValue: user,
AuthenticationKeys.token.rawValue: identityTokenString]
}
/// 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,
authData[AuthenticationKeys.token.rawValue] != nil else {
return false
}
return true
}
}
public static var __type: String { // swiftlint:disable:this identifier_name
"apple"
}
public init() { }
}
// MARK: Login
public extension ParseApple {
/**
Login a `ParseUser` *asynchronously* using Apple authentication.
- parameter user: The `user` from `ASAuthorizationAppleIDCredential`.
- parameter identityToken: The **identityToken** from `ASAuthorizationAppleIDCredential`.
- 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(user: String,
identityToken: Data,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
guard let appleAuthData = try? AuthenticationKeys.id.makeDictionary(user: user, identityToken: identityToken) else {
callbackQueue.async {
completion(.failure(.init(code: .unknownError,
message: "Could not create authData.")))
}
return
}
login(authData: appleAuthData,
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\" and \"token\".")))
}
return
}
AuthenticatedUser.login(Self.__type,
authData: authData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
}
// MARK: Link
public extension ParseApple {
/**
Link the *current* `ParseUser` *asynchronously* using Apple authentication.
- parameter user: The `user` from `ASAuthorizationAppleIDCredential`.
- parameter identityToken: The **identityToken** from `ASAuthorizationAppleIDCredential`.
- 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(user: String,
identityToken: Data,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
guard let appleAuthData = try? AuthenticationKeys.id.makeDictionary(user: user, identityToken: identityToken) else {
callbackQueue.async {
completion(.failure(.init(code: .unknownError,
message: "Could not create authData.")))
}
return
}
link(authData: appleAuthData,
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 {
let error = ParseError(code: .unknownError,
message: "Should have authData in consisting of keys \"id\" and \"token\".")
callbackQueue.async {
completion(.failure(error))
}
return
}
AuthenticatedUser.link(Self.__type,
authData: authData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
}
// MARK: 3rd Party Authentication - ParseApple
public extension ParseUser {
/// An apple `ParseUser`.
static var apple: ParseApple<Self> {
ParseApple<Self>()
}
/// An apple `ParseUser`.
var apple: ParseApple<Self> {
Self.apple
}
}
// swiftlint:enable line_length