排解基本驗證政策執行階段錯誤的問題

查看 Apigee Edge 說明文件。
前往 Apigee X說明文件
資訊

UnresolvedVariable

錯誤代碼

steps.basicauthentication.UnresolvedVariable

錯誤回應主體

{
    "fault": {
        "faultstring": "Unresolved variable : [variable_name]",
        "detail": {
            "errorcode": "steps.basicauthentication.UnresolvedVariable"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "Unresolved variable : request.header.Authorization",
        "detail": {
            "errorcode": "steps.basicauthentication.UnresolvedVariable"
        }
    }
}

原因

如果變數包含 BasicAuthentication 政策<Source> 元素中指定的 Base64 編碼字串,就會發生這個錯誤:

  • 範圍外 (不適用於執行政策的特定流程)
  • 無法解析 (未定義)

舉例來說,如果基本驗證政策在 <Source> 元素中指定了 request.header.Authorization 的變數,但 Authorization 標頭並未隨 API 要求一併傳遞,就會發生這個錯誤。

診斷

  1. 找出基本驗證政策中 <Source> 元素使用的變數。您可以在錯誤回應的 faultstring 元素中找到這項資訊。舉例來說,在下列 faultstring, 中,<Source> 元素使用的變數為 request.header.Authorization

    "faultstring": "Unresolved variable : request.header.Authorization"
    
  2. 檢查發生錯誤時,特定 API Proxy 中的所有 BasicAuthentication 政策。可能會有一或多項「基本驗證」政策。找出 <Source> 元素中指定的變數與錯誤字串中識別的變數名稱的特定基本驗證政策或政策 (上述步驟 1)。

    舉例來說,下列政策會將 <Source> 元素設為名為 request.header.Authorization 的變數,此變數與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BasicAuthentication name="DecodeBaseAuthHeaders">
      <DisplayName>Decode Basic Authentication Header</DisplayName>
      <Operation>Decode</Operation>
      <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
      <User ref="request.header.username"/>
      <Password ref="request.header.password"/>
      <Source>request.header.Authorization</Source>
    </BasicAuthentication>
    
  3. 判定是否已定義變數,並在執行基本驗證政策的流程中加以定義。

  4. 如果變數為以下任一變數:

    1. 範圍外 (不適用於執行政策的特定流程)
    2. 無法解析 (未定義)

    這就是發生錯誤的原因。

    在上方顯示的基本驗證政策範例中,未將 request.header.Authorization 變數當做要求的一部分傳遞。也就是說,在沒有 Authorization 標頭的情況下提出 API 要求,如下所示:

    curl -v "http://org-env.apigee.net/basicauth"

    由於基本驗證政策中使用的變數無法使用,因此您會收到錯誤代碼:

    steps.basicauthentication.UnresolvedVariable
    

解析度

確保基本驗證政策中使用的變數已定義或傳遞為輸入內容,且可在執行政策的流程中使用。

如要修正上述基本驗證政策範例的問題,請按照以下方式提出 API 要求:

curl -v "http://org-env.apigee.net/basicauth" -H "Authorization: Basic YWthc2g6MTIz"

InvalidBasicAuthenticationSource

錯誤代碼

steps.basicauthentication.InvalidBasicAuthenticationSource

錯誤回應主體

{
    "fault": {
        "faultstring": "Source variable : [variable_name] for basic authentication decode policy is not valid",
        "detail": {
            "errorcode": "steps.basicauthentication.InvalidBasicAuthenticationSource"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "Source variable : request.header.Authorization for basic authentication decode policy is not valid",
        "detail": {
            "errorcode": "steps.basicauthentication.InvalidBasicAuthenticationSource"
        }
    }
}

可能原因

發生這項錯誤的可能原因如下:

原因 說明
來源變數無效 BasicAuthentication Policy 的輸入來源變數不是有效的 Base64 編碼字串。
標題格式錯誤 包含傳送至 BasicAuthentication 政策的 Base64 編碼字串格式錯誤。

原因:來源變數無效

如果 BasicAuthentication 政策<Source> 元素指定含有 Base64 編碼字串,則該變數的變數不含有效值,就會發生這個錯誤。

舉例來說,如果基本驗證政策的 <Source> 元素中指定的變數缺少有效的 Base64 編碼字串,就會發生這個錯誤。

診斷

  1. 找出基本驗證政策中 <Source> 元素使用的變數。您可以在錯誤回應的 faultstring 元素中找到這項資訊。舉例來說,在下列 faultstring, 中,<Source> 元素使用的變數為 request.header.Authorization

    "faultstring": "Source variable : request.header.Authorization for basic authentication decode policy is not valid"
    
  2. 檢查發生錯誤時,特定 API Proxy 中的所有 BasicAuthentication 政策。可能會有一或多項「基本驗證」政策。找出 <Source> 元素中指定的變數與錯誤字串中識別的變數名稱的特定基本驗證政策 (上述步驟 1)。

    舉例來說,下列政策會將 <Source> 元素設為名為 request.header.Authorization 的變數,此變數與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BasicAuthentication name="DecodeBaseAuthHeaders">
        <DisplayName>Decode Basic Authentication Header</DisplayName>
        <Operation>Decode</Operation>
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <User ref="request.header.username"/>
        <Password ref="request.header.password"/>
        <Source>request.header.Authorization</Source>
    </BasicAuthentication>
    
  3. 如果 <Source> 元素中指定的變數中儲存的值不是有效的 Base64 編碼字串,就是導致錯誤的原因。

    在上述基本驗證政策範例中,用戶端以標頭形式傳送的 request.header.Authorization 變數無效,如下所示:

    curl -v "http://org-env.apigee.net/basicauth" -H "Authorization: Basic 23435"

    由於變數 request.header.Authorization 包含無效的 Base64 編碼字串「23435"」,因此您會收到錯誤代碼:

    steps.basicauthentication.InvalidBasicAuthenticationSource

解析度

確認基本驗證政策中針對 <Source> 元素指定的變數包含有效的 Base64 編碼字串。

如要修正上述基本驗證政策範例的問題,請在 Authorization 標頭中傳送有效的 Base64 編碼字串以提出 API 要求,如下所示:

curl -v "http://org-env.apigee.net/basicauth" -H "Authorization: Basic YWthc2g6MTIz"

原因:標題格式錯誤/無效

如果傳遞至 BasicAuthentication 政策的標頭格式錯誤或無效,就會發生這個錯誤。

舉例來說,如果基本驗證政策在 <Source> 元素中指定了 request.header.Authorization 的變數,且做為 API 要求中傳遞的標頭格式不正確/無效,就會發生這個錯誤。

診斷

  1. 找出基本驗證政策中 <Source> 元素使用的變數。您可以在錯誤回應的 faultstring 元素中找到這項資訊。舉例來說,在下列錯誤中,<Source> 元素使用的變數為 request.header.Authorization

    "faultstring": "Source variable : request.header.Authorization for basic authentication decode policy is not valid"
    
  2. 檢查發生錯誤時,特定 API Proxy 中的所有 BasicAuthentication 政策。可能會有一或多項「基本驗證」政策。找出 <Source> 元素中指定的變數與錯誤字串中識別的變數名稱的特定基本驗證政策 (上述步驟 1)。

    舉例來說,下列政策會將 <Source> 元素設為名為 request.header.Authorization 的變數,此變數與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BasicAuthentication name="DecodeBaseAuthHeaders">
        <DisplayName>Decode Basic Authentication Header</DisplayName>
        <Operation>Decode</Operation>
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <User ref="request.header.username"/>
        <Password ref="request.header.password"/>
        <Source>request.header.Authorization</Source>
    </BasicAuthentication>
    
  3. 如果變數是標頭,並且格式錯誤或無效 (例如,沒有驗證類型,或者驗證類型不是「基本」),就表示發生錯誤。

    Authorization 標頭的格式如下:

    Authorization: <Authentication type> <credentials>
    

    將格式錯誤的標頭傳遞至基本驗證政策的方式有兩種:

    示例 1:標頭中沒有驗證類型:

    curl -v "http://org-env.apigee.net/basicauth" -H "Authorization: YWthc2g6MTIz"

    在上述範例中,Authorization 標頭沒有「驗證」類型。如果將此標頭傳遞至上述「基本驗證」政策,您會收到錯誤代碼:

    steps.basicauthentication.InvalidBasicAuthenticationSource
    

    示例 2:驗證類型並非「基本」類型:

    curl -v "http://org-env.apigee.net/basicauth" -H "Authorization: OAuth YWthc2g6MTIz"

    在上述範例中,Authorization 標頭並不包含「Basic」類型的驗證。如果傳遞至上述基本驗證政策,您會收到錯誤代碼:

    steps.basicauthentication.InvalidBasicAuthenticationSource
    

    同樣地,如果在 Authorization 標頭中使用「驗證類型」(例如 Bearer、摘要驗證等),也可能會發生這個錯誤。

解析度

請確定將「基本驗證」政策的輸入內容傳遞到「驗證」類型,且類型為「基本」。

如要修正上述「基本驗證」政策範例的問題,請傳遞包含「Basic」的有效 Base64 編碼字串以提出 API 要求Authorization 標頭中的驗證類型,如下所示:

curl -v "http://org-env.apigee.net/basicauth" -H "Authorization: Basic YWthc2g6MTIz"