การยืนยันตัวตนและการอนุญาต
สร้างกลยุทธ์การยืนยันตัวตนและการอนุญาตผู้ใช้เพื่อรักษาความปลอดภัยให้จุดปลายทาง API ใกล้ผู้ใช้
การยืนยันตัวตนและการอนุญาต เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Edge Computing with Cloudflare Workers & Deno และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Securing Edge APIs
Welcome! In this lesson, we'll dive into Authentication and Authorization for your edge API endpoints.
These are crucial concepts for protecting your applications and ensuring only the right users can access specific resources.
AuthN vs. AuthZ
Let's clarify two key terms:
- Authentication (AuthN): Verifies who you are. Think of it like showing your ID to prove your identity.
- Authorization (AuthZ): Determines what you can do. This is like your ID granting you access to certain areas, but not others.
They often work together, but are distinct processes.
Common Edge Auth Methods
At the edge, we need lightweight and efficient authentication methods. Common approaches include:
- API Keys: Simple secrets used to identify an application or user.
- JSON Web Tokens (JWTs): Self-contained, digitally signed tokens for secure information exchange.
- OAuth/OIDC: More complex protocols for delegated authorization, often relying on JWTs.
We'll focus on API Keys and JWTs.
Implementing API Key Auth
API Keys are straightforward. A client sends a secret key, usually in a header, and your Worker validates it. Let's see a simple example:
export default {
async fetch(request, env, ctx) {
const apiKey = request.headers.get('X-API-Key');
// In a real app, fetch 'MY_API_KEY' from env.MY_API_KEY
if (apiKey !== 'super-secret-key-123') {
return new Response('Unauthorized: Invalid API Key', { status: 401 });
}
return new Response('Access Granted!', { status: 200 });
},
};API Key Auth Explained
In the example, the Worker checks for the X-API-Key header. If it doesn't match our expected secret, access is denied with a 401 Unauthorized status.
For production, always store your API keys securely, typically in Cloudflare Worker environment variables, not directly in your code.
JSON Web Tokens (JWTs)
JWTs are powerful for edge applications because they are self-contained. Once issued by an identity provider, a JWT can be verified by your Worker without needing to contact a central server every time.
A JWT has three parts: Header, Payload, and Signature, separated by dots.
Verifying JWTs on the Edge
Your Worker will receive a JWT, usually in the Authorization: Bearer header. The critical step is to verify its signature to ensure it hasn't been tampered with.
While full cryptographic verification requires a library, here's where you'd typically extract and prepare for verification:
export default {
async fetch(request, env, ctx) {
const authHeader = request.headers.get('Authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return new Response('Unauthorized: No token', { status: 401 });
}
const token = authHeader.split(' ')[1];
// In a real app, you'd use a crypto library and a secret key
// to verify the token's signature, expiry, and claims.
// e.g., using 'jose' library with `await jwtVerify(token, secretKey)`
if (token) {
// For this example, we'll assume a successful (mock) verification
return new Response('Access Granted with JWT!', { status: 200 });
}
return new Response('Unauthorized: Invalid JWT', { status: 401 });
},
};Role-Based Authorization (RBAC)
Once a user is authenticated (e.g., via a verified JWT), you need to decide what actions they are authorized to perform. This is where Role-Based Access Control (RBAC) comes in.
RBAC assigns permissions to roles (e.g., 'admin', 'user', 'guest'), and users are assigned one or more roles. Your Worker then checks the user's role before allowing an action.
Implementing RBAC in Worker
You can embed user roles or permissions directly into the JWT payload as 'claims'. After verifying the JWT, your Worker can read these claims and enforce authorization rules.
export default {
async fetch(request, env, ctx) {
// Assume JWT is already verified and we have the user's role
// In a real app, this 'userRole' would come from the decoded JWT payload.
const userRole = 'editor'; // Example role from a verified token
if (request.url.includes('/admin') && userRole !== 'admin') {
return new Response('Forbidden: Admins only!', { status: 403 });
}
if (request.method === 'POST' && userRole === 'guest') {
return new Response('Forbidden: Guests cannot create!', { status: 403 });
}
return new Response(`Welcome, ${userRole}! Request processed.`, { status: 200 });
},
};Test Your Understanding
Which of the following statements correctly describe the difference between Authentication and Authorization?
Recap: AuthN & AuthZ
Great job! You've learned the fundamentals of securing your edge APIs.
- Authentication confirms identity.
- Authorization controls access.
- API Keys offer simple authentication.
- JWTs provide secure, self-contained authentication and can carry authorization claims.
- RBAC (Role-Based Access Control) helps define permissions based on user roles.
These strategies are vital for building robust and secure edge applications.
คำถามที่พบบ่อย
บทเรียน “การยืนยันตัวตนและการอนุญาต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การยืนยันตัวตนและการอนุญาต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การยืนยันตัวตนและการอนุญาต”
สร้างกลยุทธ์การยืนยันตัวตนและการอนุญาตผู้ใช้เพื่อรักษาความปลอดภัยให้จุดปลายทาง API ใกล้ผู้ใช้ คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การยืนยันตัวตนและการอนุญาต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม
ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การยืนยันตัวตนและการอนุญาต
- การจำกัดอัตราและการป้องกัน DDoS
- การจัดการข้อมูลลับอย่างปลอดภัย
- การทำความสะอาดข้อมูลนำเข้าและการป้องกันอินเจกชัน