Stay Hungry,Stay Foolish!

微软身份平台微软身份平台和 OAuth 2.0 授权代码流和 OAuth 2.0 授权代码流

微软身份平台和 OAuth 2.0 授权代码流

https://learn.microsoft.com/zh-cn/entra/identity-platform/v2-oauth2-auth-code-flow

 

 

 

 

 

https://learn.microsoft.com/en-us/entra/msal/msal-authentication-flows

https://learn.microsoft.com/en-us/entra/msal/overview

https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-react-samples/react-router-sample

https://github.com/AzureAD/microsoft-authentication-library-for-js

 

The Microsoft Authentication Library (MSAL) enables developers to acquire security tokens from the Microsoft identity platform to authenticate users and access secured web APIs. It can be used to provide secure access to Microsoft Graph, Microsoft APIs, third-party web APIs, or your own web API. MSAL supports different application architectures and platforms, including .NET, JavaScript, Java, Python, Android, and iOS.

MSAL is a token acquisition library that offers several ways to get tokens, with a consistent API for supported platforms. Using MSAL provides the following benefits:

  • No need to directly write applications against the OAuth protocol. The plumbing is handled by the library.
  • Can acquire tokens on behalf of a user or application (when applicable to the platform).
  • The library maintains a token cache and refreshes tokens for you when they're about to expire. You don't need to handle token expiration on your own.
  • Helps you specify which audience you want your application to sign in. The sign in audience can include personal Microsoft accounts, social identities with Microsoft Entra External ID organizations, work, school, or users in sovereign and national clouds.
  • Helps you set up your application from configuration files.
  • Helps you troubleshoot your app by exposing actionable exceptions, logging, and telemetry.

 

 

FastAPI-Azure-Auth

 

https://intility.github.io/fastapi-azure-auth/single-tenant/azure_setup

https://github.com/intility/fastapi-azure-auth/tree/main

 

利用微软的这一条机制,在两个网站之间实现sso,如何实现?

根据图片描述的 ​​Microsoft Identity Platform​​ 流程,在两个独立网站(Web A 和 Web B)之间实现 SSO 的核心思路是:​​通过共享同一个 Azure AD 租户的认证会话​​,并利用 access_tokenid_token 作为跨站身份凭证。以下是具体实现步骤:


​1. 前置条件​

  • 两个网站(Web A 和 Web B)均注册为 Azure AD 的 ​​同一租户下的应用​​,并配置相同的登录策略。
  • 确保两网站的 ​​回调地址(Redirect URI)​​ 已在 Azure AD 应用注册中正确配置。

​2. 实现 SSO 的关键流程​

​(1) 用户首次登录 Web A​

  1. ​重定向到 Azure AD​
    Web A 将用户重定向到微软统一登录端点(如 https://login.microsoftonline.com/<tenant-id>/oauth2/authorize),并携带以下参数:

     
  1. POST https://login.microsoftonline.com/<tenant-id>/oauth2/token Body: client_id=web-a-client-id &client_secret=web-a-secret &code=authorization-code &redirect_uri=https://web-a.com/auth/callback &grant_type=authorization_code

    Azure AD 返回 id_token(用户身份)和 access_token(访问资源用)。

  2. ​Web A 创建本地会话​
    验证 id_token 后,Web A 在本地创建用户会话(如设置 Cookie)。


​(2) 用户访问 Web B(实现 SSO)​

  1. ​Web B 检查用户状态​

    • 若用户未登录 Web B,将其重定向到同一 Azure AD 授权端点,但 ​​不强制重新登录​​(利用 Azure AD 的现有会话):
       
    • ?client_id=web-b-client-id &response_type=code &redirect_uri=https://web-b.com/auth/callback &scope=openid profile email &prompt=none # 关键参数:静默尝试 SSO
    • 若用户已在 Web A 登录且会话未过期,Azure AD 直接返回 code 到 Web B 的回调地址,​​无需用户再次输入密码​​。
  1. ​Web B 完成登录​
    Web B 用 code 换取令牌并创建本地会话(同 Web A 步骤 3-4)。


​3. 跨站会话同步(可选)​

  • ​共享根域 Cookie​
    若两网站属于同一父域(如 a.example.comb.example.com),可通过设置根域 Cookie(.example.com)同步登录状态。
  • ​前端通信​
    使用 postMessageBroadcastChannel API 在浏览器标签页间通知登录状态变化。

​4. 关键注意事项​

  1. ​Token 验证​
    两网站需独立验证 id_tokenaccess_token 的签名、issaud 和有效期(参考前文验证逻辑)。
  2. ​权限隔离​
    若需限制 Web A 和 Web B 的权限,可在 Azure AD 中配置不同的 scope(如 api://web-a/readapi://web-b/write)。
  3. ​Token 过期处理​
    使用 refresh_token 静默更新令牌(需在 Azure AD 应用中启用离线访问权限)。

​5. 代码片段(静默 SSO 示例)​

Web B 的静默登录重定向:

 
// Web B 的登录入口 function attemptSilentSSO() { const authUrl = `https://login.microsoftonline.com/<tenant-id>/oauth2/authorize? client_id=web-b-client-id &response_type=code &redirect_uri=https://web-b.com/auth/callback &scope=openid &prompt=none`; // 关键:禁止弹出登录页 // 隐藏 iframe 或新窗口尝试静默获取 code const iframe = document.createElement('iframe'); iframe.src = authUrl; document.body.appendChild(iframe); }

​总结​

通过 Azure AD 的 ​​现有会话共享​​ 和 ​​静默授权(prompt=none)​​ 机制,两网站无需用户重复登录即可实现 SSO。核心依赖:

  1. 统一的 Azure AD 租户和身份源。
  2. 正确的 OAuth 2.0 授权流程(授权码模式)。
  3. 安全的 Token 验证与同步策略。
  • ?client_id=web-a-client-id &response_type=code &redirect_uri=https://web-a.com/auth/callback &scope=openid profile email &state=anti-csrf-token
  • ​用户登录并授权​
    用户在微软登录页完成认证后,Azure AD 返回 ​​授权码(code)​​ 到 Web A 的回调地址。

  • ​Web A 获取令牌​
    Web A 用 code 向 Azure AD 请求令牌:

     

 

 

 

 

 

https://learn.microsoft.com/en-us/entra/msal/overview
posted @ 2025-05-20 09:58  lightsong  阅读(247)  评论(0)    收藏  举报
千山鸟飞绝,万径人踪灭