bag2 / oauth-pkce
OAuth PKCE实现,独立于OAuth服务器。
0.0.1
2022-08-29 13:59 UTC
Requires
- php: ^7.1 || ~8.0.0 || ~8.1.0
- ircmaxell/random-lib: ^1.2
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-08-23 14:49:39 UTC
README
PHP RFC 7636 - OAuth公共客户端的代码交换证明密钥 (PKCE) 实现独立于OAuth服务器。
为什么使用这个包?
已知的OAuth2服务器实现(例如 league/oauth2-server)已实现PKCE,但不是基于最新实现的。此库提供向独立OAuth服务器添加PKCE验证的功能。
用法
参见OAuth 2.0: 4.1. 授权代码授予中的图3:授权代码流。
对于授权服务器
1. 在步骤(A)和(B)中存储code_challenge
在此流程中,编写如下
// This (pseudo) code is written in vanilla PHP. // Actually follow your framework / project conventions. use Bag2\OAuth\PKCE\Challenge; // Request by Web Browser $code_challenge = \filter_input(INPUT_POST, 'code_challenge'); $code_challenge_method = \filter_input(INPUT_GET, 'code_challenge_method') ?: 'plain'; if ($code_verifier !== null) { if (!Verifier::isValidCodeVerifier($code_challenge)) { throw new Exception('invalid code_challenge'); } if (!Verifier::isValidCodeChallengeMethod($code_challenge_method)) { throw new Exception('invalid code_challenge_method'); } } store_value([ 'code' => getnerate_oauth_code(), 'code_challenge' => $code_challenge, 'code_challenge_method' => $code_challenge_method, ]); // Redirect
2. 在步骤(D)中验证code_verifier
// This (pseudo) code is written in vanilla PHP. // Actually follow your framework / project conventions. use Bag2\OAuth\PKCE\Challenge; // Request by Client $code = \filter_input(INPUT_POST, 'code'); $code_verifier = \filter_input(INPUT_POST, 'code_verifier'); $saved = get_stored_value($code); if (isset($saved['code_challenge'])) { if ($code_verifier === null) { throw new Exception('$code_verifier required'); } $verifier = Challenge::fromArray($saved); if (!$verifier->verify($code_verifier)) { throw new Exception('code_challenge required'); } } // Return generated Access Token
版权
此包根据Apache License 2.0许可。
版权所有 2019 Baguette HQ
根据Apache License,版本2.0(“许可证”)许可;除非适用法律要求或经书面同意,否则不得使用此文件,除非符合许可证。您可以在以下位置获得许可证副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则在许可证下分发的软件按照“原样”基础分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言和限制,请参阅许可证。