Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit d0f6bda

Browse files
authored
Merge pull request #116 from wordpress-mobile/feature/siwa_create_account
[SIWA] Add methods to create WP account
2 parents 7922c39 + 17b41b0 commit d0f6bda

File tree

8 files changed

+86
-17
lines changed

8 files changed

+86
-17
lines changed

Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def wordpress_authenticator_pods
1212
##
1313
pod 'Gridicons', '~> 0.15'
1414
pod 'WordPressUI', '~> 1.3'
15-
pod 'WordPressKit', '~> 4.1'
15+
pod 'WordPressKit', '~> 4.5.0-beta.1'
1616
pod 'WordPressShared', '~> 1.8'
1717

1818
## Third party libraries

Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ PODS:
4444
- Specta (1.0.7)
4545
- SVProgressHUD (2.2.5)
4646
- UIDeviceIdentifier (1.1.4)
47-
- WordPressKit (4.1.2):
47+
- WordPressKit (4.5.0-beta.1):
4848
- Alamofire (~> 4.7.3)
4949
- CocoaLumberjack (~> 3.4)
5050
- NSObject-SafeExpectations (= 0.0.3)
@@ -71,7 +71,7 @@ DEPENDENCIES:
7171
- OHHTTPStubs/Swift (= 8.0.0)
7272
- Specta (= 1.0.7)
7373
- SVProgressHUD (= 2.2.5)
74-
- WordPressKit (~> 4.1)
74+
- WordPressKit (~> 4.5.0-beta.1)
7575
- WordPressShared (~> 1.8)
7676
- WordPressUI (~> 1.3)
7777

@@ -117,11 +117,11 @@ SPEC CHECKSUMS:
117117
Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66
118118
SVProgressHUD: 1428aafac632c1f86f62aa4243ec12008d7a51d6
119119
UIDeviceIdentifier: 8f8a24b257a4d978c8d40ad1e7355b944ffbfa8c
120-
WordPressKit: 09a28afc17dc63d35b6bd76c69fa94a7049183eb
120+
WordPressKit: bfb5e77a32e66b19ee304bdffd5b7128a7fe4ede
121121
WordPressShared: 34f7a1386d28d7e4650c1a225c554ee024401ca3
122122
WordPressUI: 0ea6df25bf6e63f0619376fa23870177cb37646f
123123
wpxmlrpc: 6ba55c773cfa27083ae4a2173e69b19f46da98e2
124124

125-
PODFILE CHECKSUM: 23e5845cc72e0e7b189449e9da19a8473b5f60b0
125+
PODFILE CHECKSUM: e2a10fcbbc81f9e30af60797f89b8315a6819211
126126

127127
COCOAPODS: 1.6.1

WordPressAuthenticator.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WordPressAuthenticator"
3-
s.version = "1.8.0-beta.5"
3+
s.version = "1.8.0-beta.6"
44
s.summary = "WordPressAuthenticator implements an easy and elegant way to authenticate your WordPress Apps."
55

66
s.description = <<-DESC
@@ -39,6 +39,6 @@ Pod::Spec.new do |s|
3939
s.dependency 'Gridicons', '~> 0.15'
4040
s.dependency 'GoogleSignIn', '~> 4.4'
4141
s.dependency 'WordPressUI', '~> 1.3'
42-
s.dependency 'WordPressKit', '~> 4.1'
42+
s.dependency 'WordPressKit', '~> 4.5.0-beta.1'
4343
s.dependency 'WordPressShared', '~> 1.8'
4444
end

WordPressAuthenticator.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@
10811081
DYLIB_INSTALL_NAME_BASE = "@rpath";
10821082
INFOPLIST_FILE = WordPressAuthenticator/Info.plist;
10831083
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
1084-
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
1084+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
10851085
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
10861086
PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator;
10871087
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
@@ -1105,7 +1105,7 @@
11051105
DYLIB_INSTALL_NAME_BASE = "@rpath";
11061106
INFOPLIST_FILE = WordPressAuthenticator/Info.plist;
11071107
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
1108-
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
1108+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
11091109
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
11101110
PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator;
11111111
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
@@ -1215,7 +1215,7 @@
12151215
DYLIB_INSTALL_NAME_BASE = "@rpath";
12161216
INFOPLIST_FILE = WordPressAuthenticator/Info.plist;
12171217
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
1218-
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
1218+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
12191219
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
12201220
PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator;
12211221
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
@@ -1310,7 +1310,7 @@
13101310
DYLIB_INSTALL_NAME_BASE = "@rpath";
13111311
INFOPLIST_FILE = WordPressAuthenticator/Info.plist;
13121312
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
1313-
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
1313+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
13141314
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
13151315
PRODUCT_BUNDLE_IDENTIFIER = com.automattic.WordPressAuthenticator;
13161316
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";

WordPressAuthenticator/Services/SignupService.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import WordPressShared
44
import WordPressKit
55

66

7-
87
/// SignupService: Responsible for creating a new WPCom user and blog.
98
///
109
class SignupService {
@@ -39,6 +38,42 @@ class SignupService {
3938
failure(error ?? SignupError.unknown)
4039
})
4140
}
41+
42+
/// Create a new WPcom account using Apple ID
43+
///
44+
/// - Parameters:
45+
/// - token: Token provided by Apple.
46+
/// - email: Email provided by Apple.
47+
/// - fullName: Formatted full name provided by Apple.
48+
/// - success: Block called when account is created successfully.
49+
/// - failure: Block called when account creation fails.
50+
///
51+
func createWPComUserWithApple(token: String,
52+
email: String,
53+
fullName: String?,
54+
success: @escaping (_ newAccount: Bool, _ username: String, _ wpcomToken: String) -> Void,
55+
failure: @escaping (_ error: Error) -> Void) {
56+
let remote = WordPressComServiceRemote(wordPressComRestApi: anonymousAPI)
57+
58+
remote.createWPComAccount(withApple: token,
59+
andEmail: email,
60+
andFullName: fullName,
61+
andClientID: configuration.wpcomClientId,
62+
andClientSecret: configuration.wpcomSecret,
63+
success: { response in
64+
guard let username = response?[ResponseKeys.username] as? String,
65+
let bearer_token = response?[ResponseKeys.bearerToken] as? String else {
66+
failure(SignupError.unknown)
67+
return
68+
}
69+
70+
let createdAccount = (response?[ResponseKeys.createdAccount] as? Int ?? 0) == 1
71+
success(createdAccount, username, bearer_token)
72+
}, failure: { error in
73+
failure(error ?? SignupError.unknown)
74+
})
75+
}
76+
4277
}
4378

4479

WordPressAuthenticator/Signin/AppleAuthenticator.swift

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Foundation
22
import AuthenticationServices
33

4+
#if XCODE11
5+
46
class AppleAuthenticator: NSObject {
57

68
// MARK: - Properties
@@ -21,7 +23,6 @@ class AppleAuthenticator: NSObject {
2123
private extension AppleAuthenticator {
2224

2325
func requestAuthorization() {
24-
#if XCODE11
2526
if #available(iOS 13.0, *) {
2627
let provider = ASAuthorizationAppleIDProvider()
2728
let request = provider.createRequest()
@@ -34,26 +35,54 @@ private extension AppleAuthenticator {
3435
controller.performRequests()
3536

3637
}
37-
#endif
38+
}
39+
40+
/// Creates a WordPress.com account with the Apple ID
41+
///
42+
@available(iOS 13.0, *)
43+
func createWordPressComUser(appleCredentials: ASAuthorizationAppleIDCredential) {
44+
guard let identityToken = appleCredentials.identityToken,
45+
let email = appleCredentials.email else {
46+
DDLogError("Apple Authenticator: invalid Apple credentials.")
47+
return
48+
}
49+
50+
let service = SignupService()
51+
service.createWPComUserWithApple(token: identityToken.base64EncodedString(),
52+
email: email,
53+
fullName: fullName(from: appleCredentials.fullName),
54+
success: { [weak self] accountCreated, wpcomUsername, wpcomToken in
55+
NSLog("Apple Authenticator: createWPComUserWithApple success. accountCreated: ", accountCreated)
56+
}, failure: { [weak self] error in
57+
DDLogError("Apple Authenticator: createWPComUserWithApple failure. error: \(error)")
58+
})
59+
}
60+
61+
// MARK: - Helpers
62+
63+
func fullName(from components: PersonNameComponents?) -> String {
64+
guard let name = components else {
65+
return ""
66+
}
67+
return PersonNameComponentsFormatter().string(from: name)
3868
}
3969

4070
}
4171

42-
#if XCODE11
4372
@available(iOS 13.0, *)
4473
extension AppleAuthenticator: ASAuthorizationControllerDelegate {
4574

4675
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
4776
switch authorization.credential {
4877
case let credentials as ASAuthorizationAppleIDCredential:
49-
NSLog("Apple Authenticator credentials: \(String(describing: credentials.email)), \(String(describing: credentials.fullName))")
78+
createWordPressComUser(appleCredentials: credentials)
5079
default:
5180
break
5281
}
5382
}
5483

5584
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
56-
NSLog("Apple Authenticator didCompleteWithError: \(error)")
85+
DDLogError("Apple Authenticator: didCompleteWithError: \(error)")
5786
}
5887

5988
}
@@ -64,4 +93,5 @@ extension AppleAuthenticator: ASAuthorizationControllerPresentationContextProvid
6493
return showFromViewController?.view.window ?? UIWindow()
6594
}
6695
}
96+
6797
#endif

WordPressAuthenticator/Signin/LoginPrologueViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ class LoginPrologueViewController: LoginViewController {
115115
}
116116

117117
private func appleTapped() {
118+
#if XCODE11
118119
AppleAuthenticator.sharedInstance.showFrom(viewController: self)
120+
#endif
119121
}
120122

121123
}

WordPressAuthenticator/Signin/LoginSiteAddressViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ class LoginSiteAddressViewController: LoginViewController, NUXKeyboardResponder
286286
}
287287

288288
private func appleTapped() {
289+
#if XCODE11
289290
AppleAuthenticator.sharedInstance.showFrom(viewController: self)
291+
#endif
290292
}
291293

292294
/// Whether the form can be submitted.

0 commit comments

Comments
 (0)