wp-graphql / wp-graphql-jwt-authentication
WPGraphQL的JWT身份验证
Requires
- firebase/php-jwt: ^6.1.0
Requires (Dev)
- codeception/module-asserts: ^1.0
- codeception/module-cli: ^1.0
- codeception/module-db: ^1.0
- codeception/module-filesystem: ^1.0
- codeception/module-phpbrowser: ^1.0
- codeception/module-rest: ^1.2
- codeception/module-webdriver: ^1.0
- codeception/util-universalframework: ^1.0
- lucatume/wp-browser: 3.1.0
- phpunit/phpunit: ^8.5
- dev-develop
- v0.7.0
- v0.6.0
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.5
- v0.3.4
- 0.3.3
- 0.3.1
- 0.2.1
- dev-master
- dev-fix/php-jwt-version-fix
- dev-release/v0.6.0
- dev-release/v0.5.3
- dev-feature/add-set-cookies-option
- dev-release/v0.5.2
- dev-release/v0.5.1
- dev-release/v0.5.0
- dev-release/v0.4.1
- dev-feature/document-filters
- dev-release/v0.4.0
- dev-feature/#79-add-refreshUserSecret-mutation
- dev-feature/#79-add-user-secret-validation
- dev-bug/#38-do-not-execute-for-invalid-jwt-tokens
This package is auto-updated.
Last update: 2024-09-18 22:20:45 UTC
README
WPGraphQL JWT Authentication
此插件扩展了WPGraphQL插件,以使用JWT(JSON Web Tokens)提供身份验证。
JSON Web Tokens是一种开放、行业标准的RFC 7519方法,用于在两个当事人之间安全地表示声明。
此插件最初基于Enrique Chavez的wp-api-jwt-auth
插件(https://github.com/Tmeister),但已修改(几乎完全)以与WPGraphQL插件一起使用。
安装、激活和设置
您可以像任何WordPress插件一样安装和激活此插件。从Github下载.zip文件并将其添加到您的插件目录中,然后激活。
JWT使用服务器上定义的密钥来验证令牌的签名。
建议您使用类似WordPress Salt生成器(https://api.wordpress.org/secret-key/1.1/salt/)的东西来生成密钥。
您可以这样定义密钥:
define( 'GRAPHQL_JWT_AUTH_SECRET_KEY', 'your-secret-token' );
或者,您可以使用过滤器graphql_jwt_auth_secret_key
以这种方式设置密钥:
add_filter( 'graphql_jwt_auth_secret_key', function() {
return 'your-secret-token';
});
此密钥用于JWT令牌的编码和解码。如果服务器上的密钥更改,则使用先前密钥生成的所有令牌都将无效。因此,如果您想使所有用户令牌无效,则可以在服务器上更改密钥,并且所有先前发放的令牌将无效,并要求用户重新进行身份验证。
- 了解更多关于JWT的信息:https://jwt.node.org.cn/introduction/
HTTP_AUTHORIZATION
为了使用此插件,您的WordPress环境必须支持HTTP_AUTHORIZATION头。在某些情况下,由于某些服务器配置,此头可能未传递给WordPress。
根据您的特定环境,您可能需要研究如何启用这些头,但在Apache中,您可以在您的.htaccess
文件中做以下操作:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
对于NGINX,这可能有效: https://serverfault.com/questions/511206/nginx-forward-http-auth-user#answer-511612
插件的工作方式
登录用户
此插件向WPGraphQL模式添加了新的login
变异。
可以这样使用它:
输入类型: LoginUserInput!
mutation LoginUser { login( input: { clientMutationId: "uniqueId", username: "your_login", password: "your password" } ) { authToken user { id name } } }
响应登录变异后收到的authToken
可以存储在本地存储(或类似)中,并在后续请求中使用HTTP Authorization头在执行GraphQL请求之前对用户进行身份验证。
- 在Apollo Client中设置授权头: https://apollo.graphql.net.cn/docs/react/networking/authentication/#header
- 在Relay Modern中设置授权头: https://relay.graphql.net.cn/docs/en/network-layer.html
- 在Axios中设置授权头: https://github.com/axios/axios#axioscreateconfig
注册用户
输入类型: RegisterUserInput!
mutation RegisterUser { registerUser( input: { clientMutationId: "uniqueId", username: "your_username", password: "your_password", email: "your_email" }) { user { jwtAuthToken jwtRefreshToken } } }
刷新认证令牌
输入类型: RefreshJwtAuthTokenInput!
mutation RefreshAuthToken { refreshJwtAuthToken( input: { clientMutationId: "uniqueId" jwtRefreshToken: "your_refresh_token", }) { authToken } }
过滤器
该插件提供了一些钩子过滤器。
更改认证令牌过期时间
注意:出于安全考虑,我们强烈建议认证令牌具有较短的生存期。除非你清楚自己在做什么,否则不要将此值设置高于300秒。
function custom_jwt_expiration( $expiration ) { return 60; } add_filter('graphql_jwt_auth_expire', 'custom_jwt_expiration', 10);
- 参数:以秒为单位的过期时间
- 默认值:300