spaces / oauth2-client
此包已被废弃,不再维护。未建议替代包。
Mittwald SPACES 的 OAuth 2.0 认证
v2.0.0
2018-08-29 18:46 UTC
Requires
- league/oauth2-client: ^2.3
Requires (Dev)
- helmich/phpunit-psr7-assert: ^3.1.0
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2020-04-12 14:02:33 UTC
README
客户端库,用于将 OAuth 2.0 授权流程集成到 PHP 应用程序中。
使用方法
-
提供自己的
Mw\Spaces\OAuth2\Context
接口实现namespace Your\Namespace; class Context implements \Mw\Spaces\OAuth2\Context { public function getRedirectURI() { return "https://my-application.example/oauth-redir"; } }
注意,
/oauth-redir
路径需要指向由您实现的特定应用程序的 OAuth2 重定向处理程序。 -
创建 OAuth2.0 提供者
$ctx = new \Your\Namespace\Context(); $opts = new \Mw\Spaces\OAuth2\EnvironmentOptions($_SERVER); $provider = new \Mw\Spaces\OAuth2\SpacesProvider($opts, $ctx);
-
接下来,获取授权 URL 并将用户重定向到该 URL
$authorizationURL = $provider->getAuthorizationUrl(); $_SESSION["spaces.de/auth/csrf"] = $provider->getState(); header("Location: " . $authorizationURL);
-
身份提供者将提示用户输入其凭据,并在成功后重定向用户回到您的重定向 URI。在处理重定向请求时,您需要检索授权代码并检查 CSRF 值
$state = $_GET["state"]; $code = $_GET["code"]; if ($_SESSION["spaces.de/auth/csrf"] != $state) { die("..."); }
之后,您可以使用该代码来检索访问令牌
$accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $code, ]);
-
有了
$accessToken
,现在您可以使用该令牌(在处理重定向请求的同时)来加载资源所有者try { $owner = $provider->getResourceOwner($accessToken); $ownerID = $accessToken->getResourceOwnerId(); // synchronize local user using $owner } catch (\Mw\Spaces\OAuth2\Error\UserNotPresentException $err) { // user has no access to project // deny login }
使用
$owner
对象中的数据来构建新的本地用户(或更新现有记录)。您可以存储为每个创建的用户分配的 资源所有者 ID 以便稍后匹配。