Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.
Merged
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions WordPressAuthenticator.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions WordPressAuthenticator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand All @@ -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)";
Expand Down Expand Up @@ -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)";
Expand Down Expand Up @@ -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)";
Expand Down
37 changes: 36 additions & 1 deletion WordPressAuthenticator/Services/SignupService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import WordPressShared
import WordPressKit



/// SignupService: Responsible for creating a new WPCom user and blog.
///
class SignupService {
Expand Down Expand Up @@ -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)
})
}

}


Expand Down
40 changes: 35 additions & 5 deletions WordPressAuthenticator/Signin/AppleAuthenticator.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import AuthenticationServices

#if XCODE11

class AppleAuthenticator: NSObject {

// MARK: - Properties
Expand All @@ -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()
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know PersonNameComponentsFormatter but I wonder if we should use the static localizedString method and specify the style of name

PersonNameComponentsFormatter.localizedString(from: components,
                                              style: .medium,
                                              options: [])

🤔

Copy link
Contributor Author

@ScoutHarris ScoutHarris Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default style is medium, so specifying that shouldn't be necessary. But, ya, I should have used localizedString. Thanks. I'll change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, reading the documentation again, I don't think a change is necessary. string seems to return the name in the correct style for the language. localizedString is for specifying the format and options. Which we don't need to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, great! Thanks for reading the docs 😅

}

}

#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)")
}

}
Expand All @@ -64,4 +93,5 @@ extension AppleAuthenticator: ASAuthorizationControllerPresentationContextProvid
return showFromViewController?.view.window ?? UIWindow()
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class LoginPrologueViewController: LoginViewController {
}

private func appleTapped() {
#if XCODE11
AppleAuthenticator.sharedInstance.showFrom(viewController: self)
#endif
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down