|
| 1 | +import Foundation |
| 2 | + |
| 3 | +@frozen public enum SocialServiceName: String { |
| 4 | + case google |
| 5 | + case apple |
| 6 | +} |
| 7 | + |
| 8 | +extension AccountServiceRemoteREST { |
| 9 | + |
| 10 | + /// Connect to the specified social service via its OpenID Connect (JWT) token. |
| 11 | + /// |
| 12 | + /// - Parameters: |
| 13 | + /// - service The name of the social service. |
| 14 | + /// - token The OpenID Connect (JWT) ID token identifying the user on the social service. |
| 15 | + /// - connectParameters Dictionary containing additional endpoint parameters. Currently only used for the Apple service. |
| 16 | + /// - oAuthClientID The WPCOM REST API client ID. |
| 17 | + /// - oAuthClientSecret The WPCOM REST API client secret. |
| 18 | + /// - success The block that will be executed on success. |
| 19 | + /// - failure The block that will be executed on failure. |
| 20 | + public func connectToSocialService(_ service: SocialServiceName, |
| 21 | + serviceIDToken token: String, |
| 22 | + connectParameters: [String: AnyObject]? = nil, |
| 23 | + oAuthClientID: String, |
| 24 | + oAuthClientSecret: String, |
| 25 | + success: @escaping (() -> Void), |
| 26 | + failure: @escaping ((Error) -> Void)) { |
| 27 | + let path = self.path(forEndpoint: "me/social-login/connect", withVersion: ._1_1) |
| 28 | + |
| 29 | + var params = [ |
| 30 | + "client_id": oAuthClientID, |
| 31 | + "client_secret": oAuthClientSecret, |
| 32 | + "service": service.rawValue, |
| 33 | + "id_token": token |
| 34 | + ] as [String: AnyObject] |
| 35 | + |
| 36 | + if let connectParameters = connectParameters { |
| 37 | + params.merge(connectParameters, uniquingKeysWith: { (current, _) in current }) |
| 38 | + } |
| 39 | + |
| 40 | + wordPressComRESTAPI.post(path, parameters: params, success: { (_, _) in |
| 41 | + success() |
| 42 | + }, failure: { (error, _) in |
| 43 | + failure(error) |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + /// Get Apple connect parameters from provided account information. |
| 48 | + /// |
| 49 | + /// - Parameters: |
| 50 | + /// - email Email from Apple account. |
| 51 | + /// - fullName User's full name from Apple account. |
| 52 | + /// - Returns: Dictionary with endpoint parameters, to be used when connecting to social service. |
| 53 | + static public func appleSignInParameters(email: String, fullName: String) -> [String: AnyObject] { |
| 54 | + return [ |
| 55 | + "user_email": email as AnyObject, |
| 56 | + "user_name": fullName as AnyObject |
| 57 | + ] |
| 58 | + } |
| 59 | + |
| 60 | + /// Disconnect fromm the specified social service. |
| 61 | + /// |
| 62 | + /// - Parameters: |
| 63 | + /// - service The name of the social service. |
| 64 | + /// - oAuthClientID The WPCOM REST API client ID. |
| 65 | + /// - oAuthClientSecret The WPCOM REST API client secret. |
| 66 | + /// - success The block that will be executed on success. |
| 67 | + /// - failure The block that will be executed on failure. |
| 68 | + public func disconnectFromSocialService(_ service: SocialServiceName, oAuthClientID: String, oAuthClientSecret: String, success: @escaping(() -> Void), failure: @escaping((Error) -> Void)) { |
| 69 | + let path = self.path(forEndpoint: "me/social-login/disconnect", withVersion: ._1_1) |
| 70 | + let params = [ |
| 71 | + "client_id": oAuthClientID, |
| 72 | + "client_secret": oAuthClientSecret, |
| 73 | + "service": service.rawValue |
| 74 | + ] as [String: AnyObject] |
| 75 | + |
| 76 | + wordPressComRESTAPI.post(path, parameters: params, success: { (_, _) in |
| 77 | + success() |
| 78 | + }, failure: { (error, _) in |
| 79 | + failure(error) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments