0Pricing
OAuth2 & OpenID Connect Deep Dive · บทเรียน

โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC

ใช้งานโฟลว์รหัสอนุญาตสิทธิ์ที่ปลอดภัย ซึ่งขยายด้วย OIDC เพื่อส่งโทเค็น ID ควบคู่กับโทเค็นเข้าถึง

โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

OIDC & Auth Code Flow Intro

Welcome! In this lesson, we'll combine the secure Authorization Code Flow from OAuth2 with OpenID Connect (OIDC). This allows an application to not only get permission to access resources (OAuth2) but also to verify the user's identity (OIDC).

Think of it as getting both a key to a safe and a valid ID card for the person holding the key, all in one go!

Requesting Identity with OIDC

When using the Authorization Code Flow with OIDC, your application (the Client) starts by sending an authorization request to the Authorization Server. This request is similar to OAuth2 but includes specific OIDC parameters.

  • response_type=code: Specifies we want an Authorization Code.
  • scope=openid ...: Crucially, includes the openid scope to signal an OIDC request.
  • nonce: A unique string to prevent replay attacks (optional but recommended).

The Essential `openid` Scope

The openid scope is the magic flag that tells the Authorization Server you're performing an OpenID Connect request, not just a plain OAuth2 request.

Without openid in your request's scope, the Authorization Server will treat it as a standard OAuth2 flow and will not issue an ID Token.

You can combine openid with other standard OAuth2 scopes like profile, email, or custom scopes.

Beyond `openid`: More Scopes

While openid is mandatory, other OIDC-specific scopes allow you to request more user information:

  • profile: Access to default profile claims (name, family name, gender, picture, etc.).
  • email: Access to the user's email address and verification status.
  • address: Access to the user's postal address.
  • phone: Access to the user's phone number and verification status.

These claims are then included in the ID Token or accessible via the UserInfo Endpoint.

User Consent & Code Grant

After your app sends the authorization request, the user is redirected to the Authorization Server. They log in (if needed) and are prompted to consent to your app's requested scopes.

If the user approves, the Authorization Server redirects the user back to your app's registered redirect URI, including the Authorization Code in the URL query parameters.

The Token Exchange

Now, your application (running on a server, for confidential clients) takes the received Authorization Code and exchanges it directly with the Authorization Server's token endpoint.

This is a back-channel request, meaning it happens directly between your server and the Authorization Server, not through the user's browser.

Crucially, this exchange now returns three important pieces of information:

  • Access Token
  • Refresh Token (if requested)
  • ID Token

What is the ID Token?

The ID Token is a JSON Web Token (JWT) that contains claims about the authenticated user. It's signed by the Authorization Server, allowing your application to verify its authenticity.

Unlike the Access Token (which is for resource access), the ID Token is specifically for identity verification. It tells your application *who* the user is, not *what* they can do.

ID Token for Identity, Access Token for API

Once your application receives the tokens:

  • ID Token: You can decode and validate the ID Token to confirm the user's identity and retrieve basic profile information (claims). This is often used for user login sessions.
  • Access Token: This token is then used to make requests to protected Resource Servers (APIs) on behalf of the user, granting access to specific resources based on the requested scopes.

OIDC Auth Code Flow in Action

Here's a simplified conceptual look at the server-side token exchange. In a real application, you'd use an OIDC client library.

public class OidcClientExample {
  public static void main(String[] args) {
    // 1. User is redirected to Auth Server
    //    Example URL:
    //    https://auth.example.com/oauth/authorize?
    //      response_type=code&
    //      client_id=my_app_id&
    //      scope=openid%20profile%20email&
    //      redirect_uri=https://my-app.com/callback&
    //      nonce=random_string
    
    // 2. User grants consent, redirected back with code
    String authCode = "some_long_authorization_code"; // From redirect URI
    
    // 3. Exchange code for tokens (server-side POST request)
    System.out.println("Exchanging Authorization Code for tokens...");
    // This part would typically be handled by an HTTP client
    // sending a POST request to the token endpoint.
    // The response would contain:
    // {
    //   "access_token": "...",
    //   "token_type": "Bearer",
    //   "expires_in": 3600,
    //   "refresh_token": "...",
    //   "id_token": "..." // The OIDC addition!
    // }
    System.out.println("Received Access Token, Refresh Token, and ID Token!");
    System.out.println("ID Token contains user identity claims.");
  }
}

OIDC Auth Code Flow Check

Consider an application implementing the OpenID Connect Authorization Code Flow.

Which of the following parameters is essential in the initial authorization request to ensure an ID Token is returned?

OIDC Auth Code Flow Recap

Great job! You've learned how the secure Authorization Code Flow is extended by OpenID Connect to provide both authorization and identity.

  • We use response_type=code and include the openid scope.
  • The Authorization Server returns an Authorization Code.
  • This code is exchanged for an Access Token (for resource access) and an ID Token (for user identity).
  • The ID Token is a JWT carrying user claims, signed by the Authorization Server.

This flow is highly recommended for confidential clients like web applications.

คำถามที่พบบ่อย

บทเรียน “โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC”

ใช้งานโฟลว์รหัสอนุญาตสิทธิ์ที่ปลอดภัย ซึ่งขยายด้วย OIDC เพื่อส่งโทเค็น ID ควบคู่กับโทเค็นเข้าถึง คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม

ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC
  2. โฟลว์แบบแฝงด้วย OIDC
  3. โฟลว์แบบผสมด้วย OIDC
  4. การใช้ nonce เพื่อป้องกันการเล่นซ้ำ
← กลับไปที่ OAuth2 & OpenID Connect Deep Dive