1. 認証

認証

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

Sitecore Content Hub は、セキュリティのために OAuth 2.0 認証フレームワークを使用します。

Content Hub で OAuth を設定する

プロジェクトへの接続を設定するには、Sitecore Content Hub で M.OAuthClient エンティティを作成します。次のプロパティを指定する必要があります。

  • Name
  • ClientId
  • ClientSecret
  • RedirectUrl
注意

RedirectUrl プロパティは必須ですが、ここで説明するケースには該当しません。任意のダミー値を指定できます。

次の手順は、Sitecore Content Hub で OAuth を作成するプロセスについて説明したものです。

  1. [管理] 画面で、検索ボックスを使って「OAuth クライアント」を検索し、[OAuth クライアント] タイルをクリックします。
                <img src="https://mss-p-003-delivery.stylelabs.cloud/api/public/content/e93ec6b0ca3547cba229bb28202b320b?v=689a9779" alt="">
              </li>
              <li>SDK の M.OAuthClient エンティティがリストにない場合は、[+ OAuth クライアント] をクリックして新しいエンティティを追加します。</li>
              <li>必要な M.OAuthClient エンティティ値を入力し、変更を保存します。</li>
            </ol>
          </div>
        </div>
      </section>
      <section dir="ltr" class="section accordion accordion;" data-origin-id="" data-publication-date="05/27/22" id="password-authentication">
        <div class="accordion-icon"></div>
        <div class="panel panel-default">
          <div class="panel-heading active">
            <div class="titlepage">
              <div>
                <div class="title">
                  <h2 class="title" style="clear: both" id="パスワード認証">パスワード認証</h2>
                </div>
              </div>
            </div>
          </div>
          <div class="panel-body collapse in" id="password-authentication_body" aria-expanded="true" style="">
            <p><code>ClientId</code> と <code>ClientSecret</code> に加えて、プロジェクトから Sitecore Content Hub で認証するために、ユーザー名とパスワードを指定する必要があります。以下に、Sitecore Content Hub 認証の例を示します。</p>
            <pre class="shiki shiki-themes github-light github-dark not-prose" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code data-language="shell"><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">import</span><span class="space"> </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">OAuthPasswordGrant</span><span class="space"> </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">from</span><span class="space"> </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"@sitecore/sc-contenthub-webclient-sdk/dist/authentication/oauth-password-grant"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">;</span></span>
    

    import { ContentHubClient } from "@sitecore/sc-contenthub-webclient-sdk/dist/clients/content-hub-client"; // Your Sitecore Content Hub endpoint to connect to const endpoint = "https://your.m.endpoint.com"; // Enter your credentials here const oauth = new OAuthPasswordGrant( "client_id", "client_secret", "username", "password" ); // Create the JavaScript SDK client const client = new ContentHubClient(endpoint, oauth); // Authentication // returns true when authentication succeeds. // returns false or throws an error when authentication failed. await client.internalClient.authenticateAsync();

    認証が成功すると、await client.internalClient.authenticateAsync()TRUE を返します。失敗した場合は、FALSE を返すか、エラーをスローします。

    次のように、関数の一部として await 式を記述することもできます。

    async function main() {
    await ...
    } 
    main();

    await を使用する場合は、必ず「Promise」コンストラクターの宣言を含めるか、「ES2015」を --lib オプションに含めてください。

    // configuration using tsconfig.json
    {
    "compilerOptions": {
    "lib": [
    "es2015"
    ]
    }
    }

    また、次のコードを使用することもできます。

    tsc main.ts --lib ES2015

リフレッシュ トークン認証

username と password による認証の代わりに、リフレッシュ トークン認証を使用できます。「リフレッシュ トークン認証」では、IWebMClient.RefreshTokenReceived イベントをサブスクライブすることが重要です。OAuthPasswordGrant を作成するのではなく、この部分を OAuthRefreshTokenGrant に変更します。

import OAuthRefreshTokenGrant from "@sitecore/sc-contenthub-webclient-sdk/dist/authentication/oauth-refreshtoken-grant";

// Enter your credentials here
const oauth = new OAuthRefreshTokenGrant(
"client_id",
"client_secret",
"refresh_token",
);

この記事を改善するための提案がある場合は、 お知らせください!