-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Passkey new finalize enrollment #15163
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
Changes from all commits
7ccfebf
104be78
6bf5556
0f7bcef
7a0538b
2a244a5
0cc5f5d
d7bb9ca
4d6831b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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 finalizePasskeyEnrollment rpc | ||
private let finalizePasskeyEnrollmentEndPoint = "accounts/passkeyEnrollment:finalize" | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
class FinalizePasskeyEnrollmentRequest: IdentityToolkitRequest, AuthRPCRequest { | ||
typealias Response = FinalizePasskeyEnrollmentResponse | ||
|
||
/// The raw user access token. | ||
let idToken: String | ||
/// The passkey name. | ||
let name: String | ||
/// The credential ID. | ||
let credentialID: String | ||
/// The CollectedClientData object from the authenticator. | ||
let clientDataJSON: String | ||
/// The attestation object from the authenticator. | ||
let attestationObject: String | ||
|
||
init(idToken: String, | ||
name: String, | ||
credentialID: String, | ||
clientDataJSON: String, | ||
attestationObject: String, | ||
requestConfiguration: AuthRequestConfiguration) { | ||
self.idToken = idToken | ||
self.name = name | ||
self.credentialID = credentialID | ||
self.clientDataJSON = clientDataJSON | ||
self.attestationObject = attestationObject | ||
super.init( | ||
endpoint: finalizePasskeyEnrollmentEndPoint, | ||
requestConfiguration: requestConfiguration, | ||
useIdentityPlatform: true | ||
) | ||
} | ||
|
||
var unencodedHTTPRequestBody: [String: AnyHashable]? { | ||
var postBody: [String: AnyHashable] = [ | ||
"idToken": idToken, | ||
"name": name, | ||
] | ||
let authAttestationResponse: [String: AnyHashable] = [ | ||
"clientDataJSON": clientDataJSON, | ||
"attestationObject": attestationObject, | ||
] | ||
let authRegistrationResponse: [String: AnyHashable] = [ | ||
"id": credentialID, | ||
"response": authAttestationResponse, | ||
] | ||
postBody["authenticatorRegistrationResponse"] = authRegistrationResponse | ||
if let tenantId = tenantID { | ||
postBody["tenantId"] = tenantId | ||
} | ||
return postBody | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 FinalizePasskeyEnrollmentResponse: AuthRPCResponse { | ||
/// The user raw access token. | ||
let idToken: String | ||
/// Refresh token for the authenticated user. | ||
let refreshToken: String | ||
|
||
init(dictionary: [String: AnyHashable]) throws { | ||
guard | ||
let idToken = dictionary["idToken"] as? String, | ||
let refreshToken = dictionary["refreshToken"] as? String | ||
else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
self.idToken = idToken | ||
self.refreshToken = refreshToken | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 | ||
|
||
public final class PasskeyInfo: NSObject, AuthProto, NSSecureCoding, Sendable { | ||
/// The display name for this passkey. | ||
public let name: String? | ||
/// The credential ID used by the server. | ||
public let credentialID: String? | ||
required init(dictionary: [String: AnyHashable]) { | ||
name = dictionary["name"] as? String | ||
credentialID = dictionary["credentialId"] as? String | ||
} | ||
|
||
// NSSecureCoding | ||
public static var supportsSecureCoding: Bool { true } | ||
|
||
public func encode(with coder: NSCoder) { | ||
coder.encode(name, forKey: "name") | ||
coder.encode(credentialID, forKey: "credentialId") | ||
} | ||
|
||
public required init?(coder: NSCoder) { | ||
name = coder.decodeObject(of: NSString.self, forKey: "name") as String? | ||
credentialID = coder.decodeObject(of: NSString.self, forKey: "credentialId") as String? | ||
super.init() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ extension User: NSSecureCoding {} | |
/// | ||
/// This property is available on iOS only. | ||
@objc public private(set) var multiFactor: MultiFactor | ||
public private(set) var enrolledPasskeys: [PasskeyInfo]? | ||
#endif | ||
|
||
/// [Deprecated] Updates the email address for the user. | ||
|
@@ -1107,6 +1108,52 @@ extension User: NSSecureCoding {} | |
userID: userIdInData | ||
) | ||
} | ||
|
||
/// Finalize the passkey enrollment with the platfrom public key credential. | ||
/// - Parameter platformCredential: The name for the passkey to be created. | ||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
public func finalizePasskeyEnrollment(withPlatformCredential platformCredential: ASAuthorizationPlatformPublicKeyCredentialRegistration) async throws | ||
-> AuthDataResult { | ||
let credentialID = platformCredential.credentialID.base64EncodedString() | ||
let clientDataJSON = platformCredential.rawClientDataJSON.base64EncodedString() | ||
let attestationObject = platformCredential.rawAttestationObject!.base64EncodedString() | ||
|
||
let request = FinalizePasskeyEnrollmentRequest( | ||
idToken: rawAccessToken(), | ||
name: passkeyName ?? "Unnamed account (Apple)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would have already set this in start, do we really need to this check again? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case, since passkeyName is an optional string, we either need to provide a default value like this, or force-unwrap using '!' to abort execution if the optional value contains nil. There's no compiler guarantee that startEnrollment would be called and will set the passkeyName. Although, we can add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's okay if we need to keep it, you can update this to use the same constant as you used in the StartEnrollment flow once you merge |
||
credentialID: credentialID, | ||
clientDataJSON: clientDataJSON, | ||
attestationObject: attestationObject, | ||
requestConfiguration: auth!.requestConfiguration | ||
) | ||
let response = try await backend.call(with: request) | ||
let user = try await auth!.completeSignIn( | ||
withAccessToken: response.idToken, | ||
accessTokenExpirationDate: nil, | ||
pashanka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
refreshToken: response.refreshToken, | ||
anonymous: false | ||
) | ||
return AuthDataResult(withUser: user, additionalUserInfo: nil) | ||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *) | ||
public func unenrollPasskey(withCredentialID credentialID: String) async throws { | ||
guard !credentialID.isEmpty else { | ||
throw AuthErrorCode.missingPasskeyEnrollment | ||
} | ||
let request = SetAccountInfoRequest( | ||
requestConfiguration: auth!.requestConfiguration | ||
) | ||
request.deletePasskeys = [credentialID] | ||
request.accessToken = rawAccessToken() | ||
let response = try await backend.call(with: request) | ||
_ = try await auth!.completeSignIn( | ||
withAccessToken: response.idToken, | ||
accessTokenExpirationDate: response.approximateExpirationDate, | ||
refreshToken: response.refreshToken, | ||
anonymous: false | ||
) | ||
} | ||
#endif | ||
|
||
// MARK: Internal implementations below | ||
|
@@ -1130,6 +1177,7 @@ extension User: NSSecureCoding {} | |
tenantID = nil | ||
#if os(iOS) | ||
multiFactor = MultiFactor(withMFAEnrollments: []) | ||
enrolledPasskeys = [] | ||
#endif | ||
uid = "" | ||
hasEmailPasswordCredential = false | ||
|
@@ -1364,6 +1412,7 @@ extension User: NSSecureCoding {} | |
multiFactor = MultiFactor(withMFAEnrollments: enrollments) | ||
} | ||
multiFactor.user = self | ||
enrolledPasskeys = user.enrolledPasskeys ?? [] | ||
#endif | ||
} | ||
|
||
|
@@ -1760,6 +1809,7 @@ extension User: NSSecureCoding {} | |
private let kMetadataCodingKey = "metadata" | ||
private let kMultiFactorCodingKey = "multiFactor" | ||
private let kTenantIDCodingKey = "tenantID" | ||
private let kEnrolledPasskeysKey = "passkeys" | ||
|
||
public static let supportsSecureCoding = true | ||
|
||
|
@@ -1782,6 +1832,7 @@ extension User: NSSecureCoding {} | |
coder.encode(tokenService, forKey: kTokenServiceCodingKey) | ||
#if os(iOS) | ||
coder.encode(multiFactor, forKey: kMultiFactorCodingKey) | ||
coder.encode(enrolledPasskeys, forKey: kEnrolledPasskeysKey) | ||
#endif | ||
} | ||
|
||
|
@@ -1811,6 +1862,9 @@ extension User: NSSecureCoding {} | |
let tenantID = coder.decodeObject(of: NSString.self, forKey: kTenantIDCodingKey) as? String | ||
#if os(iOS) | ||
let multiFactor = coder.decodeObject(of: MultiFactor.self, forKey: kMultiFactorCodingKey) | ||
let passkeyAllowed: [AnyClass] = [NSArray.self, PasskeyInfo.self] | ||
let passkeys = coder.decodeObject(of: passkeyAllowed, | ||
forKey: kEnrolledPasskeysKey) as? [PasskeyInfo] | ||
#endif | ||
self.tokenService = tokenService | ||
uid = userID | ||
|
@@ -1844,6 +1898,7 @@ extension User: NSSecureCoding {} | |
self.multiFactor = multiFactor ?? MultiFactor() | ||
super.init() | ||
multiFactor?.user = self | ||
enrolledPasskeys = passkeys ?? [] | ||
#endif | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.