# oauth **Repository Path**: ezy/oauth ## Basic Information - **Project Name**: oauth - **Description**: 简单易用的 OAuth2 授权登录组件。 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2020-10-11 - **Last Updated**: 2025-06-08 ## Categories & Tags **Categories**: oauth-dev **Tags**: None ## README ## OAuth Latest Version 简单易用的 OAuth2 授权登录工具。 - 获取授权页面URL - 通过授权码(code)获取访问令牌(access_token) - 通过访问令牌(access_token)获取用户信息 - 支持静默授权 - 支持移动应用与H5授权登录 - 支持PSR标准,易于集成 - 目前支持平台:Wechat, Alipay, Douyin ## 安装依赖 ```bash composer require reezy/oauth ``` ## 使用 移动应用授权登录 ```php $userId = $container->get(OAuthManagerInterface::class)->login("wechat", $code); ``` H5授权登录 ```php try { // 自动识别宿主Webview并使用对应的授权驱动 $userId = $container->get(OAuthManagerInterface::class)->oauth($request); } catch (OAuthRedirectException $exception) { // 重定向到授权页面 return $exception->getResponse(); } ``` 获取 OAuthInterface 实例 ```php $oauth = $container->get(OAuthFactoryInterface::class)->get('wechat-mp'); ``` 使用前请确保以下接口已注册到容器 - `Psr\Http\Message\ResponseFactoryInterface`,Hyperf 未实现,用户需要自己实现并注册到容器 - `Psr\SimpleCache\CacheInterface`,Hyperf 自带 - `Reezy\OAuth\Contract\OAuthFactoryInterface`,已提供默认实现,Hyperf 中自动注册到容器 - `Reezy\OAuth\Contract\OAuthManagerInterface`,已提供默认实现,Hyperf 中自动注册到容器 - `Reezy\OAuth\Contract\OAuthUserIdProviderInterface`,用户需要自己实现并注册到容器 ```php use Hyperf\HttpMessage\Server\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseFactoryInterface; class DefaultResponseFactory implements ResponseFactoryInterface { public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface { return (new Response())->withStatus($code, $reasonPhrase); } } ``` ## 接口 ```php interface OAuthManagerInterface { /** * 移动应用使用授权码登录并返回用户ID * * @param $name * @param $code * @return int */ function login($name, $code): int; /** * H5 授权登录,返回用户ID,可通过异常 OAuthRedirectException 重定向到授权页的响应对象 * * @param ServerRequestInterface $request * @param string|null $name * @return int * @throws OAuthRedirectException */ public function oauth(ServerRequestInterface $request, string $name = null): int; } interface OAuthInterface { /** * 获取授权页面URL * * @param string|null $redirectUri * @param string|null $state * @param bool $isSilent * @return string */ function getAuthUrl(string $redirectUri = null, string $state = null, bool $isSilent = false): string; /** * 通过授权码(code)获取访问令牌(access_token) * * @param string $code * @return OAuthInfo */ function getAccessToken(string $code): OAuthInfo; /** * 通过访问令牌(access_token)获取用户信息 * * @param OAuthInfo $token * @return OAuthInfo */ function getUserInfo(OAuthInfo $token): OAuthInfo; /** * 是否支持静默授权 * * @return bool */ function isSupportSilentMode(): bool; function getName(): string; function getClientId(): string; function getCodeKey(): string; } interface OAuthUserIdProviderInterface { /** * 授权成功后,获取用户ID,用户不存在时返回0 * * @param string $name * @param string $clientId * @param OAuthInfo $info * @return int */ function find(string $name, string $clientId, OAuthInfo $info): int; /** * 用户不存在时,获取用户信息,注册新用户,返回其ID * * @param string $name * @param string $clientId * @param OAuthInfo $info * @return int */ function register(string $name, string $clientId, OAuthInfo $info): int; } ``` ## 配置 ```php [ 'client_id' => env('WECHAT_OPEN_PLATFORM_APP_ID'), 'client_secret' => env('WECHAT_OPEN_PLATFORM_APP_SECRET'), ], // 微信公众号 'wechat-mp' => [ 'driver' => Reezy\OAuth\Driver\OAuthWechat::class, 'client_id' => env('WECHAT_OFFICIAL_ACCOUNT_APP_ID'), 'client_secret' => env('WECHAT_OFFICIAL_ACCOUNT_APP_SECRET'), // 公众号设置支持静默授权的scope 'scope' => 'snsapi_base' ], // 支付宝 'alipay' => [ 'client_id' => env('ALIPAY_APP_ID'), 'client_secret' => env('ALIPAY_APP_SECRET'), 'app_cert_path' => env('ALIPAY_APP_CERT_PATH'), 'root_cert_path' => env('ALIPAY_ROOT_CERT_PATH'), 'alipay_cert_path' => env('ALIPAY_ALIPAY_CERT_PATH'), ], // OAuthMiddleware 识别 webview 'ua-mappings' => [ 'wechat-mp' => ' MicroMessenger/' , 'alipay' => ' AlipayClient/', 'qq' => ' QQ/', 'douyin' => 'Aweme/' ] ]; ``` ## LICENSE The Component is open-sourced software licensed under the [Apache license](LICENSE).