臨時SSH憑證

除了BearerPoP存取權杖外,MSAL 瀏覽器還支援取得臨時 SSH 憑證,讓使用者能透過 SSH 協定遠端存取 Azure Linux 虛擬機器。 本文件概述了在取得這些 SSH 憑證時,建議使用 MSAL acquireToken API 的模式。

取得SSH證書

先決條件

MSAL 負責取得、快取及更新(或稱「刷新」)所請求的 SSH 憑證。 以下操作由用戶端應用程式負責:

  • 產生供 SSH 憑證簽署的 RSA 金鑰對
  • 安全儲存該金鑰對
  • 從上述金鑰對中擷取公開金鑰,並將其唯一的金鑰 ID 提供給所使用的 MSAL acquireToken API
  • 啟用客戶端應用程式 AzureAD 應用程式註冊所需的資源、範圍與權限
  • 利用 SSH 憑證對相關資源發出授權請求

SSH 憑證申請

為了取得 SSH 憑證,需在認證請求中加入以下參數:

參數 價值 Description
authenticationScheme ssh-cert (AuthenticationScheme.SSH) 請求的認證方案會告訴 MSAL 要請求哪種憑證。 選項有 Bearer、pop 和 ssh-cert。 MSAL 提供 AuthenticationScheme 列舉,其中列出下列選項。
sshJwk 一個以 JSON Web Key 格式呈現的串聯 RSA 公鑰 SSH JWK 是應用程式產生的 RSA 金鑰公鑰元件的序列化(串連化)版本。
sshKid 一個唯一識別 RSA 鍵的 sshJwk 字串。 SSH 金鑰 ID 用於唯一識別並匹配 MSAL 代表應用程式取得的 SSH 憑證。

MSAL 不會使用該 RSA 金鑰的私密金鑰部分。 客戶端應用程式有責任在必要時安全地儲存與檢索私鑰。

取得代幣重定向請求範例

// Configure SSH Key Data
const sshKeyData = {
    key: PUBLIC_KEY_JWK, // Replace with your SSH Public Key in JSON Web Key object format
    keyId: "PUBLIC_KEY_ID" // Replace with the SSH Public Key's unique ID
};

// Configure SSH Certificate Request
const sshCertificateRequest = {
    scopes: ["SAMPLE_SSH_SCOPE"], // Replace with your resource's scope
    authenticationScheme: msal.AuthenticationScheme.SSH,
    sshJwk: JSON.stringify(sshKeyData.key),
    sshKid: sshKeyData.keyId
}

注意:設定 authenticationScheme to AuthenticationScheme.SSH 會使 sshJwk 和 sshKid 屬性變成強制,若其中任何一個被省略,請求將以 a ClientConfigurationError 失敗。

一旦請求已完成設定,且 SSH 已設為 authenticationScheme,即可將其傳送至 acquireToken MSAL v2 API。

// The SSH Certificate will be the string value contained in the accessToken property of the AuthenticationResult object
const { accessToken } = await myMSALObj.acquireTokenRedirect(sshCertificateRequest);

取得令牌靜默請求範例

靜默取得 SSH 憑證需要對憑證請求設定進行與互動 acquireToken 式 API 相同的變更:

// Configure SSH Key Data
const sshKeyData = {
    key: PUBLIC_KEY_JWK, // Replace with your SSH Public Key in JSON Web Key object format
    keyId: "PUBLIC_KEY_ID" // Replace with the SSH Public Key's unique ID
};

// Configure SSH Certificate Request
const silentSshCertificateRequest = {
    scopes: ["SAMPLE_SSH_SCOPE"], // Replace with your resource's scope
    authenticationScheme: msal.AuthenticationScheme.SSH,
    sshJwk: JSON.stringify(sshKeyData.key),
    sshKid: sshKeyData.keyId
}

// Try to acquire certificate silently
const { accessToken } = await myMSALObj.acquireTokenSilent(silentSshTokenRequest).catch(async (error) => {
        console.log("Silent token acquisition failed.");
        if (error instanceof msal.InteractionRequiredAuthError) {
            // Fallback to interaction if silent call fails
            console.log("Acquiring SSH Certificate using redirect");
            myMSALObj.acquireTokenRedirect(silentSshCertificateRequest);
        } else {
            console.error(error);
        }
    });

程式碼範例