Testing

The loadExternalTokens() API

MSAL Browser バージョン 2.17.0 以降では、 loadExternalTokens() API が追加されました。これにより、ID、アクセス、および更新トークンを MSAL キャッシュに読み込むことができます。これにより、 acquireTokenSilent()を使用してフェッチできます。

注: これは、ブラウザー環境でのテストのみを目的とした高度な機能です。 アプリケーションのキャッシュにトークンを読み込むと、アプリが中断する可能性があります。 さらに、 loadExternalTokens() API を単体テストと統合テストで使用することをお勧めします。 E2E テストについては、代わりに TestingSample を参照してください。

loadExternalTokens() API は、MSAL キャッシュへのカスタム 読み込みトークンをアプリに容易にするパブリック API です。

await loadExternalTokens(
    config,
    silentRequest,
    serverResponse,
    loadTokenOptions,
);

loadExternalTokens() は、型 SilentRequestの要求、 ExternalTokenResponse型の応答、および型 LoadTokenOptionsのオプションを受け取ります。

@azure/msal-browserからインポートできる、それぞれの型定義を参照してください。

トークンの読み込み

キャッシュには ID、アクセス、および更新トークンの任意の組み合わせを指定できますが、少なくとも、 loadExternalTokens API では、トークンの関連付けとキャッシュを適切に識別するために、次のいずれかの入力パラメーターセットが必要です。

  • SilentRequestを含む オブジェクト(OR)
  • 権限を持つSilentRequestオブジェクトと、clientInfoを持つLoadTokenOptionsオブジェクト、または
  • 権限を持つ SilentRequest オブジェクトと、その権限を持つサーバー応答オブジェクト client_info
  • 権限を持つ SilentRequest オブジェクトと、その権限を持つサーバー応答オブジェクト id_token

次の例は、トークンを個別に読み込む方法を示していますが、1 つの要求に 1 つ、2 つ、または 3 つすべてを指定できます。

ID トークンの読み込み

上記のパラメーターに加えて、ID トークンを読み込むには次の情報を提供します。

  1. id_token フィールドを含むサーバー応答

また、上記の情報に基づいて、アカウントもキャッシュに設定されます。

以下のコード例を参照してください。

const config: Configuration = {
    auth: { clientId: "your-client-id" },
};

const silentRequest: SilentRequest = {
    account: {
        homeAccountId: "your-home-account-id",
        environment: "login.microsoftonline.com",
        tenantId: "your-tenant-id",
        username: "test@contoso.com",
        localAccountId: "your-local-account-id",
    },
};

const serverResponse: ExternalTokenResponse = {
    id_token: "id-token-here",
};

const loadTokenOptions: LoadTokenOptions = {};

const pca = new PublicClientApplication(config);
await loadExternalTokens(
    config,
    silentRequest,
    serverResponse,
    loadTokenOptions
);

// OR

const config: Configuration = {
    auth: { clientId: "your-client-id" },
};

const silentRequest: SilentRequest = {
    scopes: [],
    authority: "https://login.microsoftonline.com/your-tenant-id",
};

const serverResponse: ExternalTokenResponse = {
    id_token: "id-token-here",
};

const loadTokenOptions: LoadTokenOptions = {
    clientInfo: "client-info-here",
};

const pca = new PublicClientApplication(config);
await loadExternalTokens(
    config,
    silentRequest,
    serverResponse,
    loadTokenOptions
);

// OR

const config: Configuration = {
    auth: { clientId: "your-client-id" },
};

const silentRequest: SilentRequest = {
    scopes: [],
    authority: "https://login.microsoftonline.com/your-tenant-id",
};

const serverResponse: ExternalTokenResponse = {
    id_token: "id-token-here",
    client_info: "client-info-here",
};

const loadTokenOptions: LoadTokenOptions = {};

const pca = new PublicClientApplication(config);
await loadExternalTokens(
    config,
    silentRequest,
    serverResponse,
    loadTokenOptions
);

アクセス トークンの読み込み

上記のパラメーターに加えて、アクセス トークンを読み込むには次の情報を提供します。

  1. access_tokenexpires_intoken_type、およびscopeを含むサーバー応答

以下のコード例を参照してください。

const config: Configuration = {
    auth: { clientId: "your-client-id" },
};

const silentRequest: SilentRequest = {
    scopes: ["User.Read", "email"],
    account: {
        homeAccountId: "your-home-account-id",
        environment: "login.microsoftonline.com",
        tenantId: "your-tenant-id",
        username: "test@contoso.com",
        localAccountId: "your-local-account-id",
    },
};

const serverResponse: ExternalTokenResponse = {
    token_type: AuthenticationScheme.BEARER, // "Bearer"
    scope: "User.Read email",
    expires_in: 3599,
    access_token: "access-token-here",
};

const loadTokenOptions: LoadTokenOptions = {
    extendedExpiresOn: 6599,
};

const pca = new PublicClientApplication(config);
await loadExternalTokens(
    config,
    silentRequest,
    serverResponse,
    loadTokenOptions
);

更新トークンの読み込み

上記のパラメーターに加えて、更新トークンを読み込むには次の情報を提供します。

  1. refresh_tokenを含むサーバー応答(必要に応じて)refresh_token_expires_in

以下のコード例を参照してください。

const config: Configuration = {
    auth: { clientId: "your-client-id" },
};

const silentRequest: SilentRequest = {
    scopes: [],
    account: {
        homeAccountId: "your-home-account-id",
        environment: "login.microsoftonline.com",
        tenantId: "your-tenant-id",
        username: "test@contoso.com",
        localAccountId: "your-local-account-id",
    },
};

const serverResponse: ExternalTokenResponse = {
    refresh_token: "refresh-token-here",
    refresh_token_expires_in: "86399",
};

const loadTokenOptions: LoadTokenOptions = {};

const pca = new PublicClientApplication(config);

await loadExternalTokens(
    config,
    silentRequest,
    serverResponse,
    loadTokenOptions
);