littlewonders/wordpress-oauth

该包最新版本(1.0.10)没有可用的许可证信息。

WordPress 的简单 OAuth 登录

安装: 19

依赖关系: 0

建议者: 0

安全: 0

星级: 0

分支: 0

类型:wordpress-plugin

1.0.10 2023-01-25 16:59 UTC

This package is auto-updated.

Last update: 2024-09-25 21:07:02 UTC


README

一个基本的 WordPress 插件,允许通过 OAuth 进行单点登录。已测试与 Amazon Cognito,但应适用于任何 OAuth 兼容的服务器。

最初是从 WordPress 插件(oauth-client-for-user-authentication)[https://wordpresstheme.cn/plugins/oauth-client-for-user-authentication/] 分支出来的,因为在测试该插件时我发现许多安全漏洞,所以我决定将其用作基础,并将其尽可能简化,以便代码易于审计和理解。

该插件的范围设计得非常简单。它支持:

  • 在登录表单中添加按钮以开始 SSO
  • 使用单个身份提供者进行身份验证
  • 自动将 /wp-login.php 重定向以开始与 IDP 的 OAuth 流程
  • 通过任何 IDP 返回的任何属性查找用户,与任何 wordpress_users 列进行比较
  • 在从 WordPress 登出后,将用户重定向到 IDP 的注销端点

它不支持,也不会支持:

  • 从 wp-admin 界面编辑插件设置
  • 为 IDP 用户注册/创建新用户
  • 从 / 验证到 LDAP 的同步
  • 多个身份提供者
  • OpenID Connect

目前不支持,但计划在未来的版本中支持:

  • 在完成身份验证后将用户正确重定向到他们试图访问的页面
  • 完全禁用用户登录页面,因此无法使用用户名和密码登录

配置

所有配置都定义在您的 wp-config.php 中。您需要添加以下部分:

define('OAUTHCLIENT_CONFIG', [
    // OAuth identity provider URLs
	"client_authorization" => "https://youridp.com/oauth2/authorize",
	"client_token_endpoint" => "https://youridp.com/oauth2/token",
	"client_logout_endpoint" => "https://youridp.com/logout",
	"client_userinfo_endpoint" => "https://youridp.com/oauth2/userInfo",
    // Whether to include the client_id and client_secret in the request body of the /token call
	"client_request_in_body" => true,
    // Client ID & Client Secret 
	"client_id" => "example",
	"client_secret" => "exampleexample",
    // Which scope to include in the call to /authorize. Should normally be openid
	"scope" => "openid",
    // See "Modes" section
	"scope" => "id_token",
    // See "User Lookup" section
	"user_lookup_column" => "user_login",
	"user_lookup_attribute" => "custom:wordpress-username",
    // Whether to allow sign-in via normal Wordpress form
    //
    // Note that this only redirects the user to SSO automatically, it doesn't disable 
    // the normal sign-in mechanisms so a malicious user could still perform a 
    // username/password signin
	"force_sso" => true
]);
模式

该插件支持两种不同的身份验证方法/模式。

id_token 会在调用 client_token_endpoint 后期望返回一个 id_token 参数。它期望一个 JSONWebToken。

请注意,此模式不会验证 JSONWebToken 的有效性或过期时间,因为令牌是从 WordPress 直接到 OpenID 服务器的 HTTPS 调用中获得的,用户没有机会修改它。

access_token 模式会在调用 client_token_endpoint 后期望返回一个 access_token。然后它将使用此访问令牌作为 Bearer 令牌来调用 client_userinfo_endpoint,并使用此端点的响应数据来匹配用户。

用户查找

"用户查找"选项允许您将任何身份提供者属性与 wp_users 表中的任何列进行比较以查找匹配的用户。

user_lookup_column 的常见值:

描述
IDWordPress 用户 ID
user_loginWordPress 用户名
user_emailWordPress 用户的电子邮件

对于 user_lookup_attribute 选项,这取决于您的身份提供者数据的结构。嵌套属性将被扁平化为下划线分隔的数组键。例如,如果 userInfo 请求返回

{
    "sub": "248289761001",
    "name": "Jane Doe",
    "given_name": "Jane",
    "family_name": "Doe",
    "preferred_username": "j.doe",
    "email": "janedoe@example.com",
    "someAttributes": {
        "wordpressUsername": "jane.d"
    }
}

那么您可以设置以下配置值,以允许用户进行身份验证

user_lookup_columnuser_login
user_lookup_attributesomeAttributes_wordpressUsername