OAuth 2.0 และ OpenID Connect
ทำความเข้าใจและผสานรวมโปรโตคอลมาตรฐานอุตสาหกรรมสำหรับการอนุญาต (OAuth 2.0) และการยืนยันตัวตน (OpenID Connect) เข้ากับแอปพลิเคชันอย่างปลอดภัย
OAuth 2.0 และ OpenID Connect เป็นบทเรียน Secure Coding & OWASP Top 10 for Backend ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Secure Coding & OWASP Top 10 for Backend และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is OAuth 2.0?
Welcome! In this lesson, we'll dive into OAuth 2.0 and OpenID Connect, two crucial protocols for modern web security.
First, let's understand OAuth 2.0. It's an industry-standard protocol for authorization. Think of it as a way to grant an application limited access to a user's resources without giving away their password.
- Authorization: Granting permission to do something.
- Authentication: Verifying who someone is.
OAuth 2.0 is NOT for authentication by itself, but it's often confused!
Who's Who in OAuth 2.0
OAuth 2.0 involves four key roles working together:
- Resource Owner: The user who owns the protected data (e.g., their photos on Google).
- Client: The application requesting access to the user's resources (e.g., a photo printing app).
- Authorization Server: The server that issues access tokens to the client after the resource owner's consent (e.g., Google's auth server).
- Resource Server: The server hosting the protected resources, capable of accepting access tokens (e.g., Google Photos API).
Understanding these roles is key to grasping the flow.
The Authorization Code Flow
OAuth 2.0 defines several ways (called 'grant types') for a client to get an access token. The Authorization Code Flow is the most common and secure for web applications.
It's a multi-step process designed to keep sensitive credentials (like your password) away from the client application and directly between you and the Authorization Server.
Let's break down how this secure flow works step-by-step.
Auth Flow: Step 1 (Request)
It all starts when the Client application needs to access a user's protected resources (like their calendar). The client redirects the Resource Owner's browser to the Authorization Server.
This redirect includes:
- The client's ID (
client_id) - The type of access requested (
scope, e.g.,read:calendar) - Where to send the user back (
redirect_uri)
The user is now interacting directly with the Authorization Server.
Auth Flow: Step 2 (Consent & Code)
At the Authorization Server, the Resource Owner (you!) is prompted to log in (if not already) and then asked if they grant the Client application the requested permissions.
If permission is granted, the Authorization Server redirects the user's browser back to the Client's pre-registered redirect_uri. This redirect includes a temporary, single-use authorization code.
This code is short-lived and doesn't grant access by itself.
Auth Flow: Step 3 (Token Exchange)
Now, the Client application has the authorization code. It then makes a direct, server-to-server request to the Authorization Server's token endpoint.
In this request, the client exchanges the authorization code for:
- An Access Token
- Optionally, a Refresh Token
This direct communication ensures the client's secret (if it has one) and the tokens are never exposed in the browser.
Access & Refresh Tokens
After the exchange, the Client holds two important tokens:
- Access Token: This is the actual credential used to access the Resource Server. It's usually short-lived (minutes to hours) and contains 'scopes' defining what the client can do.
- Refresh Token: This is a long-lived credential used by the client to obtain a new access token when the current one expires, without needing the user to re-authenticate. It must be stored securely by the client!
The client uses the Access Token to make requests to the Resource Server.
Beyond Authorization: OpenID Connect
While OAuth 2.0 is great for authorization, it doesn't directly provide user authentication. That's where OpenID Connect (OIDC) comes in!
OIDC is an identity layer built on top of OAuth 2.0. It allows clients to verify the identity of the Resource Owner (the user) and get basic profile information.
Think of it as adding a 'who are you?' layer to the 'what can you do?' of OAuth.
ID Tokens and Claims
The core of OIDC is the ID Token. This is a JSON Web Token (JWT) that contains claims about the authenticated user.
Common claims include:
sub(subject): Unique identifier for the user.name: User's full name.email: User's email address.iss(issuer): The URL of the OIDC provider.aud(audience): The client ID for which the token is intended.
The client verifies the ID Token's signature to ensure its authenticity and integrity.
Quick Check: OAuth vs. OIDC
Let's quickly test your understanding of the core concepts we've covered.
Recap: OAuth & OIDC
Great job! You've learned the fundamentals of OAuth 2.0 and OpenID Connect.
- OAuth 2.0 is an authorization framework, allowing delegated access to resources.
- It involves a Resource Owner, Client, Authorization Server, and Resource Server.
- The Authorization Code Flow is the most secure for web apps.
- OpenID Connect builds on OAuth 2.0 to provide an identity layer for user authentication.
- ID Tokens (JWTs) carry user identity claims, while Access Tokens grant resource access.
Understanding these protocols is vital for building secure and modern applications!
เรียนรู้ Secure Coding & OWASP Top 10 for Backend ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “OAuth 2.0 และ OpenID Connect” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “OAuth 2.0 และ OpenID Connect” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Secure Coding & OWASP Top 10 for Backend ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “OAuth 2.0 และ OpenID Connect”
ทำความเข้าใจและผสานรวมโปรโตคอลมาตรฐานอุตสาหกรรมสำหรับการอนุญาต (OAuth 2.0) และการยืนยันตัวตน (OpenID Connect) เข้ากับแอปพลิเคชันอย่างปลอดภัย คุณปฏิบัติ Secure Coding & OWASP Top 10 for Backend ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Secure Coding & OWASP Top 10 for Backend หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Secure Coding & OWASP Top 10 for Backend บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “OAuth 2.0 และ OpenID Connect” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Secure Coding & OWASP Top 10 for Backend นี้ได้ไหม
ได้ บทเรียน Secure Coding & OWASP Top 10 for Backend ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การยืนยันตัวตนหลายปัจจัย (MFA)
- OAuth 2.0 และ OpenID Connect
- ความปลอดภัยและแนวทางปฏิบัติที่ดีของ JWT
- การจัดเก็บรหัสผ่านอย่างปลอดภัยและการกู้คืนข้อมูลรับรอง