AppAuth for iOS
AppAuth for iOS
概要
AppAuth for iOSは、OpenID Foundationによって開発されたiOS、macOS、tvOS向けのOAuth2およびOpenID Connect認証ライブラリです。2025年現在、iOS 12+、macOS 10.14+をサポートし、バージョン2.0.0として最新の機能を提供しています。RFC 8252(OAuth 2.0 for Native Apps)の仕様に準拠し、SFAuthenticationSessionやSFSafariViewControllerを使用したセキュアな認証を実装しています。PKCE(Proof Key for Code Exchange)の自動サポート、Custom URI SchemeおよびUniversal Linksの両方への対応、tvOS向けのDevice Authorization Grant、Appleプラットフォーム向けに最適化されたセキュリティ機能を提供します。
特徴
AppAuth for iOS 2.0は、RFC 6749(OAuth 2.0)、RFC 7636(PKCE)、OpenID Connect仕様の完全な実装を提供するiOSアプリケーション向けの包括的な認証ライブラリです。主な特徴として、プラットフォーム固有のAppAuthCoreフレームワークによる各プラットフォーム専用の最適化が施されています。認証フローはSFSafariViewController(iOS 9+)またはSFAuthenticationSession(iOS 12+)を使用し、UIWebViewやWKWebViewは使用せず、セキュリティを重視した設計となっています。OIDServiceConfiguration、OIDAuthorizationRequest、OIDTokenRequest、OIDAuthStateなどの主要なクラスによりOAuth 2.0のすべてのフローを網羅し、開発者にとって直感的なAPIを提供しています。
主な機能
- RFC準拠: OAuth 2.0、OpenID Connect、PKCEの完全実装
- Apple統合: SFSafariViewController、SFAuthenticationSession、Universal Links対応
- tvOS サポート: Device Authorization Grant(RFC 8628)によるtvOS認証
- 自動PKCE: セキュリティ強化のためのPKCE自動実装
- 状態管理: 認証状態の永続化とセッション管理
- Swift対応: Swift Package Manager対応とSwift APIの提供
使用例
インストール
// Swift Package Manager
dependencies: [
.package(url: "https://github.com/openid/AppAuth-iOS", from: "2.0.0")
]
基本的な認証フロー
import AppAuth
class OAuthManager {
private let clientID = "your-client-id"
private let redirectURI = URL(string: "com.yourapp://oauth2redirect")!
private let issuer = URL(string: "https://accounts.google.com")!
func startAuthentication(from viewController: UIViewController) {
// 1. サービス設定の発見
OIDAuthorizationService.discoverConfiguration(forIssuer: issuer) { config, error in
guard let configuration = config else {
print("Error: \(error?.localizedDescription ?? "Unknown error")")
return
}
// 2. 認証リクエストの作成
let request = OIDAuthorizationRequest(
configuration: configuration,
clientId: self.clientID,
scopes: [OIDScopeOpenID, OIDScopeProfile, OIDScopeEmail],
redirectURL: self.redirectURI,
responseType: OIDResponseTypeCode,
additionalParameters: nil
)
// 3. 認証フローの開始
let authState = OIDAuthState.authState(
byPresenting: request,
presenting: viewController
) { authState, error in
if let authState = authState {
print("認証成功: \(authState.lastTokenResponse?.accessToken ?? "")")
self.saveAuthState(authState)
} else {
print("認証エラー: \(error?.localizedDescription ?? "")")
}
}
}
}
}
URL Scheme設定
<!-- Info.plist -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.yourapp</string>
</array>
</dict>
</array>
SwiftUI統合
struct ContentView: View {
@StateObject private var authManager = OAuthManager()
var body: some View {
VStack {
if authManager.isAuthenticated {
Text("認証済み")
.foregroundColor(.green)
Button("ログアウト") {
authManager.logout()
}
} else {
Button("ログイン") {
authManager.startAuthentication()
}
}
}
}
}
tvOS Device Authorization Grant
@available(tvOS 12.0, *)
func startDeviceAuthorization() {
let request = OIDDeviceAuthorizationRequest(
configuration: configuration,
clientId: clientID,
scopes: [OIDScopeOpenID, OIDScopeProfile],
additionalParameters: nil
)
OIDAuthorizationService.perform(request) { response, error in
if let response = response {
print("ユーザーコード: \(response.userCode)")
print("検証URL: \(response.verificationURI)")
// トークンのポーリング開始
self.pollForToken(with: response)
}
}
}
比較・代替手段
優位性
- OpenID Foundationによる公式ライブラリ
- RFC 8252準拠によるApple推奨のセキュリティ実装
- SFSafariViewControllerによる高いセキュリティ
- PKCEサポートによる追加セキュリティ層
- Universal LinksとCustom URI Schemeの両方対応
注意点
- iOS 12+、macOS 10.14+の最新OS要件
- OAuth 2.0/OpenID Connect以外の認証方式には対応しない
- Web View基盤の認証には対応しない
- 複雑な認証フローには追加の実装が必要
類似ライブラリ比較
- Firebase Authentication: Googleのマネージド認証サービス
- Auth0 iOS SDK: Auth0プラットフォーム専用の認証ライブラリ
- Okta iOS SDK: エンタープライズ向けアイデンティティ管理
学習リソース
公式ドキュメント
チュートリアル
まとめ
AppAuth for iOSは、iOSアプリケーションにOAuth 2.0とOpenID Connect認証を統合するための最も信頼性の高いライブラリです。OpenID Foundationによる公式サポート、RFC 8252準拠のセキュリティ実装、Appleプラットフォームとの深い統合により、モバイルアプリケーションの認証要件を効率的に実現できます。特に、SFSafariViewControllerの使用とPKCEサポートにより、現代的なセキュリティ基準を満たした認証システムを構築できる点が大きな魅力です。