mirror of https://github.com/tiangolo/fastapi.git
108 lines
4.0 KiB
Markdown
108 lines
4.0 KiB
Markdown
# 🇺🇸🔍 🔰 🔐
|
||
|
||
🙅 💼, 👆 💪 ⚙️ 🇺🇸🔍 🔰 🔐.
|
||
|
||
🇺🇸🔍 🔰 🔐, 🈸 ⌛ 🎚 👈 🔌 🆔 & 🔐.
|
||
|
||
🚥 ⚫️ 🚫 📨 ⚫️, ⚫️ 📨 🇺🇸🔍 4️⃣0️⃣1️⃣ "⛔" ❌.
|
||
|
||
& 📨 🎚 `WWW-Authenticate` ⏮️ 💲 `Basic`, & 📦 `realm` 🔢.
|
||
|
||
👈 💬 🖥 🎦 🛠️ 📋 🆔 & 🔐.
|
||
|
||
⤴️, 🕐❔ 👆 🆎 👈 🆔 & 🔐, 🖥 📨 👫 🎚 🔁.
|
||
|
||
## 🙅 🇺🇸🔍 🔰 🔐
|
||
|
||
* 🗄 `HTTPBasic` & `HTTPBasicCredentials`.
|
||
* ✍ "`security` ⚖" ⚙️ `HTTPBasic`.
|
||
* ⚙️ 👈 `security` ⏮️ 🔗 👆 *➡ 🛠️*.
|
||
* ⚫️ 📨 🎚 🆎 `HTTPBasicCredentials`:
|
||
* ⚫️ 🔌 `username` & `password` 📨.
|
||
|
||
{* ../../docs_src/security/tutorial006.py hl[2,6,10] *}
|
||
|
||
🕐❔ 👆 🔄 📂 📛 🥇 🕰 (⚖️ 🖊 "🛠️" 🔼 🩺) 🖥 🔜 💭 👆 👆 🆔 & 🔐:
|
||
|
||
<img src="/img/tutorial/security/image12.png">
|
||
|
||
## ✅ 🆔
|
||
|
||
📥 🌅 🏁 🖼.
|
||
|
||
⚙️ 🔗 ✅ 🚥 🆔 & 🔐 ☑.
|
||
|
||
👉, ⚙️ 🐍 🐩 🕹 <a href="https://docs.python.org/3/library/secrets.html" class="external-link" target="_blank">`secrets`</a> ✅ 🆔 & 🔐.
|
||
|
||
`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] *}
|
||
|
||
👉 🔜 🎏:
|
||
|
||
```Python
|
||
if not (credentials.username == "stanleyjobson") or not (credentials.password == "swordfish"):
|
||
# Return some error
|
||
...
|
||
```
|
||
|
||
✋️ ⚙️ `secrets.compare_digest()` ⚫️ 🔜 🔐 🛡 🆎 👊 🤙 "🕰 👊".
|
||
|
||
### ⏲ 👊
|
||
|
||
✋️ ⚫️❔ "⏲ 👊"❓
|
||
|
||
➡️ 🌈 👊 🔄 💭 🆔 & 🔐.
|
||
|
||
& 👫 📨 📨 ⏮️ 🆔 `johndoe` & 🔐 `love123`.
|
||
|
||
⤴️ 🐍 📟 👆 🈸 🔜 🌓 🕳 💖:
|
||
|
||
```Python
|
||
if "johndoe" == "stanleyjobson" and "love123" == "swordfish":
|
||
...
|
||
```
|
||
|
||
✋️ ▶️️ 🙍 🐍 🔬 🥇 `j` `johndoe` 🥇 `s` `stanleyjobson`, ⚫️ 🔜 📨 `False`, ↩️ ⚫️ ⏪ 💭 👈 📚 2️⃣ 🎻 🚫 🎏, 💭 👈 "📤 🙅♂ 💪 🗑 🌅 📊 ⚖ 🎂 🔤". & 👆 🈸 🔜 💬 "❌ 👩💻 ⚖️ 🔐".
|
||
|
||
✋️ ⤴️ 👊 🔄 ⏮️ 🆔 `stanleyjobsox` & 🔐 `love123`.
|
||
|
||
& 👆 🈸 📟 🔨 🕳 💖:
|
||
|
||
```Python
|
||
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] *}
|