跳到主要内容
给我们的新朋友:

Logto 是一个为现代应用和 SaaS 产品设计的 Auth0 替代方案。它提供 Cloud开源 服务,帮助你快速启动身份和管理 (IAM) 系统。享受认证 (Authentication)、授权 (Authorization) 和多租户管理 一体化

我们建议从 Logto Cloud 上的免费开发租户开始。这可以让你轻松探索所有功能。

在本文中,我们将介绍使用 PythonLogto 快速构建 Microsoft Entra ID OIDC enterprise SSO 登录体验(用户认证 (Authentication))的步骤。

先决条件

  • 一个正在运行的 Logto 实例。查看 介绍页面 以开始。
  • Python 的基本知识。
  • 一个可用的 Microsoft Entra ID OIDC enterprise SSO 账户。

Create an application in Logto

Logto 基于 OpenID Connect (OIDC) 认证 (Authentication) 和 OAuth 2.0 授权 (Authorization)。它支持跨多个应用程序的联合身份管理,通常称为单点登录 (SSO)。

要创建你的 Traditional web 应用程序,只需按照以下步骤操作:

  1. 打开 Logto Console。在“开始使用”部分,点击“查看全部”链接以打开应用程序框架列表。或者,你可以导航到 Logto Console > Applications,然后点击“创建应用程序”按钮。 开始使用
  2. 在打开的模态窗口中,点击“Traditional web”部分,或使用左侧的快速过滤复选框过滤所有可用的“Traditional web”框架。点击 "Flask" 框架卡片以开始创建你的应用程序。 框架
  3. 输入应用程序名称,例如“Bookstore”,然后点击“创建应用程序”。

🎉 太棒了!你刚刚在 Logto 中创建了你的第一个应用程序。你将看到一个祝贺页面,其中包含详细的集成指南。按照指南查看你的应用程序中的体验将会是什么样的。

Integrate Flask SDK

提示:
  • 示例使用 Flask,但概念对于其他框架是相同的。
  • Python 示例项目可在我们的 Python SDK 仓库 中找到。
  • Logto SDK 利用协程,调用异步函数时请记得使用 await

安装

在项目根目录执行:

pip install logto # 或者 `poetry add logto` 或者你使用的其他工具

初始化 LogtoClient

首先,创建一个 Logto 配置:

client.py
from logto import LogtoClient, LogtoConfig

client = LogtoClient(
LogtoConfig(
endpoint="https://you-logto-endpoint.app", # 用你的 Logto endpoint 替换
appId="replace-with-your-app-id",
appSecret="replace-with-your-app-secret",
),
)
提示:

你可以在管理控制台的应用详情页面找到并复制“应用密钥”:

App Secret

还可以将默认的内存存储替换为持久存储,例如:

client.py
from logto import LogtoClient, LogtoConfig, Storage
from flask import session
from typing import Union

class SessionStorage(Storage):
def get(self, key: str) -> Union[str, None]:
return session.get(key, None)

def set(self, key: str, value: Union[str, None]) -> None:
session[key] = value

def delete(self, key: str) -> None:
session.pop(key, None)

client = LogtoClient(
LogtoConfig(...),
storage=SessionStorage(),
)

查看 Storage 了解更多详情。

实现登录和登出

在你的 Web 应用中,添加一个路由来正确处理用户的登录请求。我们以 /sign-in 为例:

flask.py
@app.route("/sign-in")
async def sign_in():
# 获取登录 URL 并重定向用户到该 URL
return redirect(await client.signIn(
redirectUri="http://localhost:3000/callback",
))

http://localhost:3000/callback 替换为你在 Logto Console 中为此应用设置的回调 URL。

如果你想在第一个屏幕显示注册页面,可以将 interactionMode 设置为 signUp

flask.py
@app.route("/sign-in")
async def sign_in():
return redirect(await client.signIn(
redirectUri="http://localhost:3000/callback",
interactionMode="signUp", # 在第一个屏幕显示注册页面
))

现在,每当你的用户访问 http://localhost:3000/sign-in 时,就会启动一个新的登录尝试,并将用户重定向到 Logto 登录页面。

注意 创建一个登录路由并不是启动登录尝试的唯一方法。你可以随时使用 signIn 方法获取登录 URL 并将用户重定向到该 URL。

在用户发出注销请求后,Logto 将清除会话中的所有用户认证 (Authentication) 信息。

要清理 Python 会话和 Logto 会话,可以实现一个注销路由,如下所示:

flask.py
@app.route("/sign-out")
async def sign_out():
return redirect(
# 在成功注销后将用户重定向到主页
await client.signOut(postLogoutRedirectUri="http://localhost:3000/")
)

处理认证 (Authentication) 状态

在 Logto SDK 中,我们可以使用 client.isAuthenticated() 来检查认证 (Authentication) 状态,如果用户已登录,值将为 true,否则,值将为 false。

这里我们还实现了一个简单的主页用于演示:

  • 如果用户未登录,显示一个登录按钮;
  • 如果用户已登录,显示一个登出按钮。
@app.route("/")
async def home():
if client.isAuthenticated() is False:
return "未认证 <a href='/sign-in'>登录</a>"
return "已认证 <a href='/sign-out'>登出</a>"

检查点:测试你的应用程序

现在,你可以测试你的应用程序:

  1. 运行你的应用程序,你将看到登录按钮。
  2. 点击登录按钮,SDK 将初始化登录过程并将你重定向到 Logto 登录页面。
  3. 登录后,你将被重定向回你的应用程序,并看到登出按钮。
  4. 点击登出按钮以清除令牌存储并登出。

Add Microsoft Entra ID OIDC enterprise SSO connector

To simplify access management and gain enterprise-level safeguards for your big clients, connect with Flask as a federated identity provider. The Logto enterprise SSO connector helps you establish this connection in minutes by allowing several parameter inputs.

To add an enterprise SSO connector, simply follow these steps:

  1. Navigate to Logto console > Enterprise SSO.

SSO page

  1. Click "Add enterprise connector" button and choose your SSO provider type. Choose from prebuilt connectors for Microsoft Entra ID (Azure AD), Google Workspace, and Okta, or create a custom SSO connection using the standard OpenID Connect (OIDC) or SAML protocol.
  2. Provide a unique name (e.g., SSO sign-in for Acme Company).

Select your SSO provider

  1. Configure the connection with your IdP in the "Connection" tab. Check the guides above for each connector types.

SSO connection

  1. Customize the SSO experience and enterprise’s email domain in the "Experience" tab. Users sign in with the SSO-enabled email domain will be redirected to SSO authentication.

SSO experience

  1. Save changes.

Set up Azure AD SSO 应用程序

步骤 1:创建 Microsoft EntraID OIDC 应用程序

  1. 前往 Microsoft Entra 管理中心 并以管理员身份登录。

  2. 浏览到 Identity > Applications > App registrations。

Create Application
  1. 选择 New registration

  2. 输入应用程序名称,并为你的应用程序选择适当的账户类型。

  3. 选择 Web 作为应用程序平台。

  4. 从 Logto 的 SSO 设置页面复制并粘贴 redirect URIredirect URI 是用户在通过 Microsoft Entra ID 认证后被重定向的 URL。

Configure Application
  1. 点击 Register 创建应用程序。

步骤 2:在 Logto 配置 Microsoft Entra ID OIDC 单点登录 (SSO)

成功创建 Microsoft Entra OIDC 应用程序后,你需要将 IdP 配置提供给 Logto。在 Logto 控制台中导航到 Connection 选项卡,并填写以下配置:

  1. Client ID:由 Microsoft Entra 分配给你的 OIDC 应用程序的唯一标识符。Logto 使用此标识符在 OIDC 流程中识别和认证 (Authentication) 应用程序。你可以在应用程序概览页面找到它,标识为 Application (client) ID
Application Details
  1. Client Secret:创建一个新的客户端密钥,并将其值复制到 Logto。此密钥用于认证 (Authentication) OIDC 应用程序并保护 Logto 与 IdP 之间的通信。
Create Secret
  1. 发行者 (Issuer):发行者 URL,是 IdP 的唯一标识符,指定可以找到 OIDC 身份提供商 (IdP) 的位置。它是 OIDC 配置的重要组成部分,因为它帮助 Logto 发现必要的端点。

    Logto 不需要手动提供所有这些 OIDC 端点,而是自动获取所有必需的配置和 IdP 端点。这是通过利用你提供的发行者 URL 并调用 IdP 的发现端点来完成的。

    要获取发行者 URL,你可以在应用程序概览页面的 Endpoints 部分找到它。

    找到 OpenID Connect metadata document 端点并复制 URL 不包括末尾路径 .well-known/openid-configuration。这是因为 Logto 在获取 OIDC 配置时会自动将 .well-known/openid-configuration 附加到发行者 URL。

Endpoints
  1. 权限 (Scope):一个以空格分隔的字符串列表,定义 Logto 在 OIDC 认证 (Authentication) 过程中请求的所需权限或访问级别。scope 参数允许你指定 Logto 从 IdP 请求哪些信息和访问权限。

scope 参数是可选的。无论自定义 scope 设置如何,Logto 始终会向 IdP 发送 openidprofileemail 权限 (Scopes)。

点击 Save 完成配置过程。

步骤 3:设置电子邮件域并启用 SSO 连接器

在连接器 体验 (Experience) 选项卡中提供你组织的电子邮件 。这将启用 SSO 连接器作为这些用户的认证 (Authentication) 方法。

在指定域中的电子邮件地址的用户将被专门限制为仅使用你的 SSO 连接器作为他们的唯一认证 (Authentication) 方法。

Save your configuration

仔细检查你是否在 Logto 连接器配置区域填写了必要的值。点击“保存并完成”(或“保存更改”),Microsoft Entra ID OIDC enterprise SSO 连接器现在应该可用了。

Enable Microsoft Entra ID OIDC enterprise SSO connector in Sign-in Experience

You don’t need to configure enterprise connectors individually, Logto simplifies SSO integration into your applications with just one click.

  1. Navigate to: Console > Sign-in experience > Sign-up and sign-in.
  2. Enable the "Enterprise SSO" toggle.
  3. Save changes.

Once enabled, a "Single Sign-On" button will appear on your sign-in page. Enterprise users with SSO-enabled email domains can access your services using their enterprise identity providers (IdPs).

Auto detect SSO sign-in via email domain Navigate to SSO sign-in via manually click link button

To learn more about the SSO user experience, including SP-initiated SSO and IdP-initiated SSO, refer to User flows: Enterprise SSO.

Testing and Validation

返回到你的 Python 应用。你现在应该可以使用 Microsoft Entra ID OIDC enterprise SSO 登录了。享受吧!

Further readings

终端用户流程:Logto 提供开箱即用的认证 (Authentication) 流程,包括多因素认证 (MFA) 和企业单点登录 (SSO),以及强大的 API,用于灵活实现账户设置、安全验证和多租户体验。

授权 (Authorization):授权 (Authorization) 定义了用户在被认证 (Authentication) 后可以执行的操作或访问的资源。探索如何保护你的 API 以用于原生和单页应用程序,并实现基于角色的访问控制 (RBAC)。

组织 (Organizations):在多租户 SaaS 和 B2B 应用中特别有效,组织功能支持租户创建、成员管理、组织级 RBAC 和即时供应。

客户 IAM 系列:我们关于客户(或消费者)身份和访问管理的系列博客文章,从 101 到高级主题及更深入的内容。