-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathParseGoogle.swift
176 lines (155 loc) · 6.86 KB
/
ParseGoogle.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
//
// ParseGoogle.swift
// ParseSwift
//
// Created by Corey Baker on 1/1/22.
// Copyright © 2022 Parse Community. All rights reserved.
//
import Foundation
// swiftlint:disable line_length
/**
Provides utility functions for working with Google User Authentication and `ParseUser`'s.
Be sure your Parse Server is configured for [sign in with Google](https://docs.parseplatform.org/parse-server/guide/#google-authdata).
For information on acquiring Google sign-in credentials to use with `ParseGoogle`, refer to [Google's Documentation](https://developers.google.com/identity/protocols/oauth2).
*/
public struct ParseGoogle<AuthenticatedUser: ParseUser>: ParseAuthentication {
/// Authentication keys required for Google authentication.
enum AuthenticationKeys: String, Codable {
case id
case idToken = "id_token"
case accessToken = "access_token"
/// Properly makes an authData dictionary with the required keys.
/// - parameter id: Required id for the user.
/// - parameter idToken: Optional identity token for Google.
/// - parameter accessToken: Optional identity token for Google.
/// - returns: authData dictionary.
func makeDictionary(id: String,
idToken: String? = nil,
accessToken: String? = nil) -> [String: String] {
var returnDictionary = [AuthenticationKeys.id.rawValue: id]
if let accessToken = accessToken {
returnDictionary[AuthenticationKeys.accessToken.rawValue] = accessToken
} else if let idToken = idToken {
returnDictionary[AuthenticationKeys.idToken.rawValue] = idToken
}
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.idToken.rawValue] != nil {
return true
}
return false
}
}
public static var __type: String { // swiftlint:disable:this identifier_name
"google"
}
public init() { }
}
// MARK: Login
public extension ParseGoogle {
/**
Login a `ParseUser` *asynchronously* using Google authentication for graph API login.
- parameter id: The `id` from **Google**.
- parameter idToken: Optional **id_token** from **Google**.
- parameter accessToken: Optional **access_token** from **Google**.
- 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(id: String,
idToken: String? = nil,
accessToken: String? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
let googleAuthData = AuthenticationKeys.id
.makeDictionary(id: id,
idToken: idToken,
accessToken: accessToken)
login(authData: googleAuthData,
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\", \"idToken\" or \"accessToken\".")))
}
return
}
AuthenticatedUser.login(Self.__type,
authData: authData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
}
// MARK: Link
public extension ParseGoogle {
/**
Link the *current* `ParseUser` *asynchronously* using Google authentication for graph API login.
- parameter id: The **id** from **Google**.
- parameter idToken: Optional **id_token** from **Google**.
- parameter accessToken: Optional **access_token** from **Google**.
- 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(id: String,
idToken: String? = nil,
accessToken: String? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
let googleAuthData = AuthenticationKeys.id
.makeDictionary(id: id,
idToken: idToken,
accessToken: accessToken)
link(authData: googleAuthData,
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\", \"idToken\" or \"accessToken\".")))
}
return
}
AuthenticatedUser.link(Self.__type,
authData: authData,
options: options,
callbackQueue: callbackQueue,
completion: completion)
}
}
// MARK: 3rd Party Authentication - ParseGoogle
public extension ParseUser {
/// A google `ParseUser`.
static var google: ParseGoogle<Self> {
ParseGoogle<Self>()
}
/// An google `ParseUser`.
var google: ParseGoogle<Self> {
Self.google
}
}
// swiftlint:enable line_length