-
Notifications
You must be signed in to change notification settings - Fork 1.7k
implement start passkey enrollment- updated #15162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,106
−1
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
714df23
implement start passkey enrollment
srushtisv e63a770
adding unit tests
srushtisv 9d87eb9
adding unit tests
srushtisv bf88537
lint fixes
srushtisv b6f1fe8
resolving comments in user.swift
srushtisv cba28d9
resolving comments in tests
srushtisv 1a93242
fixes
srushtisv 8e10c1a
updating unit tests
srushtisv bb80eb6
implementing passkey finalize enrollment (#15163)
srushtisv f583843
Merge branch 'passkey-new' into passkey-new-start-enrollment
srushtisv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
FirebaseAuth/Sources/Swift/Backend/RPC/StartPasskeyEnrollmentRequest.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
/// The GCIP endpoint for startPasskeyEnrollment rpc | ||
private let startPasskeyEnrollmentEndPoint = "accounts/passkeyEnrollment:start" | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
class StartPasskeyEnrollmentRequest: IdentityToolkitRequest, AuthRPCRequest { | ||
typealias Response = StartPasskeyEnrollmentResponse | ||
|
||
/// The raw user access token | ||
let idToken: String | ||
|
||
init(idToken: String, | ||
requestConfiguration: AuthRequestConfiguration) { | ||
self.idToken = idToken | ||
super.init( | ||
endpoint: startPasskeyEnrollmentEndPoint, | ||
requestConfiguration: requestConfiguration, | ||
useIdentityPlatform: true | ||
) | ||
} | ||
|
||
var unencodedHTTPRequestBody: [String: AnyHashable]? { | ||
var body: [String: AnyHashable] = [ | ||
"idToken": idToken, | ||
] | ||
if let tenantID = tenantID { | ||
body["tenantId"] = tenantID | ||
} | ||
return body | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
FirebaseAuth/Sources/Swift/Backend/RPC/StartPasskeyEnrollmentResponse.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
struct StartPasskeyEnrollmentResponse: AuthRPCResponse { | ||
/// The RP ID of the FIDO Relying Party. | ||
let rpID: String | ||
/// The user id | ||
let userID: String | ||
/// The FIDO challenge. | ||
let challenge: String | ||
|
||
init(dictionary: [String: AnyHashable]) throws { | ||
guard let options = dictionary["credentialCreationOptions"] as? [String: Any] else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
guard let rp = options["rp"] as? [String: Any], | ||
let rpID = rp["id"] as? String else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
guard let user = options["user"] as? [String: Any], | ||
let userID = user["id"] as? String else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
guard let challenge = options["challenge"] as? String else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
self.rpID = rpID | ||
self.userID = userID | ||
self.challenge = challenge | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
FirebaseAuth/Tests/Unit/StartPasskeyEnrollmentRequestTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if os(iOS) || os(tvOS) || os(macOS) | ||
|
||
@testable import FirebaseAuth | ||
import FirebaseCore | ||
import Foundation | ||
import XCTest | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
class StartPasskeyEnrollmentRequestTests: XCTestCase { | ||
private var request: StartPasskeyEnrollmentRequest! | ||
private var fakeConfig: AuthRequestConfiguration! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
fakeConfig = AuthRequestConfiguration( | ||
apiKey: "FAKE_API_KEY", | ||
appID: "FAKE_APP_ID" | ||
) | ||
} | ||
|
||
override func tearDown() { | ||
request = nil | ||
fakeConfig = nil | ||
super.tearDown() | ||
} | ||
|
||
func testInitWithValidIdTokenAndConfiguration() { | ||
request = StartPasskeyEnrollmentRequest( | ||
idToken: "FAKE_ID_TOKEN", | ||
requestConfiguration: fakeConfig | ||
) | ||
XCTAssertEqual(request.idToken, "FAKE_ID_TOKEN") | ||
XCTAssertEqual(request.endpoint, "accounts/passkeyEnrollment:start") | ||
XCTAssertTrue(request.useIdentityPlatform) | ||
} | ||
|
||
func testUnencodedHTTPRequestBodyWithoutTenantId() { | ||
request = StartPasskeyEnrollmentRequest( | ||
idToken: "FAKE_ID_TOKEN", | ||
requestConfiguration: fakeConfig | ||
) | ||
let body = request.unencodedHTTPRequestBody | ||
XCTAssertNotNil(body) | ||
XCTAssertEqual(body?["idToken"] as? String, "FAKE_ID_TOKEN") | ||
XCTAssertNil(body?["tenantId"]) | ||
} | ||
|
||
func testUnencodedHTTPRequestBodyWithTenantId() { | ||
// setting up fake auth to set tenantId | ||
let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000", | ||
gcmSenderID: "00000000000000000-00000000000-000000000") | ||
options.apiKey = AuthTests.kFakeAPIKey | ||
options.projectID = "myProjectID" | ||
let name = "test-AuthTests\(AuthTests.testNum)" | ||
AuthTests.testNum = AuthTests.testNum + 1 | ||
let fakeAuth = Auth(app: FirebaseApp(instanceWithName: name, options: options)) | ||
fakeAuth.tenantID = "TEST_TENANT" | ||
let configWithTenant = AuthRequestConfiguration( | ||
apiKey: "FAKE_API_KEY", | ||
appID: "FAKE_APP_ID", | ||
auth: fakeAuth | ||
) | ||
request = StartPasskeyEnrollmentRequest( | ||
idToken: "FAKE_ID_TOKEN", | ||
requestConfiguration: configWithTenant | ||
) | ||
let body = request.unencodedHTTPRequestBody | ||
XCTAssertNotNil(body) | ||
XCTAssertEqual(body?["idToken"] as? String, "FAKE_ID_TOKEN") | ||
XCTAssertEqual(body?["tenantId"] as? String, "TEST_TENANT") | ||
} | ||
} | ||
|
||
#endif |
99 changes: 99 additions & 0 deletions
99
FirebaseAuth/Tests/Unit/StartPasskeyEnrollmentResponseTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if os(iOS) || os(tvOS) || os(macOS) | ||
|
||
@testable import FirebaseAuth | ||
import XCTest | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
class StartPasskeyEnrollmentResponseTests: RPCBaseTests { | ||
private func makeValidDictionary() -> [String: AnyHashable] { | ||
return [ | ||
"credentialCreationOptions": [ | ||
"rp": ["id": "FAKE_RP_ID"] as [String: AnyHashable], | ||
"user": ["id": "FAKE_USER_ID"] as [String: AnyHashable], | ||
"challenge": "FAKE_CHALLENGE" as String, | ||
] as [String: AnyHashable], | ||
] | ||
} | ||
|
||
/// Helper function to remove a nested key from a dictionary | ||
private func removeField(_ dict: inout [String: AnyHashable], keyPath: [String]) { | ||
guard let first = keyPath.first else { return } | ||
if keyPath.count == 1 { | ||
dict.removeValue(forKey: first) | ||
} else if var inDict = dict[first] as? [String: AnyHashable] { | ||
removeField(&inDict, keyPath: Array(keyPath.dropFirst())) | ||
dict[first] = inDict | ||
} | ||
} | ||
|
||
func testInitWithValidDictionary() throws { | ||
let response = try StartPasskeyEnrollmentResponse(dictionary: makeValidDictionary()) | ||
XCTAssertEqual(response.rpID, "FAKE_RP_ID") | ||
XCTAssertEqual(response.userID, "FAKE_USER_ID") | ||
XCTAssertEqual(response.challenge, "FAKE_CHALLENGE") | ||
} | ||
|
||
func testInitWithMissingFields() throws { | ||
struct TestCase { | ||
let name: String | ||
let removeFieldPath: [String] | ||
} | ||
let cases: [TestCase] = [ | ||
.init(name: "Missing rpId", removeFieldPath: ["credentialCreationOptions", "rp", "id"]), | ||
.init(name: "Missing userId", removeFieldPath: ["credentialCreationOptions", "user", "id"]), | ||
.init( | ||
name: "Missing Challenge", | ||
removeFieldPath: ["credentialCreationOptions", "challenge"] | ||
), | ||
] | ||
for testCase in cases { | ||
var dict = makeValidDictionary() | ||
removeField(&dict, keyPath: testCase.removeFieldPath) | ||
XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict), | ||
testCase.name) { error in | ||
let nsError = error as NSError | ||
XCTAssertEqual(nsError.domain, AuthErrorDomain) | ||
XCTAssertEqual(nsError.code, AuthErrorCode.internalError.rawValue) | ||
} | ||
} | ||
} | ||
|
||
func testSuccessfulStartPasskeyEnrollmentResponse() async throws { | ||
let expectedRpID = "FAKE_RP_ID" | ||
let expectedUserID = "FAKE_USER_ID" | ||
let expectedChallenge = "FAKE_CHALLENGE" | ||
rpcIssuer.respondBlock = { | ||
try self.rpcIssuer.respond(withJSON: [ | ||
"credentialCreationOptions": [ | ||
"rp": ["id": expectedRpID], | ||
"user": ["id": expectedUserID], | ||
"challenge": expectedChallenge, | ||
], | ||
]) | ||
} | ||
let request = StartPasskeyEnrollmentRequest( | ||
idToken: "FAKE_ID_TOKEN", | ||
requestConfiguration: AuthRequestConfiguration(apiKey: "API_KEY", appID: "APP_ID") | ||
) | ||
let response = try await authBackend.call(with: request) | ||
XCTAssertEqual(response.rpID, expectedRpID) | ||
XCTAssertEqual(response.userID, expectedUserID) | ||
XCTAssertEqual(response.challenge, expectedChallenge) | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks for refactoring.