คุณกำลังดูเอกสารประกอบของ Apigee Edge
ไปที่
เอกสารประกอบของ Apigee X info
คำนวณและยืนยัน Hash-based Message Authentication Code (HMAC) บางครั้ง เรียกว่า Keyed Message Authentication Code หรือ Keyed hash โดย HMAC จะใช้ฟังก์ชันการแฮชที่เข้ารหัสลับ เช่น SHA-1, SHA-224, SHA-256, SHA-384, SHA-512 หรือ MD-5 กับ "ข้อความ" พร้อมกับคีย์ลับ เพื่อสร้างลายเซ็นหรือรหัสการตรวจสอบสิทธิ์ข้อความในข้อความนั้น คำว่า "ข้อความ" ในที่นี้ หมายถึงสตรีมไบต์ใดก็ได้ ผู้ส่งข้อความยังสามารถส่ง HMAC ไปยังผู้รับได้ด้วย และผู้รับสามารถใช้ HMAC เพื่อตรวจสอบสิทธิ์ข้อความได้
ดูข้อมูลเพิ่มเติมเกี่ยวกับ HMAC, ดูที่ HMAC: Keyed-Hashing สำหรับ 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">
แอตทริบิวต์ต่อไปนี้เป็นแอตทริบิวต์ทั่วไปขององค์ประกอบระดับบนสุดของนโยบายทั้งหมด
| แอตทริบิวต์ | คำอธิบาย | ค่าเริ่มต้น | การแสดงผล |
|---|---|---|---|
| name |
ชื่อภายในของนโยบาย อักขระที่คุณใช้ในชื่อจะจำกัดไว้ที่:
A-Z0-9._\-$ % อย่างไรก็ตาม UI ของ Apigee จะบังคับใช้ข้อจำกัดเพิ่มเติม
เช่น การนำอักขระที่ไม่ใช่ตัวอักษรและตัวเลขออกโดยอัตโนมัติ
คุณสามารถใช้องค์ประกอบ |
ไม่มี | ต้องระบุ |
| continueOnError |
ตั้งค่าเป็น false เพื่อแสดงข้อผิดพลาดเมื่อนโยบายล้มเหลว ซึ่งเป็นลักษณะการทำงานที่คาดไว้สำหรับนโยบายส่วนใหญ่
ตั้งค่าเป็น |
false | ไม่บังคับ |
| enabled |
ตั้งค่าเป็น 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>
ใช้เพิ่มเติมจากแอตทริบิวต์ name เพื่อติดป้ายกำกับนโยบายในเครื่องมือแก้ไขพร็อกซี UI ของ Apigee ด้วยชื่อภาษาธรรมชาติอื่น
| ค่าเริ่มต้น | หากคุณละเว้นองค์ประกอบนี้ ระบบจะใช้ค่าของแอตทริบิวต์ name ของนโยบาย |
| การแสดงผล | ไม่บังคับ |
| ประเภท | สตริง |
<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>
<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 เพื่อถือว่าตัวแปรที่ไม่สามารถแก้ไขได้เป็นสตริงว่าง
(ค่า Null)
บูลีน IgnoreUnresolvedVariables จะมีผลกับตัวแปรที่
เทมเพลตข้อความอ้างอิงเท่านั้น แม้ว่า SecretKey และ VerificationValue จะอ้างอิงตัวแปรได้ แต่ทั้ง
2 อย่างนี้ต้องแก้ไขได้ ดังนั้นการตั้งค่า ignore จึงไม่มีผลกับตัวแปรดังกล่าว
| ค่าเริ่มต้น | False |
| การแสดงผล | ไม่บังคับ |
| ประเภท | บูลีน |
| ค่าที่ถูกต้อง | จริงหรือเท็จ |
ตัวแปรโฟลว์
นโยบายสามารถตั้งค่าตัวแปรเหล่านี้ระหว่างการดำเนินการ
| ตัวแปร | คำอธิบาย | ตัวอย่าง |
|---|---|---|
hmac.policy_name.message |
นโยบายจะตั้งค่าตัวแปรนี้ด้วยข้อความที่มีผล
ซึ่งเป็นผลลัพธ์ของการประเมินเทมเพลตข้อความที่ระบุไว้ใน Message
องค์ประกอบ |
hmac.HMAC-Policy.message = "Hello, World" |
hmac.policy_name.output |
รับผลลัพธ์ของการคำนวณ HMAC เมื่อองค์ประกอบ Output ไม่ได้ระบุชื่อตัวแปร |
hmac.HMAC-Policy.output = /yyRjydfP+fBHTwXFgc5AZhLAg2kwCri+e35girrGw4= |
hmac.policy_name.outputencoding |
รับชื่อการเข้ารหัสเอาต์พุต | hmac.HMAC-Policy.outputencoding = base64 |
Error reference
This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.
Runtime errors
These errors can occur when the policy executes.
| Fault code | HTTP status | Occurs when |
|---|---|---|
steps.hmac.UnresolvedVariable |
401 | This error occurs if a variable specified in the HMAC policy is either:
|
steps.hmac.HmacVerificationFailed |
401 | The HMAC verification failed; the verification value provided does not match the calculated value. |
steps.hmac.HmacCalculationFailed |
401 | The policy was unable to calculate the HMAC. |
steps.hmac.EmptySecretKey |
401 | The value of the secret key variable is empty. |
steps.hmac.EmptyVerificationValue |
401 | The variable holding the verification value is empty. |
Deployment errors
These errors can occur when you deploy a proxy containing this policy.
| Error name | HTTP status | Occurs when |
|---|---|---|
steps.hmac.MissingConfigurationElement |
401 | This error occurs when a required element or attribute is missing. |
steps.hmac.InvalidValueForElement |
401 | This error occurs if the value specified in the Algorithm element is not
one of the following values: SHA-1, SHA-224, SHA-256,
SHA-512, or MD-5. |
steps.hmac.InvalidSecretInConfig |
401 | This error occurs if there is a text value explicitly provided for SecretKey. |
steps.hmac.InvalidVariableName |
401 | This error occurs if the SecretKey variable does not contain the
private prefix (private.). |
Fault variables
These variables are set when a runtime error occurs. For more information, see What you need to know about policy errors.
| Variables | Where | Example |
|---|---|---|
fault.name="fault_name" |
fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. | fault.name Matches "UnresolvedVariable" |
hmac.policy_name.failed |
The policy sets this variable in the case of a failure. | hmac.HMAC-Policy.failed = true |
Example error response
For error handling, the best practice is to trap the errorcode part of the error
response. Do not rely on the text in the faultstring, because it could change.
Example fault rule
<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>