0Pricing
Secure Coding & OWASP Top 10 for Backend · 课时

OAuth 2.0 与 OpenID Connect

了解并安全地集成行业标准的授权协议(OAuth 2.0)和身份验证协议(OpenID Connect)到您的应用程序中。

OAuth 2.0 与 OpenID Connect 是 CoddyKit 上的免费 Secure Coding & OWASP Top 10 for Backend 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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!

常见问题解答

「OAuth 2.0 与 OpenID Connect」课时是免费的吗?

是的 — 「OAuth 2.0 与 OpenID Connect」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 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 导师会在你学习这节课的过程中回答你的问题。

学习 Secure Coding & OWASP Top 10 for Backend 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Secure Coding & OWASP Top 10 for Backend 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 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 反馈 — 无需本地设置。

此课程中的所有课时

  1. 多因素身份验证(MFA)
  2. OAuth 2.0 与 OpenID Connect
  3. JWT 安全与最佳实践
  4. 安全的密码存储与凭据恢复
← 返回 Secure Coding & OWASP Top 10 for Backend