Logto は、モダンなアプリや SaaS 製品向けに設計された Auth0 の代替です。 Cloud と オープンソース の両方のサービスを提供し、アイデンティティと管理 (IAM) システムを迅速に立ち上げるのに役立ちます。認証 (Authentication)、認可 (Authorization)、マルチテナント管理を すべて一つに まとめて楽しんでください。
Logto Cloud で無料の開発テナントから始めることをお勧めします。これにより、すべての機能を簡単に探索できます。
この記事では、iOS (Swift) と Logto を使用して、SAML サインイン体験(ユーザー認証 (Authentication))を迅速に構築する手順を説明します。
前提条件
- 稼働中の Logto インスタンス。紹介ページ をチェックして始めてください。
- iOS (Swift) の基本的な知識。
- 使用可能な SAML アカウント。
Logto でアプリケーションを作成する
Logto は OpenID Connect (OIDC) 認証 (Authentication) と OAuth 2.0 認可 (Authorization) に基づいています。これは、複数のアプリケーション間でのフェデレーテッドアイデンティティ管理をサポートし、一般的にシングルサインオン (SSO) と呼ばれます。
あなたの Native app アプリケーションを作成するには、次の手順に従ってください:
- Logto コンソール を開きます。「Get started」セクションで、「View all」リンクをクリックしてアプリケーションフレームワークのリストを開きます。あるいは、Logto Console > Applications に移動し、「Create application」ボタンをクリックします。
- 開いたモーダルで、左側のクイックフィルターチェックボックスを使用して、利用可能なすべての "Native app" フレームワークをフィルタリングするか、"Native app" セクションをクリックします。"iOS (Swift)" フレームワークカードをクリックして、アプリケーションの作成を開始します。
- アプリケーション名を入力します。例:「Bookstore」と入力し、「Create application」をクリックします。
🎉 タダーン!Logto で最初のアプリケーションを作成しました。詳細な統合ガイドを含むお祝いページが表示されます。ガイドに従って、アプリケーションでの体験を確認してください。
iOS (Swift) SDK を統合する
Logto SDK を依存関係として追加する
The minimum supported iOS version of Logto Swift SDK is iOS 13.
Logto Swift SDK comes in two major versions:
- v1: Opens the sign-in experience in an embedded WebView, which is required by the native social plugin targets, but does not support passkey sign-in (WebView does not support WebAuthn, the underlying standard of passkeys).
- v2 (beta): Opens the sign-in experience in
ASWebAuthenticationSession(the system browser), which unlocks passkey sign-in and shares the browser session. Note that v2 removes the native social plugin targets; social connectors still work through the browser. If you depend on the native WeChat or Alipay SDK handoff, stay on v1.
This guide covers both versions. Choose your version in the tabs below, and the choice will be kept in sync throughout this guide.
Use the following URL to add Logto SDK as a dependency in Swift Package Manager.
https://github.com/logto-io/swift.git
Since Xcode 11, you can directly import a Swift package w/o any additional tool.
When Xcode asks for the package version, choose the version you want to integrate:
- v2 (beta)
- v1
v2 is released as 2.0.0-beta.x prereleases until GA. Use 2.0.0-beta.1 or the latest 2.0.0-beta.x prerelease as the version. During beta, we recommend selecting the prerelease explicitly instead of relying on a normal version range to pick it automatically.
If you use Package.swift directly:
.package(url: "https://github.com/logto-io/swift.git", exact: "2.0.0-beta.1")
Use the latest v1 release as the stable line. The latest v1 version is 1.2.0.
If you use Package.swift directly:
.package(url: "https://github.com/logto-io/swift.git", from: "1.2.0")
We do not support Carthage and CocoaPods at the time due to some technical issues.
Carthage
Carthage needs a xcodeproj file to build. We will try to find a workaround later.
CocoaPods
CocoaPods does not support local dependency and monorepo, thus it's hard to create a .podspec for this repo.
LogtoClient を初期化する
LogtoConfig オブジェクトを使用して LogtoClient インスタンスを作成することで、クライアントを初期化します。
import Logto
import LogtoClient
let config = try? LogtoConfig(
endpoint: "<your-logto-endpoint>", // 例: http://localhost:3001
appId: "<your-app-id>"
)
let client = LogtoClient(useConfig: config)
デフォルトでは、ID トークンやリフレッシュ トークンのような資格情報を Keychain に保存します。したがって、ユーザーは戻ってきたときに再度サインインする必要はありません。
この動作をオフにするには、usingPersistStorage を false に設定します:
let config = try? LogtoConfig(
// ...
usingPersistStorage: false
)
サインイン
詳細に入る前に、エンドユーザー体験の概要を簡単にご紹介します。サインインプロセスは次のようにシンプルにまとめられます:
- アプリがサインインメソッドを呼び出します。
- ユーザーは Logto のサインインページにリダイレクトされます。ネイティブアプリの場合は、システムブラウザが開かれます。
- ユーザーがサインインし、アプリ(リダイレクト URI として設定)に戻されます。
リダイレクトベースのサインインについて
- この認証 (Authentication) プロセスは OpenID Connect (OIDC) プロトコルに従い、Logto はユーザーのサインインを保護するために厳格なセキュリティ対策を講じています。
- 複数のアプリがある場合、同じアイデンティティプロバイダー (Logto) を使用できます。ユーザーがあるアプリにサインインすると、Logto は別のアプリにアクセスした際に自動的にサインインプロセスを完了します。
リダイレクトベースのサインインの理論と利点について詳しく知るには、Logto サインイン体験の説明を参照してください。
Configure redirect URI
- v2 (beta)
- v1
Logto コンソールのアプリケーション詳細ページに切り替えましょう。リダイレクト URI io.logto.app://callback を追加し、「変更を保存」をクリックします。

In v2, the sign-in experience opens in ASWebAuthenticationSession (the system browser), and the redirect is routed back to your app through OS-level callback matching. For a custom scheme redirect URI such as io.logto.app://callback, register only the scheme part (io.logto.app) in your app's Info.plist, then add the full redirect URI to your Logto application's Redirect URIs.
In Xcode, open your app target, select Info, expand URL Types, and add one entry with io.logto.app in URL Schemes. If you edit Info.plist directly, add:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>io.logto.app</string>
<key>CFBundleURLSchemes</key>
<array>
<string>io.logto.app</string>
</array>
</dict>
</array>
For the browser flow in v2, you do not need to call LogtoClient.handle(url:); that plugin handoff API was removed with the embedded WebView flow.
Use Universal Links instead of a custom scheme?
You can also use an HTTPS redirect URI such as https://example.com/callback:
- Add the Associated Domains capability to your app.
- Configure
webcredentials:example.comsoASWebAuthenticationSessioncan match HTTPS callbacks on iOS 17.4 and newer. - If the same URL should also open your app as a Universal Link outside the authentication session, configure
applinks:example.comand host a validapple-app-site-associationfile for the domain and path. - Add the HTTPS URI to your Logto application's Redirect URIs.
- Pass the same URI to
signInWithBrowser.
On iOS 17.4 and newer, the SDK uses ASWebAuthenticationSession's HTTPS callback matching API so HTTPS redirects can automatically complete and dismiss the session. On older iOS versions, the authorization request can still use the HTTPS redirect URI, but the session may not close automatically unless your app handles the Universal Link callback itself. Keep a custom scheme redirect as a compatibility option if you need automatic completion on older iOS versions.
Logto コンソールのアプリケーション詳細ページに切り替えましょう。リダイレクト URI io.logto://callback を追加し、「変更を保存」をクリックします。

The Redirect URI in iOS SDK is only for internal use. There's NO NEED to add a Custom URL Scheme until a connector asks.
Sign-in and sign-out
.signInWithBrowser(redirectUri:) を呼び出す前に、Admin Console でリダイレクト URI
が正しく設定されていることを確認してください。 :::
- v2 (beta)
- v1
In v2, client.signOut(postLogoutRedirectUri:) performs a complete sign-out: it clears the local credentials, revokes the refresh token, and ends the Logto session by opening the end session endpoint in the system browser. The browser then navigates back to your app through the post sign-out redirect URI. Before using it, switch to the application details page of Logto Console, add the post sign-out redirect URI io.logto.app://signed-out and click "Save changes". The post sign-out redirect URI can use the same custom scheme you registered for sign-in.
For example, in a SwiftUI app:
struct ContentView: View {
@State var isAuthenticated: Bool
private let redirectUri = "io.logto.app://callback"
private let postLogoutRedirectUri = "io.logto.app://signed-out"
init() {
isAuthenticated = client.isAuthenticated
}
var body: some View {
VStack {
if isAuthenticated {
Button("Sign Out") {
Task { [self] in
let error = await client.signOut(postLogoutRedirectUri: postLogoutRedirectUri)
if let error = error {
print(error)
return
}
isAuthenticated = false
}
}
} else {
Button("Sign In") {
Task { [self] in
do {
try await client.signInWithBrowser(redirectUri: redirectUri)
isAuthenticated = true
} catch let error as LogtoClientErrors.SignIn {
// error occurred during sign in
} catch {
// other errors
}
}
}
}
}
}
}
- You can also call
client.signOut()without a post sign-out redirect URI. No Console configuration is needed in this case: the browser shows the Logto sign-out page, and the user returns to the app by dismissing it manually. - If no UI context is available, you can call
client.clearCredentials()to clear the local credentials and revoke the refresh token. Note that this keeps the Logto session in the browser, so the nextsignInWithBrowsermay silently sign the user back in through that session.
You can use client.signInWithBrowser(redirectUri:) to sign in the user and client.signOut() to sign out the user.
For example, in a SwiftUI app:
struct ContentView: View {
@State var isAuthenticated: Bool
init() {
isAuthenticated = client.isAuthenticated
}
var body: some View {
VStack {
if isAuthenticated {
Button("Sign Out") {
Task { [self] in
await client.signOut()
isAuthenticated = false
}
}
} else {
Button("Sign In") {
Task { [self] in
do {
try await client.signInWithBrowser(redirectUri: "io.logto://callback")
isAuthenticated = true
} catch let error as LogtoClientErrors.SignIn {
// error occurred during sign in
} catch {
// other errors
}
}
}
}
}
}
}
チェックポイント: アプリケーションをテストする
これで、アプリケーションをテストできます:
- アプリケーションを実行すると、サインインボタンが表示されます。
- サインインボタンをクリックすると、SDK がサインインプロセスを初期化し、Logto のサインインページにリダイレクトされます。
- サインインすると、アプリケーションに戻り、サインアウトボタンが表示されます。
- サインアウトボタンをクリックして、トークンストレージをクリアし、サインアウトします。
SAML コネクターを追加する
迅速なサインインを有効にし、ユーザーコンバージョンを向上させるために、アイデンティティプロバイダー (IdP) として iOS (Swift) を接続します。Logto ソーシャルコネクターは、いくつかのパラメーター入力を許可することで、この接続を数分で確立するのに役立ちます。
ソーシャルコネクターを追加するには、次の手順に従ってください:
- Console > Connectors > Social Connectors に移動します。
- 「Add social connector」をクリックし、「SAML」を選択します。
- README ガイドに従い、必要なフィールドを完了し、設定をカスタマイズします。
インプレースコネクターガイドに従っている場合は、次のセクションをスキップできます。
Standard SAML app を設定する
ソーシャル IdP のアカウントを作成し、SAML アプリケーションを登録する (IdP)
SAML コネクターの設定を見ていきましょう。
始める前に、SAML プロトコルをサポートするソーシャルアイデンティティプロバイダーにアクセスし、自分のアカウントを作成できます。Okta、OneLogin、Salesforce などのプラットフォームは、SAML プロトコルに基づく認証 (Authentication) をサポートしています。
IdP が SAML アサーションの暗号化と署名された認証 (Authentication) リクエストの受信を必須とする場合、RSA アルゴリズムを使用してプライベートキーと対応する証明書を生成する必要があります。プライベートキーは SP 用に保持し、証明書を IdP にアップロードしてください。
また、IdP の SAML アサーションを処理するために ACS (Assertion Consumer Service) URL を ${your_logto_origin}/api/authn/saml/${connector_id} として設定する必要があります。connectorId は Logto の管理コンソールの SAML コネクターの詳細ページで見つけることができます。
現在の Logto の設計では、認証 (Authentication) リクエストの送信にはリダイレクトバインディングのみをサポートし、SAML アサーションの受信には POST バインディングのみをサポートしています。これはあまりクールではないかもしれませんが、現在の設計はほとんどのユースケースを処理できると信じています。問題がある場合は、お気軽にお問い合わせください!
SAML コネクターの設定 (SP)
このセクションでは、各属性を詳細に紹介します。
entityID 必須
entityID (つまり issuer) はエンティティ識別子です。これは、エンティティ (SAML SP エンティティ) を識別し、各 SAML リクエスト / レスポンスでの同等性を一致させるために使用されます。
signInEndpoint 必須
SAML 認証 (Authentication) リクエストを送信する IdP のエンドポイントです。通常、この値は IdP の詳細ページ (つまり IdP の SSO URL または Login URL) で見つけることができます。
x509Certificate 必須
IdP のプライベートキーから生成された x509 証明書で、IdP はこの値を持っていることが期待されます。
証明書の内容は -----BEGIN CERTIFICATE----- ヘッダーと -----END CERTIFICATE----- テールを伴います。
idpMetadataXml 必須
このフィールドは、IdP メタデータ XML ファイルの内容を配置するために使用されます。
使用している XML パーサーはカスタマイズされた名前空間をサポートしていません。 IdP メタデータに名前空間が含まれている場合は、手動で削除する必要があります。 XML ファイルの名前空間については、参考を参照してください。
assertionConsumerServiceUrl 必須
アサーションコンシューマーサービス (ACS) URL は、IdP の SAML アサーション POST リクエストを受信する SP のエンドポイントです。前の部分で述べたように、通常は IdP 設定で設定されますが、一部の IdP はこの値を SAML 認証 (Authentication) リクエストから取得します。そのため、この値も必須フィールドとして追加しています。この値は ${your_logto_origin}/api/authn/saml/${connector_id} のようになります。
signAuthnRequest
SAML 認証 (Authentication) リクエストに署名するかどうかを制御するブール値で、デフォルト値は false です。
encryptAssertion
encryptAssertion は、IdP が SAML アサーションを暗号化するかどうかを示すブール値で、デフォルト値は false です。
signAuthnRequest と encryptAssertion 属性は、IdP 設定の対応するパラメーターと一致する必要があります。そうでない場合、設定が一致しないことを示すエラーがスローされます。
すべての SAML レスポンスには署名が必要です。
requestSignatureAlgorithm
これは、Logto が SAML アサーションの署名を検証できるように、IdP の署名アルゴリズムと一致させる必要があります。その値は http://www.w3.org/2000/09/xmldsig#rsa-sha1、http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 または http://www.w3.org/2001/04/xmldsig-more#rsa-sha512 のいずれかであり、デフォルト値は http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 です。
messageSigningOrder
messageSigningOrder は、IdP の署名と暗号化の順序を示します。その値は sign-then-encrypt または encrypt-then-sign のいずれかであり、デフォルト値は sign-then-encrypt です。
privateKey と privateKeyPass
privateKey はオプションの値であり、signAuthnRequest が true の場合に必要です。
privateKeyPass は privateKey を作成する際に設定したパスワードで、必要に応じて必要です。
signAuthnRequest が true の場合、privateKey から生成された対応する証明書が IdP によって署名の確認に必要です。
encPrivateKey と encPrivateKeyPass
encPrivateKey はオプションの値であり、encryptAssertion が true の場合に必要です。
encPrivateKeyPass は encPrivateKey を作成する際に設定したパスワードで、必要に応じて必要です。
encryptAssertion が true の場合、encPrivateKey から生成された対応する証明書が IdP によって SAML アサーションの暗号化に必要です。
キーと証明書の生成には、openssl が素晴らしいツールです。以下は役立つかもしれないサンプルコマンドラインです:
openssl genrsa -passout pass:${privateKeyPassword} -out ${encryptPrivateKeyFilename}.pem 4096
openssl req -new -x509 -key ${encryptPrivateKeyFilename}.pem -out ${encryptionCertificateFilename}.cer -days 3650
privateKey と encPrivateKey ファイルは pem 文字列として pkcs1 スキームでエンコードされることが強制されており、プライベートキーのファイルは -----BEGIN RSA PRIVATE KEY----- で始まり、-----END RSA PRIVATE KEY----- で終わる必要があります。
nameIDFormat
nameIDFormat は、応答する名前 ID フォーマットを宣言するオプションの属性です。値は urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified、urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress、urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName、urn:oasis:names:tc:SAML:2.0:nameid-format:persistent、urn:oasis:names:tc:SAML:2.0:nameid-format:transient のいずれかであり、デフォルト値は urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified です。
timeout
timeout は時間検証のための時間許容範囲であり、SP エンティティと IdP エンティティの間の時間が異なる可能性があり、ネットワーク接続も遅延をもたらす可能性があるためです。単位はミリ秒で、デフォルト値は 5000 (つまり 5 秒) です。
profileMap
Logto は、通常標準ではないソーシャルベンダーのプロファイルからのマッピングをカスタマイズできる profileMap フィールドも提供しています。各 profileMap キーは Logto の標準ユーザープロファイルフィールド名であり、対応する値はソーシャルプロファイルフィールド名である必要があります。現在の段階では、Logto はソーシャルプロファイルから 'id'、'name'、'avatar'、'email'、'phone' のみを考慮しており、'id' のみが必須で、他はオプションフィールドです。
設定タイプ
| 名前 | タイプ | 必須 | デフォルト値 |
|---|---|---|---|
| signInEndpoint | string | true | |
| x509certificate | string | true | |
| idpMetadataXml | string | true | |
| entityID | string | true | |
| assertionConsumerServiceUrl | string | true | |
| messageSigningOrder | encrypt-then-sign | sign-then-encrypt | false | sign-then-encrypt |
| requestSignatureAlgorithm | http://www.w3.org/2000/09/xmldsig#rsa-sha1 | http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 | http://www.w3.org/2001/04/xmldsig-more#rsa-sha512 | false | http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 |
| signAuthnRequest | boolean | false | false |
| encryptAssertion | boolean | false | false |
| privateKey | string | false | |
| privateKeyPass | string | false | |
| nameIDFormat | urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified | urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress | urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName | urn:oasis:names:tc:SAML:2.0:nameid-format:persistent | urn:oasis:names:tc:SAML:2.0:nameid-format:transient | false | urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified |
| timeout | number | false | 5000 |
| profileMap | ProfileMap | false |
| ProfileMap フィールド | タイプ | 必須 | デフォルト値 |
|---|---|---|---|
| id | string | false | id |
| name | string | false | name |
| avatar | string | false | avatar |
| string | false | ||
| phone | string | false | phone |
参考
- OASIS Security Assertion Markup Language (SAML) V2.0 のプロファイル
- samlify - シングルサインオンのための高度に設定可能な Node.js SAML 2.0 ライブラリ
設定を保存する
Logto コネクター設定エリアで必要な値をすべて記入したことを確認してください。「保存して完了」または「変更を保存」をクリックすると、SAML コネクターが利用可能になります。
サインイン体験で SAML コネクターを有効にする
ソーシャルコネクターを正常に作成したら、サインイン体験で「SAML で続行」ボタンとして有効にすることができます。
- Console > サインイン体験 > サインアップとサインイン に移動します。
- (オプション)ソーシャルログインのみが必要な場合は、サインアップ識別子に「該当なし」を選択します。
- 設定済みの SAML コネクターを「ソーシャルサインイン」セクションに追加します。
テストと検証
iOS (Swift) アプリに戻ります。これで SAML を使用してサインインできるはずです。お楽しみください!
さらなる読み物
エンドユーザーフロー:Logto は、MFA やエンタープライズシングルサインオン (SSO) を含む即時使用可能な認証 (Authentication) フローを提供し、アカウント設定、セキュリティ検証、マルチテナント体験の柔軟な実装のための強力な API を備えています。
認可 (Authorization):認可 (Authorization) は、ユーザーが認証 (Authentication) された後に行えるアクションやアクセスできるリソースを定義します。ネイティブおよびシングルページアプリケーションの API を保護し、ロールベースのアクセス制御 (RBAC) を実装する方法を探ります。
組織 (Organizations):特にマルチテナント SaaS や B2B アプリで効果的な組織機能は、テナントの作成、メンバー管理、組織レベルの RBAC、およびジャストインタイムプロビジョニングを可能にします。
顧客 IAM シリーズ:顧客(または消費者)アイデンティティとアクセス管理に関する連続ブログ投稿で、101 から高度なトピックまでを網羅しています。