0Pricing
OAuth2 & OpenID Connect Deep Dive · 课时

使用 OIDC 的隐式流程

探索使用 OIDC 的隐式流程,了解其直接返回 ID Token 的方式,以及对单页应用的安全影响。

使用 OIDC 的隐式流程 是 CoddyKit 上的免费 OAuth2 & OpenID Connect Deep Dive 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 OAuth2 & OpenID Connect Deep Dive 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 OAuth2 & OpenID Connect Deep Dive 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

OIDC Implicit Flow Basics

What is the Implicit Flow with OpenID Connect (OIDC)? It's a way for web applications, especially Single-Page Applications (SPAs), to get identity information and access tokens directly from the authorization server.

It was designed for scenarios where a backend server couldn't securely store a client secret.

Direct Token Delivery

Unlike the Authorization Code Flow, the Implicit Flow doesn't involve an authorization code exchange with the authorization server's token endpoint.

Instead, the ID Token and Access Token are returned directly to the client's browser in the URL fragment after user authentication.

Requesting Tokens Directly

The client application initiates the flow by redirecting the user's browser to the Authorization Server's /authorize endpoint.

Key parameters include:

  • response_type=id_token token: Requests both an ID Token and an Access Token.
  • client_id: Identifies the client application.
  • redirect_uri: Where the user is sent back after authentication.
  • scope=openid profile: Specifies requested permissions, including openid for OIDC.
  • nonce: A unique string to prevent replay attacks.
https://auth.example.com/authorize?
  response_type=id_token%20token&
  client_id=my-spa-client&
  redirect_uri=https://app.example.com/callback&
  scope=openid%20profile&
  nonce=aRandomNonceValue&
  state=aRandomStateValue

Tokens in the URL Fragment

After the user successfully authenticates and grants consent, the Authorization Server redirects the user's browser back to the redirect_uri.

The tokens (ID Token and Access Token) are included directly in the URL's fragment part (after the # symbol).

The client-side JavaScript then reads and processes these tokens.

https://app.example.com/callback#
  id_token=eyJ...&
  access_token=eyJ...&
  token_type=Bearer&
  expires_in=3600&
  state=aRandomStateValue

Client-Side Token Processing

Since the tokens are in the URL fragment, they are accessible to client-side JavaScript. The browser does not send the fragment to the server.

The SPA extracts the id_token and access_token, validates them, and can then use the access_token to make requests to protected API resources.

public class TokenParser {
  public static void main(String[] args) {
    String urlFragment = "id_token=eyJ...&access_token=eyJ...&expires_in=3600";
    System.out.println("Processing URL fragment:");
    String[] params = urlFragment.split("&");
    for (String param : params) {
      String[] pair = param.split("=");
      if (pair.length == 2) {
        System.out.println(pair[0] + ": " + pair[1]);
      }
    }
    System.out.println("\nIn a real app, you'd validate these tokens!");
  }
}

Identity with the ID Token

The ID Token is a JSON Web Token (JWT) that contains claims about the authenticated user, such as their unique identifier, name, and email.

The client application validates this token to verify the user's identity and ensures it came from the expected Authorization Server.

This is the "identity layer" OpenID Connect adds to OAuth2.

Security Risk: Browser History

A major security concern with the Implicit Flow is that tokens are exposed in the browser's URL fragment.

This means they can be stored in browser history, server access logs (if the fragment is accidentally included), and potentially accessed by other scripts on the same page.

  • Browser History: Tokens might be saved, allowing unauthorized access if someone gains access to the browser history.
  • Referrer Headers: In some cases, tokens could leak via Referrer headers.

Security Risk: No Client Secret

The Implicit Flow is typically used by "public clients" (like SPAs) that cannot securely store a client secret.

This means the Authorization Server cannot authenticate the client application itself, only the user. This makes it vulnerable to certain attacks, such as token injection.

  • An attacker could potentially inject a malicious token.
  • There's no cryptographic proof that the client receiving the token is the one that initiated the request.

Discouraged & Replaced

Due to its inherent security weaknesses, the Implicit Flow is now largely deprecated for new implementations.

The OAuth 2.0 Security Best Current Practice recommends using the Authorization Code Flow with PKCE (Proof Key for Code Exchange) for public clients like SPAs and mobile apps.

PKCE provides a robust way to secure public clients without requiring a client secret.

Implicit Flow Quick Check

Which of the following is a primary security concern when using the OIDC Implicit Flow?

Implicit Flow Recap

We've explored the OIDC Implicit Flow, where identity and access tokens are returned directly in the URL fragment.

While it simplifies client-side access, its security risks, primarily token exposure in the URL and lack of client authentication, have led to its deprecation.

Always prefer the Authorization Code Flow with PKCE for public clients to ensure robust security.

常见问题解答

「使用 OIDC 的隐式流程」课时是免费的吗?

是的 — 「使用 OIDC 的隐式流程」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 OAuth2 & OpenID Connect Deep Dive 课程的其余内容,请升级到 CoddyKit PRO。 OAuth2 & OpenID Connect Deep Dive 课程共包含 4 节课。

「使用 OIDC 的隐式流程」这节课中我会学到什么?

探索使用 OIDC 的隐式流程,了解其直接返回 ID Token 的方式,以及对单页应用的安全影响。 你通过在浏览器中直接运行的动手代码来练习 OAuth2 & OpenID Connect Deep Dive,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 OAuth2 & OpenID Connect Deep Dive 需要有经验吗?

无需任何先前经验。CoddyKit 上的 OAuth2 & OpenID Connect Deep Dive 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 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