Introduction
This guide explains how to use the PasskeyManager
class to register a new passkey within your iOS application. We’ll cover the necessary configurations and provide code examples with detailed explanations.
Prerequisites
Before integrating passkey registration, ensure the following prerequisites are met. You may proceed to the Passkey Registration section if you have already configured the associated domains and the app site association file.
Associated Domains Entitlement
Your app must have the Associated Domains capability enabled. This allows your app to access passkeys stored in the user’s iCloud Keychain. Ensure that your domain supports HTTPS and is properly configured.
- In Xcode, select your project and navigate to the Signing & Capabilities tab.
- Click the + Capability button and add Associated Domains.
- Add your domain to the Associated Domains section, prefixed with webcredentials:. For example:
Reference: Apple Developer Documentation - Supporting Associated Domains
Apple App Site Association File
Your domain must host an apple-app-site-association
file that specifies the app identifiers allowed to access credentials. The file should be available at:
The content of the file should include the webcredentials service, as shown:
Replace <your-app-prefix>
and <your-app-bundle-id>
with your actual App ID prefix and bundle identifier.
Passkey Registration
Once the prerequisites are in place, you can proceed to implement passkey registration using PasskeyManager
.
Import Required Modules
At the top of your ViewController
or relevant class, import the necessary modules:
Initialize PasskeyManager
Create an instance of PasskeyManager
, providing the Relying Party Identifier and the presentation anchor.
Set Up User Interface
Implement a method to initiate passkey registration, typically triggered by a user action such as tapping a button.
The PasskeyManager
requires two parameters:
rpId
: The relying party identifier, typically your domain. This must match the domain configured in the Associated Domains entitlement and theapple-app-site-association
file.presentationAnchor
: The window in which the authentication services will present UI, usually obtained fromview.window
.
ViewController.swift
Register for Notifications
To handle the results of the passkey registration process, register for the relevant notifications provided by PasskeyManager.
Cleanup
Remove the observers when they are no longer needed to avoid memory leaks.
Implement Notification Handlers
Define the methods that handle the passkey registration outcomes.
Sign Up New User
After successful passkey registration, use the PasskeyRegistrationResult
to sign up a new user by creating a sub-organization using the TurnkeyClient
from the TurnkeySDK.
Initialize TurnkeyClient with Proxy
When handling the completion of passkey registration, set up the TurnkeyClient
with a proxy server URL using the ProxyMiddleware
. This configuration is essential for situations where the parent organization’s API keys are required to authenticate requests for creating a sub-organization. Your backend should relay the request to the Turnkey API, ensuring it is authenticated with the parent organization’s API keys.
The middleware adds an X-Turnkey-Request-Url
header to each request, which contains the original request URL. For more details, see the Proxy Middleware guide.
Attestation Object
Construct the attestation object using the PasskeyRegistrationResult
.
Define Parameters
Set up the necessary parameters for the sub-organization and root user. We’ll use the passkeyRegistrationResult
we received in the previous step to create a passkey authenticator for this new sub-organization.
You can find more information about the optional parameters in the Organization Features section of the documentation.
Create Sub-Organization
Use the TurnkeyClient
to create the sub-organization with the provided parameters.
Handle the Response
Process the response from the createSubOrganization
call to retrieve information about the new sub-organization and root users.
References
Was this page helpful?