คุณกำลังดูเอกสารประกอบ Apigee Edge
ไปที่
เอกสารประกอบเกี่ยวกับ Apigee X. ข้อมูล

ประมวลผลและยืนยันรหัสการตรวจสอบสิทธิ์ข้อความแบบแฮช (HMAC) บางครั้ง HMAC จะใช้แฮชแบบเข้ารหัสที่เรียกว่ารหัสการตรวจสอบสิทธิ์ข้อความคีย์หรือแฮชที่มีคีย์ เช่น SHA-1, SHA-224, SHA-256, SHA-384, SHA-512 หรือ MD-5 พร้อมกับรหัสลับ เพื่อสร้างลายเซ็นหรือรหัสการตรวจสอบสิทธิ์ข้อความในข้อความนั้น คำว่า "ข้อความ" ที่นี่ หมายถึงสตรีมของไบต์ใดก็ได้ ผู้ส่งข้อความจะส่ง HMAC ไปยังผู้รับได้ด้วย และผู้รับสามารถใช้ HMAC เพื่อตรวจสอบสิทธิ์ข้อความ
หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับ HMAC ดู HMAC: การแฮชด้วยคีย์ สำหรับ 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._\-$ % แต่ UI ของ Apigee จะบังคับใช้เพิ่มเติม
เช่น การนำอักขระที่ไม่ใช่ตัวอักษรและตัวเลขคละกันออกโดยอัตโนมัติ
(ไม่บังคับ) ใช้องค์ประกอบ |
ไม่มี | ต้องระบุ |
continueOnError |
ตั้งค่าเป็น false เพื่อแสดงผลข้อผิดพลาดเมื่อนโยบายล้มเหลว เป็นเรื่องปกติ
พฤติกรรมสำหรับนโยบายส่วนใหญ่
ตั้งค่าเป็น |
เท็จ | ไม่บังคับ |
เปิดใช้อยู่ |
ตั้งค่าเป็น true เพื่อบังคับใช้นโยบาย
ตั้งค่าเป็น |
จริง | ไม่บังคับ |
ไม่พร้อมกัน | แอตทริบิวต์นี้เลิกใช้งานแล้ว | เท็จ | เลิกใช้ |
<Algorithm>
<Algorithm>algorithm-name</Algorithm>
ระบุอัลกอริทึมแฮชเพื่อคํานวณ HMAC
ค่าเริ่มต้น | ไม่มี |
การตรวจหาบุคคล | ต้องระบุ |
ประเภท | สตริง |
ค่าที่ถูกต้อง | SHA-1 SHA-224 SHA-256 SHA-384
SHA-512 และ MD-5
การกำหนดค่านโยบายจะยอมรับชื่ออัลกอริทึมโดยไม่มีการแยกตัวพิมพ์เล็กและใหญ่ และ
มีหรือไม่มี ขีดระหว่างตัวอักษรและตัวเลข เช่น |
<DisplayName>
<DisplayName>Policy Display Name</DisplayName>
ใช้เพิ่มเติมจากแอตทริบิวต์ชื่อเพื่อติดป้ายกำกับนโยบายในตัวแก้ไขพร็อกซี UI ของ Apigee ด้วยชื่อที่เป็นภาษาธรรมชาติต่างกัน
ค่าเริ่มต้น | หากคุณไม่ใส่องค์ประกอบนี้ ระบบจะใช้ค่าของแอตทริบิวต์ชื่อนโยบาย |
การตรวจหาบุคคล | ไม่บังคับ |
ประเภท | สตริง |
<Message>
<Message>message_template_here</Message> or <Message ref='variable_here'/>
ระบุเพย์โหลดของข้อความที่จะเซ็น อินพุตขององค์ประกอบนี้สนับสนุน เทมเพลตข้อความ (การแทนที่ตัวแปร) เพื่ออนุญาตให้มีการรวมรายการเพิ่มเติมขณะรันไทม์ เช่น การประทับเวลา, nonces, รายการของ หรือข้อมูลอื่นๆ เช่น
<Message>Fixed Part {a_variable} {timeFormatUTCMs(timeFormatString1,system.timestamp)} {nonce} </Message>
เทมเพลตข้อความอาจมีส่วนต่างๆ แบบคงที่และตัวแปร รวมถึงบรรทัดใหม่และฟังก์ชันแบบคงที่ ช่องว่างเป็นสิ่งสำคัญ
ค่าเริ่มต้น | ไม่มี |
การตรวจหาบุคคล | ต้องระบุ |
ประเภท | สตริง |
ค่าที่ถูกต้อง | สตริงใดก็ได้ที่ถูกต้องสำหรับค่าข้อความ หากระบุแอตทริบิวต์ ref
จะมีลำดับความสำคัญเหนือกว่าค่าข้อความ นโยบายจะประเมินทั้งข้อความ
หรือตัวแปรที่อ้างอิงเป็นเทมเพลตข้อความ |
<Output>
<Output encoding='encoding_name'>variable_name</Output>
ระบุชื่อตัวแปรที่นโยบายควรตั้งค่าด้วยค่า HMAC ที่คำนวณแล้ว รวมถึงระบุการเข้ารหัสที่จะใช้สำหรับเอาต์พุตด้วย
ค่าเริ่มต้น |
ตัวแปรเอาต์พุตเริ่มต้นคือ ค่าเริ่มต้นสำหรับแอตทริบิวต์ |
การตรวจหาบุคคล | ไม่บังคับ หากไม่มีองค์ประกอบนี้อยู่ นโยบายจะตั้งค่าตัวแปรโฟลว์
hmac.POLICYNAME.output ซึ่งมีค่าที่เข้ารหัสฐาน 64 |
ประเภท | สตริง |
ค่าที่ถูกต้อง | สำหรับการเข้ารหัส ค่าดังกล่าวไม่คำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ค่าข้อความขององค์ประกอบ |
<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)
บูลีน ignoreUnresolvedVariable จะส่งผลต่อตัวแปรที่อ้างอิงโดยค่า
เทมเพลตข้อความ แม้ว่า SecretKey
และ VerificationValue
จะอ้างอิงตัวแปรได้ ซึ่งทั้ง 2 อย่าง
ส่วนขยายทั้งหมดต้องแก้ไขได้ ดังนั้นการตั้งค่า ignore
จะไม่มีผลกับรายการเหล่านั้น
ค่าเริ่มต้น | เท็จ |
การตรวจหาบุคคล | ไม่บังคับ |
ประเภท | บูลีน |
ค่าที่ถูกต้อง | จริงหรือเท็จ |
ตัวแปรโฟลว์
นโยบายนี้ตั้งค่าตัวแปรเหล่านี้ได้ในระหว่างการดำเนินการ
ตัวแปร | คำอธิบาย | ตัวอย่าง |
---|---|---|
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>