-
Notifications
You must be signed in to change notification settings - Fork 1.7k
implementing start passkey sign-in #15168
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
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5428146
implementing start passkey sign-in
srushtisv 59d804c
adding unit tests
srushtisv 2caf173
fix
srushtisv 527fd31
lint fixes
srushtisv e1cd312
updating unit tests
srushtisv 0f96c65
implementing finalize signin for passkey (#15173)
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
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
36 changes: 36 additions & 0 deletions
36
FirebaseAuth/Sources/Swift/Backend/RPC/StartPasskeySignInRequest.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,36 @@ | ||
// 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. | ||
|
||
/// The GCIP endpoint for startPasskeySignIn rpc | ||
private let startPasskeySignInEndpoint = "accounts/passkeySignIn:start" | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
class StartPasskeySignInRequest: IdentityToolkitRequest, AuthRPCRequest { | ||
typealias Response = StartPasskeySignInResponse | ||
|
||
init(requestConfiguration: AuthRequestConfiguration) { | ||
super.init( | ||
endpoint: startPasskeySignInEndpoint, | ||
requestConfiguration: requestConfiguration, | ||
useIdentityPlatform: true | ||
) | ||
} | ||
|
||
var unencodedHTTPRequestBody: [String: AnyHashable]? { | ||
guard let tenantID = tenantID else { | ||
return nil | ||
} | ||
return ["tenantId": tenantID] | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
FirebaseAuth/Sources/Swift/Backend/RPC/StartPasskeySignInResponse.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,33 @@ | ||
// 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. | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
struct StartPasskeySignInResponse: AuthRPCResponse { | ||
/// The RP ID of the FIDO Relying Party | ||
let rpID: String | ||
/// The FIDO challenge | ||
let challenge: String | ||
|
||
init(dictionary: [String: AnyHashable]) throws { | ||
guard let options = dictionary["credentialRequestOptions"] as? [String: Any] else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
guard let rpID = options["rpId"] as? String, | ||
let challenge = options["challenge"] as? String else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
self.rpID = rpID | ||
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
75 changes: 75 additions & 0 deletions
75
FirebaseAuth/Tests/Unit/StartPasskeySignInRequestTests.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,75 @@ | ||
// 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, *) | ||
final class StartPasskeySignInRequestTests: XCTestCase { | ||
private var config: AuthRequestConfiguration! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
config = AuthRequestConfiguration( | ||
apiKey: "FAKE_API_KEY", | ||
appID: "FAKE_APP_ID" | ||
) | ||
} | ||
|
||
override func tearDown() { | ||
config = nil | ||
super.tearDown() | ||
} | ||
|
||
func testInit_SetsEndpointAndConfig() { | ||
let request = StartPasskeySignInRequest(requestConfiguration: config) | ||
XCTAssertEqual(request.endpoint, "accounts/passkeySignIn:start") | ||
XCTAssertTrue(request.useIdentityPlatform) | ||
XCTAssertEqual(request.requestConfiguration().apiKey, "FAKE_API_KEY") | ||
XCTAssertEqual(request.requestConfiguration().appID, "FAKE_APP_ID") | ||
} | ||
|
||
func testUnencodedHTTPRequestBody_WithTenantId() { | ||
// 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 | ||
) | ||
_ = AuthRequestConfiguration(apiKey: "apiKey", appID: "appId") | ||
let request = StartPasskeySignInRequest( | ||
requestConfiguration: configWithTenant | ||
) | ||
let body = request.unencodedHTTPRequestBody | ||
XCTAssertEqual(body!["tenantId"], "TEST_TENANT") | ||
} | ||
|
||
func testUnencodedHTTPRequestBody_WithoutTenantId() { | ||
let request = StartPasskeySignInRequest(requestConfiguration: config) | ||
XCTAssertNil(request.unencodedHTTPRequestBody) | ||
} | ||
} | ||
|
||
#endif |
75 changes: 75 additions & 0 deletions
75
FirebaseAuth/Tests/Unit/StartPasskeySignInResponseTests.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,75 @@ | ||
// 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, *) | ||
final class StartPasskeySignInResponseTests: XCTestCase { | ||
private func makeValidDictionary() -> [String: AnyHashable] { | ||
return [ | ||
"credentialRequestOptions": [ | ||
"rpId": "FAKE_RPID", | ||
"challenge": "FAKE_CHALLENGE", | ||
] as [String: AnyHashable], | ||
] | ||
} | ||
|
||
func testInitWithValidDictionary() throws { | ||
let dict = makeValidDictionary() | ||
let response = try StartPasskeySignInResponse(dictionary: dict) | ||
XCTAssertEqual(response.rpID, "FAKE_RPID") | ||
XCTAssertEqual(response.challenge, "FAKE_CHALLENGE") | ||
} | ||
|
||
/// Helper function to remove nested field from 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 testInitWithInvalidDictionary() throws { | ||
struct TestCase { | ||
let name: String | ||
let removeFieldPath: [String] | ||
} | ||
let cases: [TestCase] = [ | ||
.init(name: "Missing credential options", removeFieldPath: ["credentialRequestOptions"]), | ||
.init(name: "Missing rpId", removeFieldPath: ["credentialRequestOptions", "rpId"]), | ||
.init( | ||
name: "Missing challenge", | ||
removeFieldPath: ["credentialRequestOptions", "challenge"] | ||
), | ||
] | ||
for testCase in cases { | ||
var dict = makeValidDictionary() | ||
removeField(&dict, keyPath: testCase.removeFieldPath) | ||
XCTAssertThrowsError(try StartPasskeySignInResponse(dictionary: dict), | ||
testCase.name) { error in | ||
let nsError = error as NSError | ||
XCTAssertEqual(nsError.domain, AuthErrorDomain) | ||
XCTAssertEqual(nsError.code, AuthErrorCode.internalError.rawValue) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
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.
Optional: This error message can be moved to a constant