Auth0 Swift
認証ライブラリ
Auth0 Swift
概要
Auth0 SwiftはAuth0プラットフォーム専用のiOS/macOS認証SDKです。Universal Login、ソーシャル認証、多要素認証(MFA)をネイティブアプリケーションで簡単に実装できます。モダンなSwift APIを提供し、KeychainやBiometric認証との統合も標準でサポートされており、2025年現在も活発に開発が続けられています。
詳細
Auth0 SwiftはAuth0プラットフォームと完全に統合されたiOS/macOS用の認証ライブラリです。以下の主な特徴があります:
- Universal Login統合: Auth0のHosted Login Pageとのシームレスな連携
- ソーシャル認証: Google、Facebook、AppleなどのプロバイダーとのOAuth統合
- Keychain統合: 認証情報の安全な保存とCredentials Manager
- Biometric認証: Touch ID、Face IDを活用したパスワードレス認証
- 多要素認証: Guardian SDKとの統合によるプッシュ通知ベースのMFA
- Swift Package Manager: モダンなSwiftエコシステムとの親和性
メリット・デメリット
メリット
- Auth0プラットフォームとの完全統合により設定が簡単
- エンタープライズグレードのセキュリティ機能をすぐに利用可能
- iOS/macOSのネイティブ機能(Keychain、Biometrics)との統合
- 豊富なアイデンティティプロバイダーとの連携
- 包括的なドキュメントとコミュニティサポート
- Swift Package Managerによる簡単な依存関係管理
デメリット
- Auth0プラットフォームに依存するため他の認証サービスとの併用が困難
- 商用利用時はAuth0の利用料金が発生
- iOS/macOSプラットフォーム専用でクロスプラットフォーム対応が限定的
- カスタマイズ性がAuth0の機能範囲に制限される
参考ページ
書き方の例
基本的なWebAuth設定
import Auth0
// Universal Login開始
Auth0
.webAuth()
.scope("openid profile offline_access")
.audience("https://your-domain.auth0.com/userinfo")
.start { result in
switch result {
case .success(let credentials):
print("Credentials: \(credentials)")
// 認証情報をKeychain Manager経由で保存
credentialsManager.store(credentials: credentials)
case .failure(let error):
print("Error: \(error)")
}
}
AppDelegateでの認証コールバック処理
import Auth0
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool {
return Auth0.resumeAuth(url)
}
// macOS向け
func application(_ application: NSApplication, open urls: [URL]) {
Auth0.resumeAuth(urls)
}
Credentials Managerによるトークン管理
import Auth0
let credentialsManager = CredentialsManager(authentication: Auth0.authentication())
// 認証情報の保存
credentialsManager.store(credentials: credentials)
// 有効な認証情報の取得
credentialsManager.credentials { result in
switch result {
case .success(let credentials):
// 有効なトークンを使用
print("Access token: \(credentials.accessToken)")
case .failure(let error):
// 再認証が必要
print("Need to re-authenticate: \(error)")
}
}
Touch ID/Face IDを使用したパスワードレス認証
import Auth0
import LocalAuthentication
// Credentials ManagerでBiometric認証を有効化
credentialsManager.enableBiometrics(withTitle: "Touch ID", cancelTitle: "Cancel", fallbackTitle: "Use Password")
// Biometric認証でトークン取得
credentialsManager.credentials { result in
switch result {
case .success(let credentials):
// Touch ID/Face ID認証後にトークンを取得
print("Authenticated with biometrics")
case .failure(let error):
// Biometric認証失敗またはトークン無効
print("Biometric auth failed: \(error)")
}
}
ユーザープロファイル情報の取得
import Auth0
guard let accessToken = credentials.accessToken else { return }
Auth0
.authentication()
.userInfo(withAccessToken: accessToken)
.start { result in
switch result {
case .success(let profile):
print("User ID: \(profile.sub)")
print("Email: \(profile.email ?? "No email")")
print("Name: \(profile.name ?? "No name")")
case .failure(let error):
print("Failed to get profile: \(error)")
}
}
ログアウト処理
import Auth0
// Auth0からのログアウト(Universal Login)
Auth0
.webAuth()
.clearSession { result in
switch result {
case .success:
print("Successfully logged out")
// ローカルの認証情報も削除
credentialsManager.clear()
case .failure(let error):
print("Logout failed: \(error)")
}
}
// ローカル認証情報のクリア
credentialsManager.clear()