您正在查看 Apigee Edge 說明文件。
查看 Apigee X 說明文件。 資訊
計算及驗證雜湊式訊息驗證碼 (HMAC)。有時 HMAC 會使用密碼雜湊函式 (例如 SHA-1、SHA-224、SHA-256、SHA-384、SHA-512 或 MD-5) 和密鑰產生簽名或訊息驗證碼,並在該郵件上產生簽名或訊息驗證碼。這裡的「訊息」是指任何位元組串流。訊息傳送者也可以傳送 HMAC 給接收者,且接收者可以使用 HMAC 來驗證訊息。
如要進一步瞭解 HMAC,請參閱「HMAC: Keyed-Hashing for Message Authentication (rfc2104)」。
範例
產生 HMAC
<HMAC name='HMAC-1'> <Algorithm>SHA256</Algorithm> <SecretKey ref='private.secretkey'/> <IgnoreUnresolvedVariables>true|false</IgnoreUnresolvedVariables> <!-- optional --> <!-- The "message" can include fixed and multiple variable parts, including newlines and static functions. Whitespace is significant. --> <Message>Fixed Part {a_variable} {timeFormatUTCMs(timeFormatString1,system.timestamp)} {nonce} </Message> <!-- default encoding is base64 --> <Output encoding='base16'>name_of_variable</Output> </HMAC>
驗證 HMAC
<HMAC name='HMAC-1'> <Algorithm>SHA256</Algorithm> <SecretKey ref='private.secretkey'/> <IgnoreUnresolvedVariables>true|false</IgnoreUnresolvedVariables> <!-- optional --> <!-- The "message" can include fixed and multiple variable parts, including newlines and static functions. Whitespace is significant. --> <Message>Fixed Part {a_variable} {timeFormatUTCMs(timeFormatString1,system.timestamp)} {nonce} </Message> <!-- VerificationValue is optional. Include it to perform an HMAC check. --> <VerificationValue encoding='base16' ref='expected_hmac_value'/> <!-- default encoding is base64 --> <Output encoding='base16'>name_of_variable</Output> </HMAC>
簽章和驗證的計算作業完全按照相同的程序。HMAC 政策會計算 HMAC,並視需要依據預期值驗證計算出的簽名。選用的 VerificationValue 元素 (如有) 會指示政策根據已知或指定值檢查計算的值。
HMAC 的元素參照
政策參考資料說明 HMAC 政策的元素和屬性。
套用至頂層元素的屬性
<HMAC name="HMAC" continueOnError="false" enabled="true" async="false">
以下是所有政策父項元素的通用屬性。
屬性 | 說明 | 預設 | 外觀狀態 |
---|---|---|---|
名稱 |
政策的內部名稱。名稱中使用的字元僅限 A-Z0-9._\-$ % 。不過,Apigee UI 會強制執行其他限制,例如自動移除非英數字元。您也可以選擇使用 |
不適用 | 需要 |
continueOnError |
如果設為 false ,即可在政策失敗時傳回錯誤。大部分政策都是預期中的行為。
設為 |
false | 選用 |
已啟用 |
如要強制執行政策,請設為 true 。
設為 |
true | 選用 |
async | 此屬性已淘汰。 | false | 已淘汰 |
<Algorithm>
<Algorithm>algorithm-name</Algorithm>
指定要用於計算 HMAC 的雜湊演算法。
預設 | 不適用 |
外觀狀態 | 需要 |
類型 | 字串 |
有效值 | SHA-1 、SHA-224 、SHA-256 、SHA-384 、SHA-512 和 MD-5
這項政策設定可接受演算法名稱,且不會區分大小寫,而且是否接受字母和數字之間的連字號。例如, |
<DisplayName>
<DisplayName>Policy Display Name</DisplayName>
除了名稱屬性外,您也可以使用其他自然語言名稱,在 Apigee UI Proxy 編輯器中為政策加上標籤。
預設 | 如果省略這個元素,則會使用政策名稱屬性的值。 |
外觀狀態 | 選用 |
類型 | 字串 |
<Message>
<Message>message_template_here</Message> or <Message ref='variable_here'/>
指定要簽署的訊息酬載。此元素的輸入內容支援訊息範本 (變數替換),以便在執行階段納入其他項目,例如時間戳記、 Nonce、標頭清單或其他資訊。範例如下:
<Message>Fixed Part {a_variable} {timeFormatUTCMs(timeFormatString1,system.timestamp)} {nonce} </Message>
訊息範本可以含有固定和可變的部分,包括換行符號和靜態函式。空白字元至關重要。
預設 | 不適用 |
外觀狀態 | 需要 |
類型 | 字串 |
有效值 | 任何字串都對文字值有效。如果您提供 ref 屬性,優先順序將高於文字值。這項政策會將文字值或參照的變數視為訊息範本。 |
<輸出內容>
<Output encoding='encoding_name'>variable_name</Output>
指定政策應以計算出的 HMAC 值設定的變數名稱。 以及指定用於輸出的編碼。
預設 |
預設的輸出變數為 |
外觀狀態 | 選用設定。如果這個元素不存在,這項政策會設定資料流變數 hmac.POLICYNAME.output ,並採用 Base64 編碼值。 |
類型 | 字串 |
有效值 | 針對編碼, 值不區分大小寫;
|
<SecretKey>
<SecretKey encoding='encoding_name' ref='private.secretkey'/>
指定用於計算 HMAC 的密鑰。金鑰會從參照的變數取得,並根據特定編碼進行解碼。
預設 |
參照的變數沒有預設值, 根據預設,在沒有 |
外觀狀態 | 需要 |
類型 | 字串 |
有效值 | 對 使用編碼屬性可讓您指定索引鍵,該索引鍵包含 UTF-8 可列印字元範圍以外的位元組。舉例來說,假設政策設定包含以下內容: <SecretKey encoding='hex' ref='private.encodedsecretkey'/>
假設
在這種情況下,金鑰位元組將解碼為:[53 65 63 72 65 74 31 32 33] (每個以十六進位表示的位元組)。再舉一個例子,如果 |
<VerificationValue>
<VerificationValue encoding='encoding_name' ref='variable_name'/> or <VerificationValue encoding='encoding_name'>string_value</VerificationValue>
(選用) 指定驗證值,以及用來為驗證值編碼的編碼演算法。政策會使用這個演算法將值解碼。
預設 | 沒有預設的驗證值。如果元素存在,但沒有 encoding 屬性,這項政策會使用 base64 的預設編碼 |
外觀狀態 | 選用 |
類型 | 字串 |
有效值 |
編碼屬性的有效值為:
|
<IgnoreUnresolvedVariables>
<IgnoreUnresolvedVariables>true|false</IgnoreUnresolvedVariables>
如要讓政策在政策中指定的任何參照變數無法解析時擲回錯誤,請設為 false
。設為 true
可將所有無法解析的變數視為空字串 (空值)。
IgnoreUnresolvedVariables 布林值只會影響訊息範本參照的變數。雖然 SecretKey
和 VerificationValue
可以參照變數,但這兩者都需要可解析,因此 ignore
設定不適用於這些變數。
預設 | false |
外觀狀態 | 選用 |
類型 | 布林值 |
有效值 | true 或 false |
流程變數
這項政策可在執行期間設定這些變數。
變數 | 說明 | 範例 |
---|---|---|
hmac.policy_name.message |
這項政策會以有效訊息設定這個變數,也就是評估 Message 元素中指定的訊息範本後的結果。 |
hmac.HMAC-Policy.message = "Hello, World" |
hmac.policy_name.output |
當 Output 元素未指定變數名稱時,取得 HMAC 計算的結果。 |
hmac.HMAC-Policy.output = /yyRjydfP+fBHTwXFgc5AZhLAg2kwCri+e35girrGw4= |
hmac.policy_name.outputencoding |
取得輸出編碼的名稱。 | hmac.HMAC-Policy.outputencoding = base64 |
錯誤參考資料
本節說明政策觸發錯誤時,系統傳回的錯誤代碼和錯誤訊息,以及 Apigee 所設定的錯誤變數。 如果您正在開發錯誤規則來處理錯誤,請務必瞭解這項資訊。詳情請參閱「政策錯誤須知」和「處理錯誤」。
執行階段錯誤
執行政策時,可能會發生這些錯誤。
錯誤代碼 | HTTP 狀態 | 發生時機 |
---|---|---|
steps.hmac.UnresolvedVariable |
401 | 如果 HMAC 政策中指定的變數符合下列任一條件,就會發生這個錯誤:
|
steps.hmac.HmacVerificationFailed |
401 | HMAC 驗證失敗;提供的驗證值與計算值不符。 |
steps.hmac.HmacCalculationFailed |
401 | 這項政策無法計算 HMAC。 |
steps.hmac.EmptySecretKey |
401 | 密鑰變數的值為空白。 |
steps.hmac.EmptyVerificationValue |
401 | 保留驗證值的變數空白。 |
部署錯誤
若您部署包含這項政策的 Proxy,就可能會發生這些錯誤。
錯誤名稱 | HTTP 狀態 | 發生時機 |
---|---|---|
steps.hmac.MissingConfigurationElement |
401 | 缺少必要元素或屬性時,就會發生這個錯誤。 |
steps.hmac.InvalidValueForElement |
401 | 如果演算法元素中指定的值不是下列任一值,就會發生這個錯誤:SHA-1 、SHA-224 、SHA-256 、SHA-512 或 MD-5 。 |
steps.hmac.InvalidSecretInConfig |
401 | 如果已明確為 SecretKey 提供文字值,就會發生這個錯誤。 |
steps.hmac.InvalidVariableName |
401 | 如果 SecretKey 變數不含 private 前置字元 (private. ),就會發生這個錯誤。 |
錯誤變數
這些變數是在執行階段錯誤發生時設定。詳情請參閱「政策錯誤須知」。
Variables | 地點 | 範例 |
---|---|---|
fault.name="fault_name" |
fault_name 是錯誤的名稱,如上方的執行階段錯誤表格所示。錯誤名稱是錯誤代碼的最後一個部分。 | fault.name Matches "UnresolvedVariable" |
hmac.policy_name.failed |
這項政策會在失敗時設定這個變數。 | hmac.HMAC-Policy.failed = true |
錯誤回應範例
如要處理錯誤,最佳做法是納入錯誤回應的 errorcode
部分。請勿依賴 faultstring
中的文字,因為文字可能會有所變動。
故障規則示例
<FaultRules> <FaultRule name="HMAC Policy Errors"> <Step> <Name>AM-Unauthorized</Name> <Condition>(fault.name Matches "HmacVerificationFailed")</Condition> </Step> <Condition>hmac.HMAC-1.failed = true</Condition> </FaultRule> </FaultRules>