diff --git a/Podfile b/Podfile index 33236914e..ce44697cf 100644 --- a/Podfile +++ b/Podfile @@ -12,7 +12,7 @@ def wordpress_authenticator_pods ## pod 'Gridicons', '~> 0.15' pod 'WordPressUI', '~> 1.3' - pod 'WordPressKit', '~> 4.1' + pod 'WordPressKit', '~> 4.5.0-beta.1' pod 'WordPressShared', '~> 1.8' ## Third party libraries diff --git a/Podfile.lock b/Podfile.lock index 089c9d6e5..83d1b3cf2 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -44,7 +44,7 @@ PODS: - Specta (1.0.7) - SVProgressHUD (2.2.5) - UIDeviceIdentifier (1.1.4) - - WordPressKit (4.1.2): + - WordPressKit (4.5.0-beta.1): - Alamofire (~> 4.7.3) - CocoaLumberjack (~> 3.4) - NSObject-SafeExpectations (= 0.0.3) @@ -71,7 +71,7 @@ DEPENDENCIES: - OHHTTPStubs/Swift (= 8.0.0) - Specta (= 1.0.7) - SVProgressHUD (= 2.2.5) - - WordPressKit (~> 4.1) + - WordPressKit (~> 4.5.0-beta.1) - WordPressShared (~> 1.8) - WordPressUI (~> 1.3) @@ -117,11 +117,11 @@ SPEC CHECKSUMS: Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66 SVProgressHUD: 1428aafac632c1f86f62aa4243ec12008d7a51d6 UIDeviceIdentifier: 8f8a24b257a4d978c8d40ad1e7355b944ffbfa8c - WordPressKit: 09a28afc17dc63d35b6bd76c69fa94a7049183eb + WordPressKit: bfb5e77a32e66b19ee304bdffd5b7128a7fe4ede WordPressShared: 34f7a1386d28d7e4650c1a225c554ee024401ca3 WordPressUI: 0ea6df25bf6e63f0619376fa23870177cb37646f wpxmlrpc: 6ba55c773cfa27083ae4a2173e69b19f46da98e2 -PODFILE CHECKSUM: 23e5845cc72e0e7b189449e9da19a8473b5f60b0 +PODFILE CHECKSUM: e2a10fcbbc81f9e30af60797f89b8315a6819211 COCOAPODS: 1.6.1 diff --git a/WordPressAuthenticator.podspec b/WordPressAuthenticator.podspec index 0a9dde299..81b57b7e6 100644 --- a/WordPressAuthenticator.podspec +++ b/WordPressAuthenticator.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "WordPressAuthenticator" - s.version = "1.8.0-beta.5" + s.version = "1.8.0-beta.6" s.summary = "WordPressAuthenticator implements an easy and elegant way to authenticate your WordPress Apps." s.description = <<-DESC @@ -39,6 +39,6 @@ Pod::Spec.new do |s| s.dependency 'Gridicons', '~> 0.15' s.dependency 'GoogleSignIn', '~> 4.4' s.dependency 'WordPressUI', '~> 1.3' - s.dependency 'WordPressKit', '~> 4.1' + s.dependency 'WordPressKit', '~> 4.5.0-beta.1' s.dependency 'WordPressShared', '~> 1.8' end diff --git a/WordPressAuthenticator.xcodeproj/project.pbxproj b/WordPressAuthenticator.xcodeproj/project.pbxproj index ad6a1060d..7fa3048ed 100644 --- a/WordPressAuthenticator.xcodeproj/project.pbxproj +++ b/WordPressAuthenticator.xcodeproj/project.pbxproj @@ -1081,7 +1081,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = WordPressAuthenticator/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; @@ -1105,7 +1105,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = WordPressAuthenticator/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; @@ -1215,7 +1215,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = WordPressAuthenticator/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; @@ -1310,7 +1310,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = WordPressAuthenticator/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; diff --git a/WordPressAuthenticator/Services/SignupService.swift b/WordPressAuthenticator/Services/SignupService.swift index f348d5006..bf97e0764 100644 --- a/WordPressAuthenticator/Services/SignupService.swift +++ b/WordPressAuthenticator/Services/SignupService.swift @@ -4,7 +4,6 @@ import WordPressShared import WordPressKit - /// SignupService: Responsible for creating a new WPCom user and blog. /// class SignupService { @@ -39,6 +38,42 @@ class SignupService { failure(error ?? SignupError.unknown) }) } + + /// Create a new WPcom account using Apple ID + /// + /// - Parameters: + /// - token: Token provided by Apple. + /// - email: Email provided by Apple. + /// - fullName: Formatted full name provided by Apple. + /// - success: Block called when account is created successfully. + /// - failure: Block called when account creation fails. + /// + func createWPComUserWithApple(token: String, + email: String, + fullName: String?, + success: @escaping (_ newAccount: Bool, _ username: String, _ wpcomToken: String) -> Void, + failure: @escaping (_ error: Error) -> Void) { + let remote = WordPressComServiceRemote(wordPressComRestApi: anonymousAPI) + + remote.createWPComAccount(withApple: token, + andEmail: email, + andFullName: fullName, + andClientID: configuration.wpcomClientId, + andClientSecret: configuration.wpcomSecret, + success: { response in + guard let username = response?[ResponseKeys.username] as? String, + let bearer_token = response?[ResponseKeys.bearerToken] as? String else { + failure(SignupError.unknown) + return + } + + let createdAccount = (response?[ResponseKeys.createdAccount] as? Int ?? 0) == 1 + success(createdAccount, username, bearer_token) + }, failure: { error in + failure(error ?? SignupError.unknown) + }) + } + } diff --git a/WordPressAuthenticator/Signin/AppleAuthenticator.swift b/WordPressAuthenticator/Signin/AppleAuthenticator.swift index 71593fb91..2f4bfbf4e 100644 --- a/WordPressAuthenticator/Signin/AppleAuthenticator.swift +++ b/WordPressAuthenticator/Signin/AppleAuthenticator.swift @@ -1,6 +1,8 @@ import Foundation import AuthenticationServices +#if XCODE11 + class AppleAuthenticator: NSObject { // MARK: - Properties @@ -21,7 +23,6 @@ class AppleAuthenticator: NSObject { private extension AppleAuthenticator { func requestAuthorization() { - #if XCODE11 if #available(iOS 13.0, *) { let provider = ASAuthorizationAppleIDProvider() let request = provider.createRequest() @@ -34,26 +35,54 @@ private extension AppleAuthenticator { controller.performRequests() } - #endif + } + + /// Creates a WordPress.com account with the Apple ID + /// + @available(iOS 13.0, *) + func createWordPressComUser(appleCredentials: ASAuthorizationAppleIDCredential) { + guard let identityToken = appleCredentials.identityToken, + let email = appleCredentials.email else { + DDLogError("Apple Authenticator: invalid Apple credentials.") + return + } + + let service = SignupService() + service.createWPComUserWithApple(token: identityToken.base64EncodedString(), + email: email, + fullName: fullName(from: appleCredentials.fullName), + success: { [weak self] accountCreated, wpcomUsername, wpcomToken in + NSLog("Apple Authenticator: createWPComUserWithApple success. accountCreated: ", accountCreated) + }, failure: { [weak self] error in + DDLogError("Apple Authenticator: createWPComUserWithApple failure. error: \(error)") + }) + } + + // MARK: - Helpers + + func fullName(from components: PersonNameComponents?) -> String { + guard let name = components else { + return "" + } + return PersonNameComponentsFormatter().string(from: name) } } -#if XCODE11 @available(iOS 13.0, *) extension AppleAuthenticator: ASAuthorizationControllerDelegate { func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { switch authorization.credential { case let credentials as ASAuthorizationAppleIDCredential: - NSLog("Apple Authenticator credentials: \(String(describing: credentials.email)), \(String(describing: credentials.fullName))") + createWordPressComUser(appleCredentials: credentials) default: break } } func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { - NSLog("Apple Authenticator didCompleteWithError: \(error)") + DDLogError("Apple Authenticator: didCompleteWithError: \(error)") } } @@ -64,4 +93,5 @@ extension AppleAuthenticator: ASAuthorizationControllerPresentationContextProvid return showFromViewController?.view.window ?? UIWindow() } } + #endif diff --git a/WordPressAuthenticator/Signin/LoginPrologueViewController.swift b/WordPressAuthenticator/Signin/LoginPrologueViewController.swift index ff27f9350..436764150 100644 --- a/WordPressAuthenticator/Signin/LoginPrologueViewController.swift +++ b/WordPressAuthenticator/Signin/LoginPrologueViewController.swift @@ -115,7 +115,9 @@ class LoginPrologueViewController: LoginViewController { } private func appleTapped() { + #if XCODE11 AppleAuthenticator.sharedInstance.showFrom(viewController: self) + #endif } } diff --git a/WordPressAuthenticator/Signin/LoginSiteAddressViewController.swift b/WordPressAuthenticator/Signin/LoginSiteAddressViewController.swift index 6d775faca..5e221abbd 100644 --- a/WordPressAuthenticator/Signin/LoginSiteAddressViewController.swift +++ b/WordPressAuthenticator/Signin/LoginSiteAddressViewController.swift @@ -286,7 +286,9 @@ class LoginSiteAddressViewController: LoginViewController, NUXKeyboardResponder } private func appleTapped() { + #if XCODE11 AppleAuthenticator.sharedInstance.showFrom(viewController: self) + #endif } /// Whether the form can be submitted.