מדיניות Basicאימות

אתם צופים במסמכי התיעוד של Apigee Edge.
אפשר לעיין במסמכי התיעוד של Apigee X.
מידע

מה

מאפשרת להשתמש באימות בסיסי קל משקל כדי לאבטח את החיבורים האחרונים. המדיניות מקבלת שם משתמש וסיסמה, מקודדת אותם בפורמט Base64 וכותבת את הערך שמתקבל למשתנה. הערך שמתקבל הוא בפורמט Basic Base64EncodedString. בדרך כלל כותבים את הערך הזה בכותרת HTTP, כמו הכותרת Authorization.

המדיניות מאפשרת גם לפענח פרטי כניסה שמאוחסנים במחרוזת מקודדת של Base64 לשם משתמש ולסיסמה.

סרטון: בסרטון הזה מוסבר איך לקודד שם משתמש וסיסמה ב-Base64 באמצעות מדיניות האימות הבסיסי.

סרטון: בסרטון הזה מוסבר איך לפענח שם משתמש וסיסמה בקידוד Base64 באמצעות מדיניות אימות בסיסי.

דוגמאות

קידוד בשיחות יוצאות

<BasicAuthentication name="ApplyBasicAuthHeader">
   <DisplayName>ApplyBasicAuthHeader</DisplayName>
   <Operation>Encode</Operation>
   <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
   <User ref="BasicAuth.credentials.username" />
   <Password ref="BasicAuth.credentials.password" />
   <AssignTo createNew="false">request.header.Authorization</AssignTo>
</BasicAuthentication>

בהגדרת המדיניות לדוגמה שלמעלה, שם המשתמש והסיסמה שצריך לקודד נגזרים מהמשתנים שצוינו במאפייני ref ברכיבים <User> ו-<Password>. צריך להגדיר את המשתנים לפני שהמדיניות הזו מופעלת. בדרך כלל, המשתנים מאוכלסים בערכים שנקראים ממפת מפתח/ערך. ראו מדיניות בנושא פעולות של Key Value Map.

ההגדרה הזו גורמת לכותרת ה-HTTP בשם Authorization, כפי שמצוין ברכיב <AssignTo>, להתווסף להודעת הבקשה היוצאת שנשלחת לשרת העורפי:

Authorization: Basic TXlVc2VybmFtZTpNeVBhc3N3b3Jk

הערכים של <User> ו-<Password> משורשרים עם נקודתיים לפני קידוד Base64.

נניח שיש לכם מיפוי של מפתח/ערך עם הרשומה הבאה:

{
  "encrypted" : true,
  "entry" : [ {
    "name" : "username",
    "value" : "MyUsername"
  }, {
    "name" : "password",
    "value" : "MyPassword"
  } ],
  "name" : "BasicAuthCredentials"
}
      

כדי לחלץ את הערכים של הרכיבים <User> ו-<Password> מחנות המפתחות/ערכים ולאכלס אותם במשתנים credentials.username ו-credentials.password, צריך לצרף את כללי המדיניות הבאים של KeyValueMapOperations לפני כלל המדיניות BasicAuthentication:

<KeyValueMapOperations name="getCredentials" mapIdentifier="BasicAuthCredentials">
  <Scope>apiproxy</Scope>
  <Get assignTo="credentials.username" index='1'>
    <Key>
      <Parameter>username</Parameter>
    </Key>
  </Get>
  <Get assignTo="credentials.password" index='1'>
    <Key>
      <Parameter>password</Parameter>
    </Key>
  </Get>
</KeyValueMapOperations>
      

פענוח של שיחות נכנסות

<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>

בדוגמה הזו למדיניות, המדיניות מפענחת את שם המשתמש והסיסמה מתוך Authorization כותרת ה-HTTP, כפי שמצוין ברכיב <Source>. המחרוזת בקידוד Base64 צריכה להיות בפורמט Basic Base64EncodedString.

המדיניות כותבת את שם המשתמש המפוענח למשתנה request.header.username ואת הסיסמה המפוענחת למשתנה request.header.password.


מידע על המדיניות בנושא אימות בסיסי

למדיניות יש שני מצבי פעולה:

  • קידוד: קידוד Base64 מקודד שם משתמש וסיסמה שמאוחסנים במשתנים
  • Decode: מפענח את שם המשתמש והסיסמה ממחרוזת בקידוד Base64

בדרך כלל, שם המשתמש והסיסמה מאוחסנים בחנות של זוגות מפתח/ערך, ואז נקראים ממנה בזמן הריצה. פרטים על שימוש בחנות של זוגות מפתח/ערך מופיעים במאמר מדיניות בנושא פעולות של מיפוי מפתח/ערך.

הפניה לרכיב

הפניה לרכיב מתארת את הרכיבים והמאפיינים של מדיניות BasicAuthentication.

<BasicAuthentication async="false" continueOnError="false" enabled="true" name="Basic-Authentication-1">
   <DisplayName>Basic Authentication 1</DisplayName>
   <Operation>Encode</Operation>
   <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
   <User ref="request.queryparam.username" />
   <Password ref="request.queryparam.password" />
   <AssignTo createNew="false">request.header.Authorization</AssignTo>
   <Source>request.header.Authorization</Source> 
</BasicAuthentication>

מאפיינים של <BasicAuthentication>

<BasicAuthentication async="false" continueOnError="false" enabled="true" name="Basic-Authentication-1">

The following table describes attributes that are common to all policy parent elements:

Attribute Description Default Presence
name

The internal name of the policy. The value of the name attribute can contain letters, numbers, spaces, hyphens, underscores, and periods. This value cannot exceed 255 characters.

Optionally, use the <DisplayName> element to label the policy in the management UI proxy editor with a different, natural-language name.

N/A Required
continueOnError

Set to false to return an error when a policy fails. This is expected behavior for most policies.

Set to true to have flow execution continue even after a policy fails.

false Optional
enabled

Set to true to enforce the policy.

Set to false to turn off the policy. The policy will not be enforced even if it remains attached to a flow.

true Optional
async

This attribute is deprecated.

false Deprecated

<DisplayName> element

Use in addition to the name attribute to label the policy in the management UI proxy editor with a different, natural-language name.

<DisplayName>Policy Display Name</DisplayName>
Default

N/A

If you omit this element, the value of the policy's name attribute is used.

Presence Optional
Type String

אלמנט <Operation>

המדיניות קובעת אם פרטי הכניסה מקודדים או מפוענחים ב-Base64.

<Operation>Encode</Operation>
ברירת מחדל: לא רלוונטי
נוכחות: חובה
סוג:

מחרוזת.

הערכים האפשריים כוללים:

  • קידוד
  • Decode

אלמנט <IgnoreUnresolvedVariables>

אם המדיניות מוגדרת כ-true, לא תופיע שגיאה אם לא ניתן לפתור משתנה. כשמשתמשים בהגדרה הזו בהקשר של מדיניות BasicAuthentication, בדרך כלל היא מוגדרת ל-false כי בדרך כלל עדיף להציג שגיאה אם לא ניתן למצוא שם משתמש או סיסמה במשתנים שצוינו.

<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
ברירת מחדל: true
נוכחות: אופציונלי
סוג:

בוליאני

אלמנט <User>

  • לקידוד, משתמשים ברכיב <User> כדי לציין את המשתנה שמכיל את שם המשתמש. הערכים של שם המשתמש והסיסמה מחוברים באמצעות נקודתיים לפני קידוד Base64.
  • לפענוח, מציינים את המשתנה שבו ייכתב שם המשתמש המפוענח.
<User ref="request.queryparam.username" /> 
ברירת מחדל: לא רלוונטי
נוכחות: חובה
סוג:

לא רלוונטי

מאפיינים

מאפיין תיאור ברירת מחדל נוכחות
ref

המשתנה שממנו המדיניות קוראת באופן דינמי את שם המשתמש (קידוד) או כותבת את שם המשתמש (פענוח).

לא רלוונטי חובה

אלמנט <Password>

  • לקידוד, משתמשים באלמנט <Password> כדי לציין את המשתנה שמכיל את הסיסמה.
  • לפענוח, מציינים את המשתנה שבו הסיסמה המפוענחת נכתבת.
<Password ref="request.queryparam.password" />
ברירת מחדל: לא רלוונטי
נוכחות: חובה
סוג:

לא רלוונטי

מאפיינים

מאפיין תיאור ברירת מחדל נוכחות
ref

המשתנה שממנו המדיניות קוראת באופן דינמי את הסיסמה (encode) או כותבת את הסיסמה (decode).

לא רלוונטי חובה

אלמנט <AssignTo>

עבור פעולת Encode, המדיניות הזו מציינת את משתנה היעד שיוגדר עם הערך המקודד שנוצר על ידי המדיניות הזו.

בדוגמה הבאה מצוין שהמדיניות צריכה להגדיר את הכותרת Authorization של ההודעה לערך שנוצר:

<AssignTo createNew="false">request.header.Authorization</AssignTo>
ברירת מחדל: לא רלוונטי
נוכחות: נדרש לפעולה Encode.
סוג:

מחרוזת

מאפיינים

מאפיין תיאור ברירת מחדל נוכחות
createNew ההגדרה קובעת אם המדיניות תשכתב את המשתנה אם הוא כבר מוגדר.

אם הערך הוא false, ההקצאה למשתנה מתרחשת רק אם המשתנה לא מוגדר (null).

אם הערך הוא true, ההקצאה למשתנה תמיד מתרחשת.

בדרך כלל מגדירים את המאפיין הזה לערך false (ברירת המחדל).

false אופציונלי

אלמנט <Source>

לפענוח, המשתנה שמכיל את המחרוזת בקידוד Base64, בפורמט Basic Base64EncodedString. לדוגמה, מציינים request.header.Authorization, שמתאים לכותרת Authorization.

<Source>request.header.Authorization</Source>
ברירת מחדל: לא רלוונטי
נוכחות: נדרש לפעולת הפענוח.
סוג:

לא רלוונטי

משתני תהליך

משתנה הזרימה הבא מוגדר כשהמדיניות נכשלת:

  • BasicAuthentication.{policy_name}.failed (עם הערך true)

הפניה לשגיאה

This section describes the fault codes and error messages that are returned and fault variables that are set by Edge when this policy triggers an error. This information is important to know if you are developing fault rules to handle errors. 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 Cause Fix
steps.basicauthentication.InvalidBasicAuthenticationSource 500 On a decode when the incoming Base64 encoded string does not contain a valid value or the header is malformed (e.g., does not start with "Basic").
steps.basicauthentication.UnresolvedVariable 500 The required source variables for the decode or encode are not present. This error can only occur if IgnoreUnresolvedVariables is false.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name Occurs when Fix
UserNameRequired The <User> element must be present for the named operation.
PasswordRequired The <Password> element must be present for the named operation.
AssignToRequired The <AssignTo> element must be present for the named operation.
SourceRequired The <Source> element must be present for the named operation.

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"
BasicAuthentication.policy_name.failed policy_name is the user-specified name of the policy that threw the fault. BasicAuthentication.BA-Authenticate.failed = true

Example error response

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

Example fault rule

<FaultRule name="Basic Authentication Faults">
    <Step>
        <Name>AM-UnresolvedVariable</Name>
        <Condition>(fault.name Matches "UnresolvedVariable") </Condition>
    </Step>
    <Step>
        <Name>AM-AuthFailedResponse</Name>
        <Condition>(fault.name = "InvalidBasicAuthenticationSource")</Condition>
    </Step>
    <Condition>(BasicAuthentication.BA-Authentication.failed = true) </Condition>
</FaultRule>

סכימות

נושאים קשורים

מדיניות בנושא פעולות של Key Value Map