-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathAccountServiceRemoteREST+SocialService.swift
More file actions
82 lines (73 loc) · 3.42 KB
/
Copy pathAccountServiceRemoteREST+SocialService.swift
File metadata and controls
82 lines (73 loc) · 3.42 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
import Foundation
@frozen public enum SocialServiceName: String {
case google
case apple
}
extension AccountServiceRemoteREST {
/// Connect to the specified social service via its OpenID Connect (JWT) token.
///
/// - Parameters:
/// - service The name of the social service.
/// - token The OpenID Connect (JWT) ID token identifying the user on the social service.
/// - connectParameters Dictionary containing additional endpoint parameters. Currently only used for the Apple service.
/// - oAuthClientID The WPCOM REST API client ID.
/// - oAuthClientSecret The WPCOM REST API client secret.
/// - success The block that will be executed on success.
/// - failure The block that will be executed on failure.
public func connectToSocialService(_ service: SocialServiceName,
serviceIDToken token: String,
connectParameters: [String: AnyObject]? = nil,
oAuthClientID: String,
oAuthClientSecret: String,
success: @escaping (() -> Void),
failure: @escaping ((Error) -> Void)) {
let path = self.path(forEndpoint: "me/social-login/connect", withVersion: ._1_1)
var params = [
"client_id": oAuthClientID,
"client_secret": oAuthClientSecret,
"service": service.rawValue,
"id_token": token
] as [String: AnyObject]
if let connectParameters {
params.merge(connectParameters, uniquingKeysWith: { current, _ in current })
}
wordPressComRESTAPI.post(path, parameters: params, success: { _, _ in
success()
}, failure: { error, _ in
failure(error)
})
}
/// Get Apple connect parameters from provided account information.
///
/// - Parameters:
/// - email Email from Apple account.
/// - fullName User's full name from Apple account.
/// - Returns: Dictionary with endpoint parameters, to be used when connecting to social service.
public static func appleSignInParameters(email: String, fullName: String) -> [String: AnyObject] {
return [
"user_email": email as AnyObject,
"user_name": fullName as AnyObject
]
}
/// Disconnect fromm the specified social service.
///
/// - Parameters:
/// - service The name of the social service.
/// - oAuthClientID The WPCOM REST API client ID.
/// - oAuthClientSecret The WPCOM REST API client secret.
/// - success The block that will be executed on success.
/// - failure The block that will be executed on failure.
public func disconnectFromSocialService(_ service: SocialServiceName, oAuthClientID: String, oAuthClientSecret: String, success: @escaping(() -> Void), failure: @escaping((Error) -> Void)) {
let path = self.path(forEndpoint: "me/social-login/disconnect", withVersion: ._1_1)
let params = [
"client_id": oAuthClientID,
"client_secret": oAuthClientSecret,
"service": service.rawValue
] as [String: AnyObject]
wordPressComRESTAPI.post(path, parameters: params, success: { _, _ in
success()
}, failure: { error, _ in
failure(error)
})
}
}