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

Commit c0d1606

Browse files
authored
Introduce NewGoogleAuthenticator for SDK-less authentication (#743)
2 parents 015e698 + 76fd3c4 commit c0d1606

20 files changed

+508
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ _None._
1818
1919
### New Features
2020
21-
_None._
21+
- It's now possible to authenticate with a Google account without using the Google SDK, via the `googleLoginWithoutSDK` configuration. [#743]
2222
2323
### Bug Fixes
2424

Demo/AuthenticatorDemo/AppDelegate.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import UIKit
2+
import WordPressKit
23

34
@main
45
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -10,7 +11,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1011
// crash the app if the value it finds is nil.
1112
var window: UIWindow?
1213

14+
let logger = ConsoleLogger()
15+
1316
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
17+
WPKitSetLoggingDelegate(logger)
1418
return true
1519
}
1620

Demo/AuthenticatorDemo/ViewController+WordPressAuthenticationDelegate.swift

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import WebKit
12
import WordPressAuthenticator
3+
import WordPressKit
24

35
extension ViewController: WordPressAuthenticatorDelegate {
46

@@ -68,11 +70,7 @@ extension ViewController: WordPressAuthenticatorDelegate {
6870

6971
func sync(credentials: AuthenticatorCredentials, onCompletion: @escaping () -> Void) {
7072
dismiss(animated: true) { [weak self] in
71-
self?.presentAlert(
72-
title: "Authentication Successful",
73-
message: "Next step will be syncing credentials",
74-
onDismiss: {}
75-
)
73+
self?.sync(credentials: credentials)
7674
}
7775
}
7876

@@ -90,3 +88,46 @@ extension ViewController: WordPressAuthenticatorDelegate {
9088
print(error)
9189
}
9290
}
91+
92+
extension ViewController {
93+
94+
// This is just so we can avoid nesting within a dismiss block and the weak self dance.
95+
//
96+
// See WordPress iOS
97+
//
98+
// - WordPressAuthenticationManager sync(credentials:, onCompletion:)
99+
// - WordPressAuthenticationManager syncWPCom(authToken:, isJetpackLogin:, onCompletion:)
100+
// - AccountService createOrUpdateAccountWithAuthToken:success:failure:
101+
private func sync(credentials: AuthenticatorCredentials) {
102+
switch (credentials.wpcom, credentials.wporg) {
103+
case (.none, .none), (.some, .some):
104+
fatalError("Inconsistent state!")
105+
case (.none, .some):
106+
fatalError("Not implemented yet")
107+
case (.some(let wpComCredentials), .none):
108+
let api = WordPressComRestApi(
109+
oAuthToken: wpComCredentials.authToken,
110+
// TODO: there should be a way to read the user agent from the library configs
111+
userAgent: WKWebView.userAgent
112+
)
113+
let remote = AccountServiceRemoteREST(wordPressComRestApi: api)
114+
115+
remote.getAccountDetails(
116+
success: { [weak self] remoteUser in
117+
guard let remoteUser else {
118+
fatalError("Received no RemoteUser – Likely an Objective-C types byproduct.")
119+
}
120+
121+
self?.presentAlert(
122+
title: "🎉",
123+
message: "Welcome \(remoteUser.displayName ?? "'no display name'")",
124+
onDismiss: {}
125+
)
126+
},
127+
failure: { error in
128+
print(error!.localizedDescription)
129+
}
130+
)
131+
}
132+
}
133+
}

Demo/AuthenticatorDemo/ViewController+WordPressAuthenticator.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import WebKit
22
import WordPressAuthenticator
3+
import WordPressKit
34

45
extension ViewController {
56

6-
func initializeWordPressAuthenticator() {
7+
func initializeWordPressAuthenticator(withGoogleSDK: Bool) {
78
// In a proper app, we'd want to split this call to keep the code readable. Here, it's
89
// useful to keep it all in one block to show how insanely long it is.
910
WordPressAuthenticator.initialize(
@@ -28,7 +29,8 @@ extension ViewController {
2829
enableUnifiedCarousel: true,
2930
// Notice that this is required as well as `enableSignupWithGoogle` to show the
3031
// option to login with Google.
31-
enableSocialLogin: true
32+
enableSocialLogin: true,
33+
googleLoginWithoutSDK: withGoogleSDK == false
3234
),
3335
style: WordPressAuthenticatorStyle(
3436
// Primary (normal and highlight) is the color of buttons such as "Log in or signup
@@ -76,5 +78,27 @@ extension ViewController {
7678
navTitleTextColor: .white
7779
)
7880
)
81+
82+
WordPressAuthenticator.shared.delegate = self
83+
}
84+
85+
// Note that this method does not try to authenticate the user with the WordPress backend.
86+
// It only verifies that we can get a token from Google.
87+
func getAuthTokenFromGoogle() {
88+
Task { @MainActor in
89+
do {
90+
let token = try await self.googleAuthenticator.getOAuthToken()
91+
92+
presentAlert(
93+
title: "🎉",
94+
message: "Successfully authenticated with Google.\n\nEmail in received token: \(token.email)",
95+
onDismiss: {}
96+
)
97+
} catch let error as OAuthError {
98+
presentAlert(title: "", message: error.errorDescription, onDismiss: {})
99+
} catch {
100+
fatalError("Caught an error that was not of the expected `OAuthError` type: \(error)")
101+
}
102+
}
79103
}
80104
}

Demo/AuthenticatorDemo/ViewController.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AuthenticationServices
12
import UIKit
23
import WordPressAuthenticator
34

@@ -7,24 +8,43 @@ class ViewController: UIViewController {
78

89
/// Add `CellConfiguration` items to add new actionable rows to the table view in `ViewController`.
910
lazy var configuration: [CellConfiguration] = [
10-
CellConfiguration(text: "Show Login") { [weak self] in
11+
CellConfiguration(text: "Show Login - With Google SDK") { [weak self] in
1112
guard let self else { fatalError() }
13+
14+
self.initializeWordPressAuthenticator(withGoogleSDK: true)
15+
WordPressAuthenticator.showLoginFromPresenter(self, animated: true)
16+
},
17+
CellConfiguration(text: "Show Login - Without Google SDK") { [weak self] in
18+
guard let self else { fatalError() }
19+
20+
self.initializeWordPressAuthenticator(withGoogleSDK: false)
1221
WordPressAuthenticator.showLoginFromPresenter(self, animated: true)
22+
},
23+
CellConfiguration(text: "Get Google token only - Standalone, Wihout SDK") { [weak self] in
24+
guard let self else { fatalError() }
25+
26+
self.initializeWordPressAuthenticator(withGoogleSDK: false)
27+
self.getAuthTokenFromGoogle()
1328
}
1429
]
1530

1631
let tableView = UITableView(frame: .zero, style: .grouped)
1732
let reuseIdentifier = "cell"
1833

34+
lazy var googleAuthenticator = NewGoogleAuthenticator(
35+
clientId: GoogleClientId(string: APICredentials.googleLoginClientId)!,
36+
scheme: APICredentials.googleLoginSchemeId,
37+
audience: APICredentials.googleLoginServerClientId,
38+
viewController: self,
39+
urlSession: URLSession.shared
40+
)
41+
1942
override func viewDidLoad() {
2043
super.viewDidLoad()
2144

2245
title = "Authenticator Demo 🔐"
2346

2447
setUpTableView()
25-
26-
initializeWordPressAuthenticator()
27-
WordPressAuthenticator.shared.delegate = self
2848
}
2949

3050
func setUpTableView() {

Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def wordpress_authenticator_pods
2929
pod 'Gridicons', '~> 1.0-beta' # Don't change this until we hit 2.0 in Gridicons
3030
pod 'WordPressUI', '~> 1.7-beta' # Don't change this until we hit 2.0 in WordPressUI
3131
pod 'WordPressKit', '~> 6.0-beta' # Don't change this until we hit 5.0 in WPKit
32-
pod 'WordPressShared', '~> 2.0-beta' # Don't change this until we hit 2.0 in WPShared
32+
pod 'WordPressShared', '~> 2.1-beta'
3333

3434
third_party_pods
3535
end

Podfile.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ PODS:
2828
- "NSURL+IDN (= 0.4)"
2929
- SVProgressHUD (~> 2.2.5)
3030
- WordPressKit (~> 6.0-beta)
31-
- WordPressShared (~> 2.0-beta)
31+
- WordPressShared (~> 2.1-beta)
3232
- WordPressUI (~> 1.7-beta)
3333
- WordPressKit (6.0.0-beta.1):
3434
- Alamofire (~> 4.8.0)
3535
- NSObject-SafeExpectations (~> 0.0.4)
3636
- UIDeviceIdentifier (~> 2.0)
3737
- WordPressShared (~> 2.0-beta)
3838
- wpxmlrpc (~> 0.10)
39-
- WordPressShared (2.0.0-beta.2)
39+
- WordPressShared (2.1.0)
4040
- WordPressUI (1.7.0)
4141
- wpxmlrpc (0.10.0)
4242

@@ -51,7 +51,7 @@ DEPENDENCIES:
5151
- SwiftLint (~> 0.49)
5252
- WordPressAuthenticator (from `.`)
5353
- WordPressKit (~> 6.0-beta)
54-
- WordPressShared (~> 2.0-beta)
54+
- WordPressShared (~> 2.1-beta)
5555
- WordPressUI (~> 1.7-beta)
5656

5757
SPEC REPOS:
@@ -94,12 +94,12 @@ SPEC CHECKSUMS:
9494
SVProgressHUD: 1428aafac632c1f86f62aa4243ec12008d7a51d6
9595
SwiftLint: 32ee33ded0636d0905ef6911b2b67bbaeeedafa5
9696
UIDeviceIdentifier: f33af270ba9045ea18b31d9aab88e42a0082ea67
97-
WordPressAuthenticator: fb233ed409d2cf86edf01ce5616dd15d4e9c9ca4
97+
WordPressAuthenticator: 0620a9f3a367af505d9bc946975184a73906e2e5
9898
WordPressKit: 08da0bc981f6398ef7a32e523fd054de7d7c7069
99-
WordPressShared: 04403b43f821c4ed2b84a2112ef9f64f1e7cdceb
99+
WordPressShared: 0aa459e5257a77184db87805a998f447443c9706
100100
WordPressUI: 1cf47a3b78154faf69caa18569ee7ece1e510fa0
101101
wpxmlrpc: 68db063041e85d186db21f674adf08d9c70627fd
102102

103-
PODFILE CHECKSUM: f1d40bf767881fbe9b0328b875768ee4a60a113f
103+
PODFILE CHECKSUM: 8746ca27deaec1c319801f91d81dab582ee1c0f5
104104

105105
COCOAPODS: 1.11.3

WordPressAuthenticator.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ Pod::Spec.new do |s|
4242
# If you want to update which of these is used, specify it in the host app.
4343
s.dependency 'WordPressUI', '~> 1.7-beta'
4444
s.dependency 'WordPressKit', '~> 6.0-beta'
45-
s.dependency 'WordPressShared', '~> 2.0-beta'
45+
s.dependency 'WordPressShared', '~> 2.1-beta'
4646
end

0 commit comments

Comments
 (0)