Microsoft Entra ID (SAML)
Microsoft Entra IDは、Amazon Cognitoで使用するためのSAMLプロバイダーとして構成できます。Entra IDを統合することで、既存のエンタープライズユーザーでサインインし、Amplifyアプリ内で使用するためのAmplify Authリソースに一意のプロファイルを保持できます。詳細については、Microsoft Entra IDを使用したSAML認証に関するAzureドキュメントをご覧ください。
パーソナルクラウドサンドボックスを開始する
開始するには、適切なリダイレクトURIを使用して認証リソースを定義します:
import { defineAuth } from "@aws-amplify/backend"
/** * Define and configure your auth resource * @see https://docs.amplify.aws/gen2/build-a-backend/auth */export const auth = defineAuth({ loginWith: { email: true, externalProviders: { logoutUrls: ["http://localhost:3000/come-back-soon"], callbackUrls: ["http://localhost:3000/profile"], }, },})npx ampx sandboxでパーソナルクラウドサンドボックスにデプロイします。これにより、新しいEntra IDアプリを構成するために使用できるドメインが生成されます。変更を正常にデプロイした後、amplify_outputs.jsonから生成されたdomain値をコピーします
{ "auth": { "aws_region": "us-east-1", "user_pool_id": "<your-cognito-user-pool-id>", "user_pool_client_id": "<your-cognito-user-pool-client-id>", "identity_pool_id": "<your-cognito-identity-pool-id>", "mfa_methods": [], "standard_required_attributes": [ "email" ], "username_attributes": [ "email" ], "user_verification_types": [ "email" ], "mfa_configuration": "OFF", "password_policy": { "min_length": 8, "require_numbers": true, "require_lowercase": true, "require_uppercase": true, "require_symbols": true }, "oauth": { "identity_providers": [], "redirect_sign_in_uri": [ "http://localhost:3000/profile" ], "redirect_sign_out_uri": [ "http://localhost:3000/come-back-soon" ], "response_type": "code", "scopes": [ "phone", "email", "openid", "profile", "aws.cognito.signin.user.admin" ], "domain": "<some-hash>.auth.us-east-1.amazoncognito.com" }, }, "version": "1"}Microsoft Entra IDのセットアップ
次に、portal.azure.comに移動して、Entra IDを選択します。デフォルトディレクトリまたは企業の既存ディレクトリで、管理の下のエンタープライズアプリケーションを選択します
その後、新しいアプリケーションを選択し、独自のアプリケーションを作成を選択します。アプリケーションの名前を指定し、**ギャラリーに見つからない他のアプリケーション統合(ギャラリー以外)**を選択します
新しいエンタープライズアプリケーションを作成したので、SAMLでシングルサインオンを構成できます。シングルサインオンを選択します
次にSAMLを選択します
SAMLでシングルサインオンを設定するページが表示され、Amplify Authリソースからのいくつかの情報が必要になります。
基本的なSAML構成ステップで編集を選択し、適切な値を入力します。
| ラベル | 値 |
|---|---|
| Identifier (Entity ID) | urn:amazon:cognito:sp:<your-cognito-user-pool-id> |
| Reply URL (Assertion Consumer Service URL) | https://<your-cognito-domain>/saml2/idpresponse |
| Logout Url (Optional) | https://<your-cognito-domain>/saml2/logout |
構成を保存し、ステップ3のSAML証明書セクションに進みます。アプリフェデレーションメタデータURLをコピーします
Entra IDでバックエンドを構成する
SAMLプロバイダーをMicrosoft Entra IDで構成し、アプリフェデレーションメタデータURLをコピーしたので、認証リソースを新しいSAMLプロバイダーで構成し、URLの値をmetadataContentプロパティに貼り付けます:
import { defineAuth } from "@aws-amplify/backend"
/** * Define and configure your auth resource * @see https://docs.amplify.aws/gen2/build-a-backend/auth */export const auth = defineAuth({ loginWith: { email: true, externalProviders: { saml: { name: "MicrosoftEntraIDSAML", metadata: { metadataType: "URL", metadataContent: "<your-metadata-content-url>", }, attributeMapping: { email: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", }, }, logoutUrls: ["http://localhost:3000/come-back-soon"], callbackUrls: ["http://localhost:3000/profile"], }, },})ユーザー属性は、ステップ2の属性とクレームセクションにあり、デフォルトではネームスペースが付与されます。上の例は、SAMLユーザーのメールアドレスのデフォルトクレームをマッピングしていますが、追加の属性をマッピングできます。
オプション: Cognitoサイニング証明書をアップロードする
AWSコンソールで、Cognito User Poolに移動します。Amplify AuthをEntra ID SAMLプロバイダーで構成した後に作成されたIDプロバイダーMicrosoftEntraIDSAMLを選択します。署名証明書を表示を選択し、.crtとしてダウンロードします
ファイルの拡張子を.cerに変更してAzureにアップロードします。シングルサインオンページで、ステップ3(SAML証明書)にスクロールダウンし、検証証明書(オプション)の下で編集を選択します。
検証証明書が必要を選択し、証明書をアップロードします。
変更を保存すると、Cognito User PoolからEntra IDへのリクエストが検証されるようになります。
フロントエンドを接続する
これで、ユーザーはMicrosoft Entra IDでサインインする準備が整いました。このカスタムプロバイダーでサインインするには、認証リソース定義で指定された名前(MicrosoftEntraIDSAML)としてプロバイダー名を指定します
Future<void> signInWithMicrosoftEntraID() async { try { final result = await Amplify.Auth.signInWithWebUI( provider: AuthProvider.custom("MicrosoftEntraIDSAML"), ); safePrint('Sign in result: $result'); } on AuthException catch (e) { safePrint('Error signing in: ${e.message}'); }}