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

Commit 4c5ed14

Browse files
authored
Pass UIViewController for Google auth presentation at call site (#755)
2 parents b89ade5 + 3f43d56 commit 4c5ed14

File tree

5 files changed

+33
-48
lines changed

5 files changed

+33
-48
lines changed

Demo/AuthenticatorDemo/ViewController+WordPressAuthenticator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ extension ViewController {
8787
func getAuthTokenFromGoogle() {
8888
Task { @MainActor in
8989
do {
90-
let token = try await self.googleAuthenticator.getOAuthToken()
90+
let token = try await self.googleAuthenticator.getOAuthToken(from: self)
9191

9292
presentAlert(
9393
title: "🎉",

Demo/AuthenticatorDemo/ViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class ViewController: UIViewController {
3535
clientId: GoogleClientId(string: APICredentials.googleLoginClientId)!,
3636
scheme: APICredentials.googleLoginSchemeId,
3737
audience: APICredentials.googleLoginServerClientId,
38-
viewController: self,
3938
urlSession: URLSession.shared
4039
)
4140

WordPressAuthenticator/GoogleSignIn/NewGoogleAuthenticator.swift

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,18 @@ public class NewGoogleAuthenticator: NSObject {
55
let clientId: GoogleClientId
66
let scheme: String
77
let audience: String
8-
let contextProvider: ASWebAuthenticationPresentationContextProviding
9-
108
let oauthTokenGetter: GoogleOAuthTokenGetting
119

1210
public convenience init(
1311
clientId: GoogleClientId,
1412
scheme: String,
1513
audience: String,
16-
viewController: UIViewController,
1714
urlSession: URLSession
1815
) {
1916
self.init(
2017
clientId: clientId,
2118
scheme: scheme,
2219
audience: audience,
23-
contextProvider: WebAuthenticationPresentationContext(viewController: viewController),
24-
oautTokenGetter: GoogleOAuthTokenGetter(dataGetter: urlSession)
25-
)
26-
}
27-
28-
public convenience init(
29-
clientId: GoogleClientId,
30-
scheme: String,
31-
audience: String,
32-
contextProvider: ASWebAuthenticationPresentationContextProviding,
33-
urlSession: URLSession
34-
) {
35-
self.init(
36-
clientId: clientId,
37-
scheme: scheme,
38-
audience: audience,
39-
contextProvider: contextProvider,
4020
oautTokenGetter: GoogleOAuthTokenGetter(dataGetter: urlSession)
4121
)
4222
}
@@ -45,24 +25,46 @@ public class NewGoogleAuthenticator: NSObject {
4525
clientId: GoogleClientId,
4626
scheme: String,
4727
audience: String,
48-
contextProvider: ASWebAuthenticationPresentationContextProviding,
4928
oautTokenGetter: GoogleOAuthTokenGetting
5029
) {
5130
self.clientId = clientId
5231
self.scheme = scheme
5332
self.audience = audience
5433
self.oauthTokenGetter = oautTokenGetter
55-
self.contextProvider = contextProvider
5634
}
5735

5836
/// Get the user's OAuth token from their Google account. This token can be used to authenticate with the WordPress backend.
59-
public func getOAuthToken() async throws -> IDToken {
37+
///
38+
/// The app will present the browser to hand over authentication to Google from the given `UIViewController`.
39+
public func getOAuthToken(from viewController: UIViewController) async throws -> IDToken {
40+
return try await getOAuthToken(
41+
from: WebAuthenticationPresentationContext(viewController: viewController)
42+
)
43+
}
44+
45+
/// Get the user's OAuth token from their Google account. This token can be used to authenticate with the WordPress backend.
46+
///
47+
/// The app will present the browser to hand over authentication to Google using the given
48+
/// `ASWebAuthenticationPresentationContextProviding`.
49+
public func getOAuthToken(
50+
from contextProvider: ASWebAuthenticationPresentationContextProviding
51+
) async throws -> IDToken {
6052
let pkce = try ProofKeyForCodeExchange()
61-
let url = try await getURL(clientId: clientId, scheme: scheme, pkce: pkce)
53+
let url = try await getURL(
54+
clientId: clientId,
55+
scheme: scheme,
56+
pkce: pkce,
57+
contextProvider: contextProvider
58+
)
6259
return try await requestOAuthToken(url: url, clientId: clientId, audience: audience, pkce: pkce)
6360
}
6461

65-
func getURL(clientId: GoogleClientId, scheme: String, pkce: ProofKeyForCodeExchange) async throws -> URL {
62+
func getURL(
63+
clientId: GoogleClientId,
64+
scheme: String,
65+
pkce: ProofKeyForCodeExchange,
66+
contextProvider: ASWebAuthenticationPresentationContextProviding
67+
) async throws -> URL {
6668
let url = try URL.googleSignInAuthURL(clientId: clientId, pkce: pkce)
6769
return try await withCheckedThrowingContinuation { continuation in
6870
let session = ASWebAuthenticationSession(

WordPressAuthenticator/Unified Auth/GoogleAuthenticator.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,15 @@ extension GoogleAuthenticator {
307307

308308
trackRequestAuthorizitation(type: authType)
309309

310-
// We might want to change this in subsequent iterations, perhaps by moving the
311-
// `contextProvider` to the `getOAuthToken()` method so to allow it to be stored
312-
// as a `lazy` property?
313310
let sdkLessGoogleAuthenticator = NewGoogleAuthenticator(
314311
clientId: authConfig.googleClientId,
315312
scheme: authConfig.googleLoginScheme,
316313
audience: authConfig.googleLoginServerClientId,
317-
contextProvider: WebAuthenticationPresentationContext(viewController: viewController),
318314
urlSession: .shared
319315
)
320316

321317
await SVProgressHUD.show()
322-
return try await sdkLessGoogleAuthenticator.getOAuthToken()
318+
return try await sdkLessGoogleAuthenticator.getOAuthToken(from: viewController)
323319
}
324320
}
325321

WordPressAuthenticatorTests/GoogleSignIn/NewGoogleAuthenticatorTests.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ class NewGoogleAuthenticatorTests: XCTestCase {
88
func testRequestingOAuthTokenThrowsIfCodeCannotBeExtractedFromURL() async throws {
99
// Notice the use of a stub that returns a successful value.
1010
// This way, if we get an error, we can be more confident it's legit.
11-
let authenticator = await NewGoogleAuthenticator(
11+
let authenticator = NewGoogleAuthenticator(
1212
clientId: fakeClientId,
1313
scheme: "scheme",
1414
audience: "audience",
15-
contextProvider: FakeContextProvider(),
1615
oautTokenGetter: GoogleOAuthTokenGettingStub(response: .fixture())
1716
)
1817
let url = URL(string: "https://test.com?without=code")!
@@ -35,11 +34,10 @@ class NewGoogleAuthenticatorTests: XCTestCase {
3534
}
3635

3736
func testRequestingOAuthTokenRethrowsTheErrorItRecives() async throws {
38-
let authenticator = await NewGoogleAuthenticator(
37+
let authenticator = NewGoogleAuthenticator(
3938
clientId: fakeClientId,
4039
scheme: "scheme",
4140
audience: "audience",
42-
contextProvider: FakeContextProvider(),
4341
oautTokenGetter: GoogleOAuthTokenGettingStub(error: TestError(id: 1))
4442
)
4543
let url = URL(string: "https://test.com?code=a_code")!
@@ -59,11 +57,10 @@ class NewGoogleAuthenticatorTests: XCTestCase {
5957
}
6058

6159
func testRequestingOAuthTokenThrowsIfIdTokenMissingFromResponse() async throws {
62-
let authenticator = await NewGoogleAuthenticator(
60+
let authenticator = NewGoogleAuthenticator(
6361
clientId: fakeClientId,
6462
scheme: "scheme",
6563
audience: "audience",
66-
contextProvider: FakeContextProvider(),
6764
oautTokenGetter: GoogleOAuthTokenGettingStub(response: .fixture(rawIDToken: .none))
6865
)
6966
let url = URL(string: "https://test.com?code=a_code")!
@@ -85,11 +82,10 @@ class NewGoogleAuthenticatorTests: XCTestCase {
8582
}
8683

8784
func testRequestingOAuthTokenReturnsTokenIfSuccessful() async throws {
88-
let authenticator = await NewGoogleAuthenticator(
85+
let authenticator = NewGoogleAuthenticator(
8986
clientId: fakeClientId,
9087
scheme: "scheme",
9188
audience: "audience",
92-
contextProvider: FakeContextProvider(),
9389
oautTokenGetter: GoogleOAuthTokenGettingStub(response: .fixture(rawIDToken: JSONWebToken.validJWTStringWithEmail))
9490
)
9591
let url = URL(string: "https://test.com?code=a_code")!
@@ -107,11 +103,3 @@ class NewGoogleAuthenticatorTests: XCTestCase {
107103
}
108104
}
109105
}
110-
111-
import AuthenticationServices
112-
113-
class FakeContextProvider: UIViewController, ASWebAuthenticationPresentationContextProviding {
114-
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
115-
return view.window!
116-
}
117-
}

0 commit comments

Comments
 (0)