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

Commit b4af009

Browse files
authored
Expose name property in IDToken (#761)
2 parents 8aba8c4 + 44724d4 commit b4af009

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ _None._
3838

3939
### New Features
4040

41-
_None._
41+
- Google's `IDToken` now exposes the user's full name via `name`. [#761]
4242

4343
### Bug Fixes
4444

Demo/AuthenticatorDemo/ViewController+WordPressAuthenticator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ extension ViewController {
9292

9393
presentAlert(
9494
title: "🎉",
95-
message: "Successfully authenticated with Google.\n\nEmail in received token: \(token.email)",
95+
message: "Successfully authenticated with Google.\n\nEmail in received token: \(token.email)\n\nName: \(token.name)",
9696
onDismiss: {}
9797
)
9898
} catch let error as OAuthError {

WordPressAuthenticator/GoogleSignIn/IDToken.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
public struct IDToken {
33

44
public let token: JSONWebToken
5+
public let name: String
56
public let email: String
67

78
// TODO: Validate token! – https://developers.google.com/identity/openid-connect/openid-connect#validatinganidtoken
89
init?(jwt: JSONWebToken) {
10+
// Name and email might not be part of the JWT Google sent us if the scope used for the
11+
// request didn't include them
912
guard let email = jwt.payload["email"] as? String else {
1013
return nil
1114
}
15+
guard let name = jwt.payload["name"] as? String else {
16+
return nil
17+
}
1218

1319
self.token = jwt
20+
self.name = name
1421
self.email = email
1522
}
1623
}

WordPressAuthenticatorTests/GoogleSignIn/IDTokenTests.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ import XCTest
33

44
class IDTokenTests: XCTestCase {
55

6-
func testInitWithJWTWithoutEmail() throws {
6+
func testInitWithJWTWithoutNameNorEmailFails() throws {
77
XCTAssertNil(IDToken(jwt: try XCTUnwrap(JSONWebToken(encodedString: JSONWebToken.validJWTString))))
88
}
99

10-
func testInitWithJWTWithEmail() throws {
11-
let jwt = try XCTUnwrap(JSONWebToken(encodedString: JSONWebToken.validJWTStringWithEmail))
10+
func testInitWithJWTWithoutEmailFails() throws {
11+
XCTAssertNil(IDToken(jwt: try XCTUnwrap(JSONWebToken(encodedString: JSONWebToken.validJWTStringWithNameOnly))))
12+
}
13+
14+
func testInitWithJWTWithoutNameFails() throws {
15+
XCTAssertNil(IDToken(jwt: try XCTUnwrap(JSONWebToken(encodedString: JSONWebToken.validJWTStringWithEmailOnly))))
16+
}
17+
18+
func testInitWithJWTWithNameAndEmailSucceeds() throws {
19+
let jwt = try XCTUnwrap(JSONWebToken(encodedString: JSONWebToken.validJWTStringWithNameAndEmail))
1220
let token = try XCTUnwrap(IDToken(jwt: jwt))
1321

22+
XCTAssertEqual(token.name, JSONWebToken.nameFromValidJWTStringWithEmail)
1423
XCTAssertEqual(token.email, JSONWebToken.emailFromValidJWTStringWithEmail)
1524
}
1625

WordPressAuthenticatorTests/GoogleSignIn/JSONWebToken+Fixtures.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,35 @@ extension JSONWebToken {
2424
// "key": "value",
2525
// "email": "[email protected]"
2626
// }
27-
private(set) static var validJWTStringWithEmail = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ2YWx1ZSIsImVtYWlsIjoidGVzdEBlbWFpbC5jb20ifQ.b-2oTvjpc_qHM5dU6akk_ESe3eWUZwL21pvTsCmW2gE"
27+
private(set) static var validJWTStringWithEmailOnly = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ2YWx1ZSIsImVtYWlsIjoidGVzdEBlbWFpbC5jb20ifQ.b-2oTvjpc_qHM5dU6akk_ESe3eWUZwL21pvTsCmW2gE"
2828

29-
// For convenience, this exposes the email value used in validJWTStringWithEmail.
29+
// Created with https://jwt.io/ with input:
30+
//
31+
// header: {
32+
// "alg": "HS256",
33+
// "typ": "JWT"
34+
// }
35+
// payload: {
36+
// "name": "John Doe",
37+
// "key": "value"
38+
// }
39+
private(set) static var validJWTStringWithNameOnly = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiBEb2UiLCJrZXkiOiJ2YWx1ZSJ9.P7Se5_EMlFBg5q8PV4C2IQ1YojTTSgitCBX7FgmXZzs"
40+
41+
// Created with https://jwt.io/ with input:
42+
//
43+
// header: {
44+
// "alg": "HS256",
45+
// "typ": "JWT"
46+
// }
47+
// payload: {
48+
// "name": "John Doe",
49+
// "key": "value",
50+
// "email": "[email protected]"
51+
// }
52+
private(set) static var validJWTStringWithNameAndEmail = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiBEb2UiLCJrZXkiOiJ2YWx1ZSIsImVtYWlsIjoidGVzdEBlbWFpbC5jb20ifQ.-xzg0r5mMnSZ8hE3hk7S93iCZHhOez1QFYdheSmDlx4"
53+
54+
// For convenience, this exposes the email and name value used in the fixtures.
3055
// This allows us to use raw strings in tests, rather than having to implement encoding the JWT from an arbitrary string.
3156
private(set) static var emailFromValidJWTStringWithEmail = "[email protected]"
57+
private(set) static var nameFromValidJWTStringWithEmail = "John Doe"
3258
}

WordPressAuthenticatorTests/GoogleSignIn/NewGoogleAuthenticatorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class NewGoogleAuthenticatorTests: XCTestCase {
8686
clientId: fakeClientId,
8787
scheme: "scheme",
8888
audience: "audience",
89-
oautTokenGetter: GoogleOAuthTokenGettingStub(response: .fixture(rawIDToken: JSONWebToken.validJWTStringWithEmail))
89+
oautTokenGetter: GoogleOAuthTokenGettingStub(response: .fixture(rawIDToken: JSONWebToken.validJWTStringWithNameAndEmail))
9090
)
9191
let url = URL(string: "https://test.com?code=a_code")!
9292

0 commit comments

Comments
 (0)