iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔐

Integrating iOS Widgets with Auth0

に公開

This article explains how to share authentication information between an iOS app and its Widget using Auth0's authentication mechanism via Keychain.

Auth0 is an IDaaS (Identity as a Service) platform. It offers a free tier, so you should be able to try the following simple samples for free.
https://auth0.com/en

The iOS QuickStart guide provides a clear walkthrough for integrating Auth0 into an iOS app.

https://auth0.com/docs/quickstart/native/ios-swift/interactive

Steps to verify the sample app

Below are the notes on how I verified Auth0 authentication using the sample app.

  1. Select Quick Start iOS and download the Sample.
  2. Change the Team ID and Bundle ID in the Signing settings to match your own Apple Developer Program (ADP) account.
  3. Associated Domains need to be configured under Capabilities, but they are already pre-configured in the downloaded version.
  4. In the Auth0 Settings, copy and paste the Allowed Callback URLs and Allowed Logout URLs, replace the BUNDLE_ID, and register them.
  5. Set the Team ID and App ID in Advanced Settings.
  6. Build and install the app.
  7. Launch the app and log in.
  8. Google Sign-in.
  9. Confirm that you can log in.
  10. Confirm that you can log out.

Auth0 provides a helper class called CredentialsManager for managing authentication credentials.

https://auth0.github.io/Auth0.swift/documentation/auth0/credentialsmanager/

CredentialsManager handles the Keychain, and Auth0 also provides SimpleKeychain to make working with the Keychain easier.

https://auth0.github.io/Auth0.swift/documentation/auth0/simplekeychain

To enable Refresh Tokens, you need to include offline_access in the scope of your request. To function like the sample, profile email is also required.

To share the Keychain between the app and the Widget, group settings are required.

https://developer.apple.com/documentation/security/sharing-access-to-keychain-items-among-a-collection-of-apps

To share the Keychain, you need to specify the following for SimpleKeychain:

  • service
  • accessGroup

https://github.com/auth0/SimpleKeychain/blob/master/SimpleKeychain/SimpleKeychain.swift

By default, service is set to Bundle.main.bundleIdentifier. Since the app and the Widget have different Bundle IDs, you need to explicitly specify a unique ID. This service is required for sharing the Keychain; as long as you set a common value, it doesn't necessarily have to be the Bundle ID.

accessGroup sets the value of the Keychain Group. The Team ID prefix is also necessary.

With this setup, the login information saved in the Keychain when logging into the app becomes accessible from the Widget as well.

GitHubで編集を提案

Discussion