> ## Documentation Index
> Fetch the complete documentation index at: https://docs-dev-actions-triggers-prototype.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ユーザー移行のシナリオ

> 複数の方法を使用したさまざまなプラットフォームからのユーザー移行シナリオ。

Gigya、Okta、StormpathからAuth0にユーザーを移行するためのサンプルシナリオをいくつか示します。これらの各シナリオでは、それらのプラットフォームにアカウントがあることを前提としています。

## 前提条件

[カスタムデータベース接続](/docs/ja-jp/authenticate/database-connections/custom-db)を構成します。

1. [Dashboard>Authentication>Database](https://manage.auth0.com/#/connections/database)でデータベース接続を作成します。必ず **［Custom Database（カスタムデータベース）］** ビューを選択し、**［Use my own database（独自のデータベース）］** を使用するスイッチを有効にしてください。
2. データベースをアプリケーションに接続します：データベース接続設定で、**アプリケーション** ビューを選択します。**この接続を使用しているアプリケーション** セクションを見つけて、各アプリケーションのデータベース接続を有効にします。

## シナリオ1：GigyaからAuth0へのユーザー移行

1. Gigyaユーザーをエクスポート：[GigyaのIdentitySync](https://help.sap.com/docs/SAP_CUSTOMER_DATA_CLOUD/8b8d6fffe113457094a17701f63e3d6a/2b717202e42a4165aa7f75f7eecab64f.html)を使用して、ユーザーデータを変換し、ターゲットスキーマに合わせてエクスポートします。このプロセスの詳細については、[Gigya IdentitySync：Using IdentitySync](https://help.sap.com/docs/SAP_CUSTOMER_DATA_CLOUD/8b8d6fffe113457094a17701f63e3d6a/2b717202e42a4165aa7f75f7eecab64f.html?locale=en-US)を参照してください。

   指示に従って、Gigyaデータベースのユーザーデータを正しい[［schema（スキーマ）］](/docs/ja-jp/manage-users/user-migration/bulk-user-import-database-schema-and-examples)に変換し、変換されたデータをJSON形式でエクスポートします。
2. GigyaユーザーをAuth0にインポートします。[ユーザーインポート/エクスポート拡張](/docs/ja-jp/manage-users/user-migration/user-import-export-extension)または[［Management API］](/docs/ja-jp/api/management/v2)を使用してユーザーをインポートできます。

   * **［User Import/Export Extension（ユーザーインポート/エクスポート拡張）］**：[Auth0 Dashboard>Extensions](https://manage.auth0.com/#/extensions)へ移動し、**［User Import / Export（ユーザーインポート/エクスポート）］** 拡張を選択し、インストールします。拡張がインストールされたら、それをクリックしてインポート/エクスポートインターフェイスを開くことができます。

     エクスポートしたGigyaユーザーのJSONファイルを指定されたアップロード領域にドラッグし、先ほど作成したデータベースを選択します。インポートを開始するには、**ユーザーのインポートの開始** を選択します。詳細については、[ユーザーのインポート/エクスポート拡張](/docs/ja-jp/manage-users/user-migration/user-import-export-extension)を参照してください。
   * **Management API**：ユーザーをAuth0にインポートする[ジョブを作成](/docs/ja-jp/api/management/v2#!/Jobs/post_users_imports)します。詳細な手順については、[［一括ユーザーインポート］](/docs/ja-jp/manage-users/user-migration/bulk-user-imports)を参照してください。

## シナリオ2：OktaからAuth0へのユーザー移行

1. ユーザーのインポートを有効にする：[Auth0 Dashboard>Authentication>Database](https://manage.auth0.com/#/connections/database)に移動し、データベース接続を選択します。**［Settings（設定）］** で、**［Import Users to Auth0（Auth0 にユーザーをインポート）］** オプションを有効にします。
2. [ログインスクリプト](/docs/ja-jp/authenticate/database-connections/custom-db/templates/login)を作成する：**［Login（ログイン）］** スクリプトは、ユーザーがログインしようとしたときに、そのアカウントがAuth0データベースに見つからない場合に実行されます。メールとパスワードを`username`および`password`のパラメータとして渡して、Okta[プライマリ認証](https://developer.okta.com/docs/api/resources/authn.html#primary-authentication)エンドポイントを呼び出すスクリプトを作成する必要があります。認証が成功すると、Oktaは[埋め込みリソース](https://developer.okta.com/docs/api/resources/authn.html#embedded-resources)内の[ユーザーのプロファイル](https://developer.okta.com/docs/api/resources/authn.html#user-profile-object)を含む[認証トランザクションオブジェクト](https://developer.okta.com/docs/api/resources/authn.html#authentication-transaction-model)を返します。その後、ユーザーの情報を抽出し、コールバック関数でAuth0に渡すことができます。

   データベース接続の **［Custom Database（カスタムデータベース）］** ビューで、 **［Database Action Scripts（データベースアクションスクリプト）］** を見つけて、**Login（ログイン）］** を選択します。

   ```javascript lines theme={null}
   function login (email, password, callback) {
     // Replace {yourOktaDomain} with your own Okta domain
     var url = 'https:/{yourOktaDomain}/api/v1/authn';

     // Make the POST request to authenticate a user
     request({
       url: url,
       method: 'POST',
       headers: {
         'Content-Type': 'application/json',
         'Accept': 'application/json'
       },
       body: {
         username: email,
         password: password,
         options: {
           multiOptionalFactorEnroll: false,
           warnBeforePasswordExpired: false
         }
       },
       json: true
     }, function (error, response, body) {
       // Ensure we have a successful response
       if (response.statusCode !== 200) return callback();

       // Get the user from the response body
       var user = body._embedded.user;

       // Set the data we want to store in Auth0 and migrate the user
       return callback(null, {
           user_id : user.id,
           username: user.profile.login,
           email: user.profile.login,
           // We set the users email_verified to true as we assume if they were a valid
           // user in Okta, they have already verified their email
           // If this field is not set, the user will get an email asking them to verify
           // their account
           email_verified: true,
           name: user.profile.firstName + ' ' + user.profile.lastName
         });
     });
   }
   ```

   スクリプトが機能するかどうかをテストするには、スクリプトの上にある\*\*［Try（試す）］\*\*ボタンをクリックします。
3. [ユーザー取得](/docs/ja-jp/authenticate/database-connections/custom-db/templates/get-user)スクリプトを作成します：**［Get User（ユーザー取得）］** スクリプトは、ユーザーがパスワードをリセットしようとしたが、Auth0データベースにそのアカウントが見つからない場合に実行されます。ユーザーのメールアドレスを`login`パラメータとして渡して、Okta[ログインでユーザー取得](https://developer.okta.com/docs/api/resources/users.html#get-user-with-login)エンドポイントを呼び出すスクリプトを作成する必要があります。また、`Authorization`ヘッダーでこのエンドポイントに渡す必要がある[API トークンを作成する](https://developer.okta.com/docs/api/getting_started/getting_a_token.html)必要があります。成功した場合、Oktaはユーザーの情報を含む[ユーザーオブジェクト](https://developer.okta.com/docs/api/resources/users.html#user-model)を返します。ここでも、ユーザーの情報を抽出し、コールバック関数でAuth0に渡すことができます。

   データベース接続の **［Custom Database（カスタムデータベース）］** ビューで、**［Database Action Scripts（データベースアクションスクリプト）］** を見つけて、**［Get User（ユーザーの取得）］** を選択します。

   ```javascript lines theme={null}
   function getByEmail(email, callback) {
     // Replace {yourOktaDomain} with your own Okta domain
     var url = 'https://{yourOktaDomain}/api/v1/users/' + encodeURIComponent(email);

     // Make a GET request to find a user by email
     // Replace {yourOktaApiToken} with an Okta API Token 
     // (see https://developer.okta.com/docs/api/getting_started/getting_a_token.html) 
     request({
       url: url,
       method: 'GET',
       headers: {
         'Content-Type': 'application/json',
         'Accept': 'application/json',
         'Authorization': 'SSWS ' + '{yourOktaApiToken}'
       },
       json: true
     }, function (error, response, body) {
       // Ensure we have a successful response
       if (response.statusCode !== 200) return callback();

       // Set the data we want to store in Auth0 and migrate the user
       return callback(null, {
         user_id: body.id,
         username: body.profile.login,
         email: body.profile.login,
         email_verified: true,
         name: body.profile.firstName + ' ' + body.profile.lastName
       });
     });
   }
   ```

   スクリプトが機能するかどうかをテストするには、スクリプトの上にある **［Try（試す）］** ボタンをクリックします。
4. カスタムデータベース接続をテストします：**［Try connection（接続を試す）］** をクリックします。[Auth0 Lockウィジェット](/docs/ja-jp/libraries/lock)が表示されます。Oktaユーザーのメールアドレスとパスワードを入力し、**（Log In（ログイン）］** をクリックします。接続が機能していることを示すWebページと、ユーザーに関する情報が表示されます。
5. 新しくインポートされたユーザーを確認するには、[Auth0 Dashboard>User Management>Users](https://manage.auth0.com/#/users)へ移動します。

## シナリオ3：StormpathからAuth0にユーザーを移行する

1. ユーザーのインポートを有効にする：[Auth0 Dashboard>Authentication>Database](https://manage.auth0.com/#/connections/database)に移動し、データベース接続を選択します。**［Settings（設定）］** で、**［Import Users to Auth0（Auth0 にユーザーをインポート）］** オプションを有効にします。
2. 接続用のアプリケーションを有効にします：**［Applications（アプリケーション）］** ビューを選択します。データベース接続を使用するアプリケーションを有効にします。
3. [ログインスクリプト](/docs/ja-jp/authenticate/database-connections/custom-db/templates/login)を作成する：**［Login（ログイン）］** スクリプトは、ユーザーがログインしようとしたときに、そのアカウントがAuth0データベースに見つからない場合に実行されます。ユーザーを認証するためにStormpathのAPIを呼び出すスクリプトを作成し、`username`と`password`のパラメータとしてユーザーの資格情報を渡す必要があります。

   認証が成功すると、Stormpathからの応答にユーザーアカウントURLが含まれます。ユーザーの情報を取得するには、アカウントURLに対して2回目の要求を実行します。その後、ユーザーの情報を抽出し、コールバック関数でAuth0に渡すことができます。

   ```javascript lines theme={null}
   function login(username, password, callback) {
     // Replace the {yourStormpathClientId} attribute with your Stormpath ID
     var url = 'https://api.stormpath.com/v1/applications/{yourStormpathClientId}/loginAttempts';

     // Stormpath requires the user credentials be passed in as a base64 encoded message
     var message = username + ':' + password;
     var pass = new Buffer(message).toString('base64');

     // Here we are making the POST request to authenticate a user
     request({
       url: url,
       method: 'POST',
       auth: {
         // Your API Client ID
         user: '{yourStormpathClientId}',
         // Your API Client Secret
         password: '{yourStormpathClientSecret}'
       },
       headers: {
         'Content-Type': 'application/json'
       },
       json: {
         type: 'basic',
         // Passing in the base64 encoded credentials
         value: pass
       }
     }, function (error, response, body) {
       // If response is successful we'll continue
       if (response.statusCode !== 200) return callback();
       // A successful response will return a URL to get the user information
       var accountUrl = body.account.href;

       // We'll make a second request to get the user info. This time it will be a GET request
       request({
         url: accountUrl,
         method: 'GET',
         auth: {
           // Your API Client ID
           user: '{yourStormpathClientId}',
           // YOUR API Client Secret
           password: '{yourStormpathClientSecret}'
         }
       }, function (errorUserInfo, responseUserInfo, bodyUserInfo) {
         // If we get a successful response, we'll process it
         if (responseUserInfo.statusCode !== 200) return callback();

         var parsedBody = JSON.parse(bodyUserInfo);
         // To get the user identifier, we'll strip out the Stormpath API
         var id = parsedBody.href.replace('https://api.stormpath.com/v1/accounts/', '');

         // Finally, we'll set the data we want to store in Auth0 and migrate the user
         return callback(null, {
           user_id : id,
           username: parsedBody.username,
           email: parsedBody.email,
           // We set the users email_verified to true as we assume if they were a valid
           // user in Stormpath, they have already verified their email
           // If this field is not set, the user will get an email asking them to verify
           // their account
           email_verified: true,
           // Add any additional fields you would like to carry over from Stormpath
         });
       });
     });
   }
   ```

   スクリプトの上にある **［Try（試す）］** ボタンをクリックして、スクリプトが機能するかどうかをテストして確認します。
4. [ユーザー取得](/docs/ja-jp/authenticate/database-connections/custom-db/templates/get-user)スクリプトを作成します：**［Get User（ユーザー取得）］** スクリプトは、ユーザーがパスワードのリセットを試みたものの、Auth0データベース内にそのアカウントが見つからない場合に実行されます。Stormpath APIの`/accounts`エンドポイントを呼び出し、ユーザーのメールアドレスを`email`パラメータとして渡すスクリプトを作成する必要があります。成功した場合、Stormpathはユーザーの情報を返します。ユーザーの情報を抽出し、コールバック関数でAuth0に渡すことができます。

   ```javascript lines theme={null}
   function getByEmail(email, callback) {
     // Replace the {yourStormpathClientId} attribute with your Stormpath ID
     var url = 'https://api.stormpath.com/v1/applications/{yourStormpathClientId}/accounts';

     request({
       url: url,
       method: 'GET',
       auth: {
         // Your API Client ID
         user: '{yourStormpathClientId}',
         // YOUR API Client Secret
         password: '{yourStormpathClientSecret}'
       },
       qs: { q: email }
     }, function (error, response, body) {
       if (response.statusCode !== 200) return callback();

       var parsedBody = JSON.parse(body);
       var user = parsedBody.items[0];

       if (!user) return callback();

       var id = user.href.replace('https://api.stormpath.com/v1/accounts/', '');

       return callback(null, {
         user_id: id,
         username: user.username,
         email: user.email,
         email_verified: true,
         // Add any additional fields you would like to carry over from Stormpath
       });
     });
   }
   ```

   スクリプトの上にある **［Try（試す）］** ボタンをクリックして、スクリプトが機能するかどうかをテストして確認します。
5. カスタムデータベース接続をテストします：**［Try connection（接続を試す）］** をクリックします。[Auth0 Lockウィジェット](/docs/ja-jp/libraries/lock)が表示されます。Stormpathユーザーのメールアドレスとパスワードを入力し、**（Log In（ログイン）］** をクリックします。接続が機能していることを示すWebページと、ユーザーに関する情報が表示されます。
6. 新しくインポートされたユーザーを確認するには、[Dashboard>User Management>Users](https://manage.auth0.com/#/users)に移動します。
