4.0 KiB
🇺🇸🔍 🔰 🔐
🙅 💼, 👆 💪 ⚙️ 🇺🇸🔍 🔰 🔐.
🇺🇸🔍 🔰 🔐, 🈸 ⌛ 🎚 👈 🔌 🆔 & 🔐.
🚥 ⚫️ 🚫 📨 ⚫️, ⚫️ 📨 🇺🇸🔍 4️⃣0️⃣1️⃣ "⛔" ❌.
& 📨 🎚 WWW-Authenticate ⏮️ 💲 Basic, & 📦 realm 🔢.
👈 💬 🖥 🎦 🛠️ 📋 🆔 & 🔐.
⤴️, 🕐❔ 👆 🆎 👈 🆔 & 🔐, 🖥 📨 👫 🎚 🔁.
🙅 🇺🇸🔍 🔰 🔐
- 🗄
HTTPBasic&HTTPBasicCredentials. - ✍ "
security⚖" ⚙️HTTPBasic. - ⚙️ 👈
security⏮️ 🔗 👆 ➡ 🛠️. - ⚫️ 📨 🎚 🆎
HTTPBasicCredentials:- ⚫️ 🔌
username&password📨.
- ⚫️ 🔌
{* ../../docs_src/security/tutorial006.py hl[2,6,10] *}
🕐❔ 👆 🔄 📂 📛 🥇 🕰 (⚖️ 🖊 "🛠️" 🔼 🩺) 🖥 🔜 💭 👆 👆 🆔 & 🔐:
✅ 🆔
📥 🌅 🏁 🖼.
⚙️ 🔗 ✅ 🚥 🆔 & 🔐 ☑.
👉, ⚙️ 🐍 🐩 🕹 secrets ✅ 🆔 & 🔐.
secrets.compare_digest() 💪 ✊ bytes ⚖️ str 👈 🕴 🔌 🔠 🦹 (🕐 🇪🇸), 👉 ⛓ ⚫️ 🚫🔜 👷 ⏮️ 🦹 💖 á, Sebastián.
🍵 👈, 👥 🥇 🗜 username & password bytes 🔢 👫 ⏮️ 🔠-8️⃣.
⤴️ 👥 💪 ⚙️ secrets.compare_digest() 🚚 👈 credentials.username "stanleyjobson", & 👈 credentials.password "swordfish".
{* ../../docs_src/security/tutorial007.py hl[1,11:21] *}
👉 🔜 🎏:
if not (credentials.username == "stanleyjobson") or not (credentials.password == "swordfish"):
# Return some error
...
✋️ ⚙️ secrets.compare_digest() ⚫️ 🔜 🔐 🛡 🆎 👊 🤙 "🕰 👊".
⏲ 👊
✋️ ⚫️❔ "⏲ 👊"❓
➡️ 🌈 👊 🔄 💭 🆔 & 🔐.
& 👫 📨 📨 ⏮️ 🆔 johndoe & 🔐 love123.
⤴️ 🐍 📟 👆 🈸 🔜 🌓 🕳 💖:
if "johndoe" == "stanleyjobson" and "love123" == "swordfish":
...
✋️ ▶️️ 🙍 🐍 🔬 🥇 j johndoe 🥇 s stanleyjobson, ⚫️ 🔜 📨 False, ↩️ ⚫️ ⏪ 💭 👈 📚 2️⃣ 🎻 🚫 🎏, 💭 👈 "📤 🙅♂ 💪 🗑 🌅 📊 ⚖ 🎂 🔤". & 👆 🈸 🔜 💬 "❌ 👩💻 ⚖️ 🔐".
✋️ ⤴️ 👊 🔄 ⏮️ 🆔 stanleyjobsox & 🔐 love123.
& 👆 🈸 📟 🔨 🕳 💖:
if "stanleyjobsox" == "stanleyjobson" and "love123" == "swordfish":
...
🐍 🔜 ✔️ 🔬 🎂 stanleyjobso 👯♂️ stanleyjobsox & stanleyjobson ⏭ 🤔 👈 👯♂️ 🎻 🚫 🎏. ⚫️ 🔜 ✊ ➕ ⏲ 📨 🔙 "❌ 👩💻 ⚖️ 🔐".
🕰 ❔ ℹ 👊
👈 ☝, 👀 👈 💽 ✊ ⏲ 📏 📨 "❌ 👩💻 ⚖️ 🔐" 📨, 👊 🔜 💭 👈 👫 🤚 🕳 ▶️️, ▶️ 🔤 ▶️️.
& ⤴️ 👫 💪 🔄 🔄 🤔 👈 ⚫️ 🎲 🕳 🌖 🎏 stanleyjobsox 🌘 johndoe.
"🕴" 👊
↗️, 👊 🔜 🚫 🔄 🌐 👉 ✋, 👫 🔜 ✍ 📋 ⚫️, 🎲 ⏮️ 💯 ⚖️ 💯 💯 📍 🥈. & 🔜 🤚 1️⃣ ➕ ☑ 🔤 🕰.
✋️ 🔨 👈, ⏲ ⚖️ 📆 👊 🔜 ✔️ 💭 ☑ 🆔 & 🔐, ⏮️ "ℹ" 👆 🈸, ⚙️ 🕰 ✊ ❔.
🔧 ⚫️ ⏮️ secrets.compare_digest()
✋️ 👆 📟 👥 🤙 ⚙️ secrets.compare_digest().
📏, ⚫️ 🔜 ✊ 🎏 🕰 🔬 stanleyjobsox stanleyjobson 🌘 ⚫️ ✊ 🔬 johndoe stanleyjobson. & 🎏 🔐.
👈 🌌, ⚙️ secrets.compare_digest() 👆 🈸 📟, ⚫️ 🔜 🔒 🛡 👉 🎂 ↔ 💂♂ 👊.
📨 ❌
⏮️ 🔍 👈 🎓 ❌, 📨 HTTPException ⏮️ 👔 📟 4️⃣0️⃣1️⃣ (🎏 📨 🕐❔ 🙅♂ 🎓 🚚) & 🚮 🎚 WWW-Authenticate ⚒ 🖥 🎦 💳 📋 🔄:
{* ../../docs_src/security/tutorial007.py hl[23:27] *}