Skip to content

Commit 4870aad

Browse files
committed
[auth-swift] Finish coding coverage and fixes (#11445)
1 parent 0cdea68 commit 4870aad

File tree

10 files changed

+229
-48
lines changed

10 files changed

+229
-48
lines changed

FirebaseAuth/Sources/Swift/AuthProvider/FacebookAuthProvider.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ import Foundation
3838
}
3939

4040
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
41-
@objc(FIRFacebookAuthCredential) private class FacebookAuthCredential: AuthCredential,
42-
NSSecureCoding {
43-
private let accessToken: String
41+
@objc class FacebookAuthCredential: AuthCredential, NSSecureCoding {
42+
let accessToken: String
4443

4544
init(withAccessToken accessToken: String) {
4645
self.accessToken = accessToken
@@ -54,7 +53,7 @@ import Foundation
5453
static var supportsSecureCoding = true
5554

5655
func encode(with coder: NSCoder) {
57-
coder.encode(accessToken)
56+
coder.encode(accessToken, forKey: "accessToken")
5857
}
5958

6059
required init?(coder: NSCoder) {

FirebaseAuth/Sources/Swift/AuthProvider/GameCenterAuthProvider.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@
149149
public static var supportsSecureCoding = true
150150

151151
public func encode(with coder: NSCoder) {
152-
coder.encode("playerID")
153-
coder.encode("teamPlayerID")
154-
coder.encode("gamePlayerID")
155-
coder.encode("publicKeyURL")
156-
coder.encode("signature")
157-
coder.encode("salt")
158-
coder.encode("timestamp")
159-
coder.encode("displayName")
152+
coder.encode(playerID, forKey: "playerID")
153+
coder.encode(teamPlayerID, forKey: "teamPlayerID")
154+
coder.encode(gamePlayerID, forKey: "gamePlayerID")
155+
coder.encode(publicKeyURL, forKey: "publicKeyURL")
156+
coder.encode(signature, forKey: "signature")
157+
coder.encode(salt, forKey: "salt")
158+
coder.encode(timestamp, forKey: "timestamp")
159+
coder.encode(displayName, forKey: "displayName")
160160
}
161161

162162
public required init?(coder: NSCoder) {

FirebaseAuth/Sources/Swift/AuthProvider/GoogleAuthProvider.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ import Foundation
4040
}
4141

4242
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
43-
@objc(FIRGoogleAuthCredential) private class GoogleAuthCredential: AuthCredential, NSSecureCoding {
44-
private let idToken: String
45-
private let accessToken: String
43+
@objc(FIRGoogleAuthCredential) class GoogleAuthCredential: AuthCredential, NSSecureCoding {
44+
let idToken: String
45+
let accessToken: String
4646

4747
init(withIDToken idToken: String, accessToken: String) {
4848
self.idToken = idToken
@@ -58,8 +58,8 @@ import Foundation
5858
static var supportsSecureCoding = true
5959

6060
func encode(with coder: NSCoder) {
61-
coder.encode(idToken)
62-
coder.encode(accessToken)
61+
coder.encode(idToken, forKey: "idToken")
62+
coder.encode(accessToken, forKey: "accessToken")
6363
}
6464

6565
required init?(coder: NSCoder) {

FirebaseAuth/Sources/Swift/AuthProvider/OAuthCredential.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ import Foundation
3333
*/
3434
@objc public let secret: String?
3535

36-
// TODO: delete objc's and public's below
3736
// internal
38-
@objc public let OAuthResponseURLString: String?
39-
@objc public let sessionID: String?
40-
@objc public let pendingToken: String?
37+
let OAuthResponseURLString: String?
38+
let sessionID: String?
39+
let pendingToken: String?
4140
let fullName: PersonNameComponents?
4241
// private
43-
@objc public let rawNonce: String?
42+
let rawNonce: String?
4443

4544
// TODO: Remove public objc
4645
@objc public init(withProviderID providerID: String,

FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthCredential.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import Foundation
2121
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
2222
@objc(FIRPhoneAuthCredential) public class PhoneAuthCredential: AuthCredential, NSSecureCoding {
2323
enum CredentialKind {
24-
case phoneNumber(String, String) // phoneNumber, temporaryProof
25-
case verification(String, String) // id, code
24+
case phoneNumber(_ phoneNumber: String, _ temporaryProof: String)
25+
case verification(_ id: String, _ code: String)
2626
}
2727

2828
let credentialKind: CredentialKind

FirebaseAuth/Sources/Swift/User/User.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,11 +1653,6 @@ extension User: NSSecureCoding {}
16531653
switch credential.credentialKind {
16541654
case .phoneNumber: fatalError("Internal Error: Missing verificationCode")
16551655
case let .verification(verificationID, code):
1656-
let finalizeMFAPhoneRequestInfo =
1657-
AuthProtoFinalizeMFAPhoneRequestInfo(
1658-
sessionInfo: verificationID,
1659-
verificationCode: code
1660-
)
16611656
let operation = isLinkOperation ? AuthOperationType.link : AuthOperationType.update
16621657
let request = VerifyPhoneNumberRequest(verificationID: verificationID,
16631658
verificationCode: code,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
import XCTest
17+
18+
@testable import FirebaseAuth
19+
20+
/** @class FIRFacebookAuthProviderTests
21+
@brief Tests for @c FIRFacebookAuthProvider
22+
*/
23+
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
24+
class FacebookAuthProviderTests: XCTestCase {
25+
/** @fn testCredentialWithToken
26+
@brief Tests the @c credentialWithToken method to make sure the credential it produces populates
27+
the appropriate fields in a verify assertion request.
28+
*/
29+
func testCredentialWithToken() {
30+
let kFacebookToken = "Token"
31+
let requestConfiguration = AuthRequestConfiguration(apiKey: "APIKey", appID: "appID")
32+
let credential = FacebookAuthProvider.credential(withAccessToken: kFacebookToken)
33+
let request = VerifyAssertionRequest(providerID: FacebookAuthProvider.id,
34+
requestConfiguration: requestConfiguration)
35+
credential.prepare(request)
36+
XCTAssertEqual(kFacebookToken, request.providerAccessToken)
37+
}
38+
39+
/** @fn testFacebookAuthCredentialCoding
40+
@brief Tests successful archiving and unarchiving of @c FacebookAuthCredential.
41+
*/
42+
func testFacebookAuthCredentialCoding() throws {
43+
let kFacebookToken = "Token"
44+
let credential = FacebookAuthProvider.credential(withAccessToken: kFacebookToken)
45+
XCTAssertTrue(FacebookAuthCredential.supportsSecureCoding)
46+
let data = try NSKeyedArchiver.archivedData(
47+
withRootObject: credential,
48+
requiringSecureCoding: true
49+
)
50+
let unarchivedCredential = try XCTUnwrap(NSKeyedUnarchiver.unarchivedObject(
51+
ofClass: FacebookAuthCredential.self, from: data
52+
))
53+
XCTAssertEqual(unarchivedCredential.accessToken, kFacebookToken)
54+
}
55+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
import XCTest
17+
18+
@testable import FirebaseAuth
19+
20+
/** @class FIRGoogleAuthProviderTests
21+
@brief Tests for @c FIRGoogleAuthProvider
22+
*/
23+
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
24+
class GoogleAuthProviderTests: XCTestCase {
25+
let kAccessToken = "Token"
26+
let kIDToken = "idToken"
27+
28+
/** @fn testCredentialWithToken
29+
@brief Tests the @c credentialWithToken method to make sure the credential it produces populates
30+
the appropriate fields in a verify assertion request.
31+
*/
32+
func testCredentialWithToken() {
33+
let requestConfiguration = AuthRequestConfiguration(apiKey: "APIKey", appID: "appID")
34+
let credential = GoogleAuthProvider.credential(withIDToken: kIDToken, accessToken: kAccessToken)
35+
let request = VerifyAssertionRequest(providerID: GoogleAuthProvider.id,
36+
requestConfiguration: requestConfiguration)
37+
credential.prepare(request)
38+
XCTAssertEqual(kAccessToken, request.providerAccessToken)
39+
}
40+
41+
/** @fn testGoogleAuthCredentialCoding
42+
@brief Tests successful archiving and unarchiving of @c GoogleAuthCredential.
43+
*/
44+
func testGoogleAuthCredentialCoding() throws {
45+
let credential = GoogleAuthProvider.credential(withIDToken: kIDToken, accessToken: kAccessToken)
46+
XCTAssertTrue(GoogleAuthCredential.supportsSecureCoding)
47+
let data = try NSKeyedArchiver.archivedData(
48+
withRootObject: credential,
49+
requiringSecureCoding: true
50+
)
51+
let unarchivedCredential = try XCTUnwrap(NSKeyedUnarchiver.unarchivedObject(
52+
ofClass: GoogleAuthCredential.self, from: data
53+
))
54+
XCTAssertEqual(unarchivedCredential.accessToken, kAccessToken)
55+
XCTAssertEqual(unarchivedCredential.idToken, kIDToken)
56+
}
57+
}

FirebaseAuth/Tests/Unit/OAuthProviderTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,42 @@ import FirebaseCore
227227
OAuthProviderTests.testAppCheck = false
228228
}
229229

230+
/** @fn testOAuthCredentialCoding
231+
@brief Tests successful archiving and unarchiving of @c GoogleAuthCredential.
232+
*/
233+
func testOAuthCredentialCoding() throws {
234+
let kAccessToken = "accessToken"
235+
let kIDToken = "idToken"
236+
let kRawNonce = "nonce"
237+
let kSecret = "sEcret"
238+
let kFullName = PersonNameComponents()
239+
let kPendingToken = "pendingToken"
240+
241+
let credential = OAuthCredential(withProviderID: "dummyProvider",
242+
idToken: kIDToken,
243+
rawNonce: kRawNonce,
244+
accessToken: kAccessToken,
245+
secret: kSecret,
246+
fullName: kFullName,
247+
pendingToken: kPendingToken)
248+
249+
XCTAssertTrue(OAuthCredential.supportsSecureCoding)
250+
let data = try NSKeyedArchiver.archivedData(
251+
withRootObject: credential,
252+
requiringSecureCoding: true
253+
)
254+
let unarchivedCredential = try XCTUnwrap(NSKeyedUnarchiver.unarchivedObject(
255+
ofClasses: [OAuthCredential.self, NSPersonNameComponents.self], from: data
256+
) as? OAuthCredential)
257+
XCTAssertEqual(unarchivedCredential.idToken, kIDToken)
258+
XCTAssertEqual(unarchivedCredential.rawNonce, kRawNonce)
259+
XCTAssertEqual(unarchivedCredential.accessToken, kAccessToken)
260+
XCTAssertEqual(unarchivedCredential.secret, kSecret)
261+
XCTAssertEqual(unarchivedCredential.fullName, kFullName)
262+
XCTAssertEqual(unarchivedCredential.pendingToken, kPendingToken)
263+
XCTAssertEqual(unarchivedCredential.provider, OAuthProvider.id)
264+
}
265+
230266
private func initApp(_ functionName: String, useAppID: Bool = false, omitClientID: Bool = false,
231267
scheme: String = OAuthProviderTests.kFakeReverseClientID) {
232268
let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",

FirebaseAuth/Tests/Unit/SignInWithGameCenterTests.swift

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,33 @@ class SignInWithGameCenterTests: RPCBaseTests {
2828
private let kReturnSecureTokenKey = "returnSecureToken"
2929
private let kExpectedAPIURL =
3030
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/signInWithGameCenter?key=APIKey"
31+
let kIDToken = "IDTOKEN"
32+
let kPlayerIDKey = "playerId"
33+
let kPlayerID = "PLAYERID"
34+
let kTeamPlayerIDKey = "teamPlayerId"
35+
let kTeamPlayerID = "TEAMPLAYERID"
36+
let kGamePlayerIDKey = "gamePlayerId"
37+
let kGamePlayerID = "GAMEPLAYERID"
38+
let kApproximateExpirationDate = "3600"
39+
let kPublicKeyURLKey = "publicKeyUrl"
40+
let kPublicKeyURL = "PUBLICKEYURL"
41+
let kSignatureKey = "signature"
42+
let kSignature = "AAAABBBBCCCC"
43+
let kSaltKey = "salt"
44+
let kSalt = "AAAA"
45+
let kTimestampKey = "timestamp"
46+
let kTimestamp = UInt64(12_345_678)
47+
let kAccessTokenKey = "idToken"
48+
let kAccessToken = "ACCESSTOKEN"
3149

3250
/** @fn testSignInWithGameCenterRequestAnonymous
3351
@brief Tests the encoding of a sign up new user request when user is signed in anonymously.
3452
*/
3553
func testRequestResponseEncoding() throws {
36-
let kIDToken = "IDTOKEN"
3754
let kRefreshToken = "PUBLICKEYURL"
3855
let kLocalID = "LOCALID"
39-
let kPlayerIDKey = "playerId"
40-
let kPlayerID = "PLAYERID"
41-
let kTeamPlayerIDKey = "teamPlayerId"
42-
let kTeamPlayerID = "TEAMPLAYERID"
43-
let kGamePlayerIDKey = "gamePlayerId"
44-
let kGamePlayerID = "GAMEPLAYERID"
45-
let kApproximateExpirationDate = "3600"
4656
let kDisplayNameKey = "displayName"
4757
let kDisplayName = "DISPLAYNAME"
48-
let kPublicKeyURLKey = "publicKeyUrl"
49-
let kPublicKeyURL = "PUBLICKEYURL"
50-
let kSignatureKey = "signature"
51-
let kSignature = "AAAABBBBCCCC"
52-
let kSaltKey = "salt"
53-
let kSalt = "AAAA"
54-
let kTimestampKey = "timestamp"
55-
let kTimestamp = UInt64(12_345_678)
56-
let kAccessTokenKey = "idToken"
57-
let kAccessToken = "ACCESSTOKEN"
58-
5958
var callbackInvoked = false
6059
var rpcResponse: SignInWithGameCenterResponse?
6160
var rpcError: NSError?
@@ -119,4 +118,45 @@ class SignInWithGameCenterTests: RPCBaseTests {
119118
XCTAssertEqual(rpcResponse?.displayName, kDisplayName)
120119
XCTAssertTrue(try XCTUnwrap(rpcResponse?.isNewUser))
121120
}
121+
122+
#if !os(watchOS)
123+
/** @fn testGameCenterAuthCredentialCoding
124+
@brief Tests successful archiving and unarchiving of @c GameCenterAuthCredential.
125+
*/
126+
func testGameCenterAuthCredentialCoding() throws {
127+
let credential = try makeGameCenterCredential()
128+
XCTAssertTrue(GameCenterAuthCredential.supportsSecureCoding)
129+
let data = try NSKeyedArchiver.archivedData(
130+
withRootObject: credential,
131+
requiringSecureCoding: true
132+
)
133+
let unarchivedCredential = try XCTUnwrap(NSKeyedUnarchiver.unarchivedObject(
134+
ofClasses: [NSURL.self, GameCenterAuthCredential.self], from: data
135+
) as? GameCenterAuthCredential)
136+
XCTAssertEqual(unarchivedCredential.playerID, kPlayerID)
137+
XCTAssertEqual(unarchivedCredential.teamPlayerID, kTeamPlayerID)
138+
XCTAssertEqual(unarchivedCredential.gamePlayerID, kGamePlayerID)
139+
XCTAssertEqual(unarchivedCredential.publicKeyURL, URL(string: kPublicKeyURL))
140+
XCTAssertEqual(String(data: try XCTUnwrap(unarchivedCredential.signature),
141+
encoding: .utf8), kSignature)
142+
XCTAssertEqual(String(data: try XCTUnwrap(unarchivedCredential.salt), encoding: .utf8), kSalt)
143+
XCTAssertEqual(unarchivedCredential.timestamp, kTimestamp)
144+
XCTAssertEqual(unarchivedCredential.displayName, kDisplayName)
145+
}
146+
147+
private func makeGameCenterCredential() throws -> GameCenterAuthCredential {
148+
let signature = try XCTUnwrap(kSignature.data(using: .utf8))
149+
let salt = try XCTUnwrap(kSalt.data(using: .utf8))
150+
return GameCenterAuthCredential(withPlayerID: kPlayerID,
151+
teamPlayerID: kTeamPlayerID,
152+
gamePlayerID: kGamePlayerID,
153+
publicKeyURL: try XCTUnwrap(
154+
URL(string: kPublicKeyURL)
155+
),
156+
signature: signature,
157+
salt: salt,
158+
timestamp: kTimestamp,
159+
displayName: kDisplayName)
160+
}
161+
#endif
122162
}

0 commit comments

Comments
 (0)