
Logto Discord 连接器版本演进与 OAuth 2.0 实现解析从 logto/connector-discord 变更日志看社交登录适配层【免费下载链接】logto Authentication and authorization infrastructure for SaaS and AI apps, built on OIDC and OAuth 2.1 with multi-tenancy, SSO, and RBAC.项目地址: https://gitcode.com/GitHub_Trending/lo/logto本文以 Discord 连接器的变更日志logto/connector-discord当前版本 1.6.6为主线梳理该包从 1.1.0 到 1.6.6 的完整版本轨迹并结合 实现源码、常量定义与单元测试逐项印证每一次变更记录背后的实际代码变化帮助读者理解 Logto 社交登录连接器Social Connector的 OAuth 2.0 授权码流程实现、配置项设计clientId/clientSecret/scope以及版本升级中可配置 scope、rawData 落库、构建与运行时基线调整等技术细节。版本全景1.1.0 到 1.6.6 的完整轨迹logto/connector-discord的变更日志记录了 15 个版本。将其按“功能演进 / 构建与依赖 / 安全”三条线索整理如下版本变更类型摘要1.6.6Patch升级依赖logto/connector-kit5.1.11.6.5Patch依赖升级提交e7b6e9de1、b7386a5113connector-kit升至 5.1.01.6.4Patch依赖升级提交41a56f79e3connector-kit升至 5.0.11.6.3Patch依赖升级提交4e25126228connector-kit升至 5.0.01.6.2Patch依赖升级提交462e430445、7c87ebc068connector-kit升至 4.7.01.6.1Patch依赖升级提交ad4f9d6abf、5da6792d40connector-kit升至 4.6.01.6.0MinorgetAuthorizationUri方法支持自定义scope参数提交34964af461.5.0MinorNode 版本要求提升至^22.14.0提交2961d355d1.4.1Patch安全更新依赖升级提交e11e57de81.4.0Minor改用tsup构建构建更快功能不受影响提交510f681fa1.3.1Patch依赖升级connector-kit升至 4.0.01.3.0Minor社交连接器返回并存储 raw data提交57d97a4df1.2.0Minorengine 要求改为 Node 20 LTS提交31e60811d另含 TypeScript 升级至 5.3.39089dbf84等 Patch1.1.1Patch依赖升级connector-kit升至 2.0.01.1.0Minor启用可配置的scope#3723提交5581f6476从版本分布可以读出两个特点其一功能类Minor变更非常克制集中在“可配置 scope”1.1.0、“rawData 返回与存储”1.3.0和“授权请求级自定义 scope”1.6.0三次能力扩展上其二大量 Patch 版本由核心依赖logto/connector-kit的版本联动驱动——这说明 Discord 连接器本身是“薄适配层”其契约与能力演进主要发生在共享工具包中。以下逐一结合仓库源码印证这些关键变更。1.1.0 / 1.6.0两级 scope 演进——从“可配置”到“请求级自定义”变更日志中 1.1.0 的条目是5581f6476: enable configurablescope(#3723)这意味着scope从写死的常量变成了连接器配置项。对应到当前源码有三处证据1配置守卫允许scope作为可选配置项。在 types.ts 中export const discordConfigGuard z.object({ clientId: z.string(), clientSecret: z.string(), scope: z.string().optional(), });2控制台表单定义了三个配置字段。constant.ts 的defaultMetadata.formItems声明了clientId必填 Text、clientSecret必填 Text与scope非必填 MultilineText占位提示为 “Enter the scopes (separated by a space)”描述为 “Thescopedetermines permissions granted by the users authorization.”。这与 README 的配置表完全一致NameTypeclientIdstringclientSecretstringscopestringREADME 同时给出了注册开发者应用的完整步骤在 Discord Developer Portal 创建应用如LogtoAuth在 OAuth2 页Reset Secret记录CLIENT ID与CLIENT SECRET并添加有效的重定向 URI如http://auth.mycompany.io/callback/${connector_id}其中connector_id可在 Logto 管理控制台的连接器详情页顶栏找到。3默认值保留为identify email。在 constant.ts 中/** * OAuth2 Scopes * https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes */ export const scope identify email;而 1.6.0 的变更——34964af46: feat: support custom scope in thegetAuthorizationUrimethod This change allows thegetAuthorizationUrimethod in the social connectors to accept an extrascopeparameter, enabling more flexible authorization requests. If the scope is provided, it will be used in the authorization request; otherwise, the default scope configured in the connector settings will be used.——则在授权 URI 构造处体现为三级优先级调用方传入的scope→ 配置中的scope→ 内置默认identify email。见 index.tsconst queryParameters new URLSearchParams({ client_id: config.clientId, redirect_uri: redirectUri, response_type: code, scope: scope ?? config.scope ?? defaultScope, state, });该scope?参数来自logto/connector-kit的统一契约 GetAuthorizationUri其 payload 类型为{ state, redirectUri, connectorId, connectorFactoryId, jti, headers, scope? }。也就是说1.6.0 的能力扩展是在共享工具包层定义的接口上做的增强所有社交连接器含 Discord一次性获得该能力。单元测试 用两条用例锁定了这个行为不带scope时生成的授权链接包含scopeidentifyemail传入scope: custom_scope时链接变为scopecustom_scope。1.3.0返回并存储社交连接器 raw data变更条目 “57d97a4df: return and store social connector raw data” 在实现上的体现是getUserInfo在归一化用户信息的同时把 Discord 返回的原始 JSON 原样附在结果的rawData字段上const rawUserInfo { id, name, avatar: conditional(avatar https://cdn.discordapp.com/avatars/${id}/${avatar}), email: conditional(verified email), }; const userInfoResult socialUserInfoGuard.safeParse(rawUserInfo); // ... return { ...userInfoResult.data, rawData };对应的统一类型定义在 connector-kit 的 social.ts/** * Normalized social user info that can be used in the system. The raw data returned from the * social provider is also included in the rawData field. */ export type SocialUserInfo { id: string; email?: string; phone?: string; name?: string; avatar?: string; rawData?: Json; };这里的归一化逻辑值得注意Discord 的avatar字段只是头像资源 ID连接器将其拼接为完整 CDN 地址https://cdn.discordapp.com/avatars/${id}/${avatar}而email仅在verified为真时才采信conditional(verified email)避免把未验证邮箱写入账号体系。测试用例验证了完整映射结果输入{ id: 1234567890, username: Whumpus, avatar: avatar_id, email: whumpusdiscord.com, verified: true }输出avatar: https://cdn.discordapp.com/avatars/1234567890/avatar_id且rawData与原始响应逐字段一致。1.2.0 与 1.5.0运行时基线两次抬升1.2.0 条目 “use Node 20 LTS for engine requirement”日志中特别注明 “We mark it as minor because Logto is shipping with Docker image and its not a breaking change for users”——即由于官方以 Docker 镜像交付运行时版本抬升不构成对用户的破坏性变更。1.5.0 条目 “bump node version to ^22.14.0”与当前 package.json 中的engines: { node: ^22.14.0 }和 devDependencies 里的types/node: ^22.14.0完全对应。这两条变更记录提示部署方连接器包的运行时基线跟随仓库整体 LTS 策略演进若以源码方式独立集成该包需以package.json中的engines字段为准。1.4.0 与 1.4.1构建工具切换与安全修复1.4.0 “use tsup for building”日志说明 “This will make the build process faster, and should not affect the functionality of the packages. Use minor version bump to catch your attention.” 当前 package.json 的 scripts 印证了这一点build: tsup、dev: tsup --watchtsup出现在 devDependencies^8.5.1产物统一输出到lib/main: ./lib/index.jsESMtype: module。1.4.1 “bump dependencies for security update”纯安全相关的依赖升级 Patch无功能变化升级时可按常规 Patch 处理。其余 Patch 版本1.1.1、1.3.1、1.6.11.6.6均为logto/connector-kit的联动升级例如 1.6.6 对应logto/connector-kit5.1.1。从源码结构看Discord 连接器对 connector-kit 的依赖面包括类型CreateConnector、SocialConnector、GetAuthorizationUri、GetUserInfo与运行期工具socialUserInfoGuard、validateConfig、ConnectorError、ConnectorErrorCodes、ConnectorType、parseJson因此 kit 的版本联动是保持契约一致的必要动作。完整 OAuth 2.0 授权码流程三个端点与错误处理理解版本演进后把整个连接器的运行流程串起来有助于定位问题。Discord 连接器采用标准授权码response_type: code模式围绕三个固定端点工作定义于 constant.ts端点地址用途authorizationEndpointhttps://discord.com/oauth2/authorize生成用户跳转的授权页 URLaccessTokenEndpointhttps://discord.com/api/v10/oauth2/token用授权码换取 access token固定使用 API v10userInfoEndpointhttps://discord.com/api/v10/users/me以 Bearer token 获取当前用户信息第一步生成授权 URIgetAuthorizationUri。见上文代码参数经URLSearchParams拼装后拼接到authorizationEndpoint之后。第二步用 code 换 tokengetAccessToken。以grant_type: authorization_code表单 POST 到 token 端点携带client_id、client_secret、code、redirect_uri超时由defaultTimeout 5000毫秒控制const httpResponse await got.post(accessTokenEndpoint, { form: { client_id, client_secret, grant_type: authorization_code, code, redirect_uri: redirectUri }, timeout: { request: defaultTimeout }, });响应经accessTokenResponseGuard严格校验必须包含access_token、token_type、expires_in、scope四个字段若access_token为空则抛出ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid)——测试中专门有一条用例mock token 端点返回access_token: 时断言抛出SocialAuthCodeInvalid。第三步拉取用户信息getUserInfo。以Authorization: Bearer tokenGETusers/me解析失败抛InvalidResponse网络错误则按 HTTP 状态细分401映射为SocialAccessTokenInvalidtoken 过期或无效其他状态码包装为General错误并附带响应体 JSON。测试用 nock 模拟了 401 与 500 两种异常路径验证了该错误分支确实存在且可被外部依赖如上层重试/提示逻辑区分处理。连接器最终通过createDiscordConnector工厂导出index.ts 末尾const createDiscordConnector: CreateConnectorSocialConnector async ({ getConfig }) { return { metadata: defaultMetadata, type: ConnectorType.Social, configGuard: discordConfigGuard, getAuthorizationUri: getAuthorizationUri(getConfig), getUserInfo: getUserInfo(getConfig), }; };连接器标识为discord-universal平台为ConnectorPlatform.Universal多语言描述包含zh-CN“Discord 是一款专为社群设计的免费网络实时通话软件与数字发行平台。”等五种语言。小结与升级建议若你需要请求级自定义 scope例如仅identify或扩展其他权限需要logto/connector-discord≥ 1.6.0 且配套logto/connector-kit≥ 4.4.0仅改控制台配置的scope则在 ≥ 1.1.0 即可。若你的系统依赖社交账号原始数据落库用于审计或字段扩展需 ≥ 1.3.0getUserInfo返回值中会携带rawData。独立集成该包的开发者应满足 Node^22.14.0≥ 1.5.0 的要求构建使用 tsup测试使用 vitestpnpm test即vitest run src。1.6.11.6.6 的连续 Patch 均由connector-kit联动驱动升级时建议与仓库整体的connector-kit版本保持同步避免契约错位。更多上下文可参考README应用注册与配置说明、connector-kit 社交连接器类型定义、连接器目录总览。【免费下载链接】logto Authentication and authorization infrastructure for SaaS and AI apps, built on OIDC and OAuth 2.1 with multi-tenancy, SSO, and RBAC.项目地址: https://gitcode.com/GitHub_Trending/lo/logto创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考