lengbin/auth

dev-master 2020-11-10 15:35 UTC

This package is auto-updated.

Last update: 2024-11-11 00:04:11 UTC


README

感谢yii3

Install

The preferred way to install this extension is through composer.

Either run

composer require lengbin/auth

or add

"lengbin/auth": "*"

to the require section of your composer.json file.

如果没有看懂可以参考hyper-helper

Usage

// 中间件
class ApiMiddleware extends AbstractAuth implements MiddlewareInterface
{
    /**
     * @inheritDoc
     */
    public function getConfig(): array
    {
        return [
              // 全局变量 名称
              'requestName'   => 'api',
              // 实现类,请实现接口 \Lengbin\Auth\IdentityRepositoryInterface::class
              'identityClass' => User::class,
              // 验证器方法,支持
              // header: \Lengbin\Auth\Method\HttpHeaderAuth::class //默认接收参数名称:X-Api-Token
              // query : \Lengbin\Auth\Method\QueryParamAuth::class //默认接收参数名称:access-token
              // sign  : \Lengbin\Auth\Method\SignAuth::class 
              // 如果为 数组 则为 混合验证
              // key => val  接收参数名称 => 验证类
              'method' => [
                  \Lengbin\Auth\Method\HttpHeaderAuth::class,
                  'token' => \Lengbin\Auth\Method\QueryParamAuth::class,
              ],
              //路由白名单。列如 /test/{id}, 可以使用*来通配, /test/*
              'whitelist'     => [],
              //公共访问,不走验证。列如 /test/{id}, 可以使用*来通配, /test/*
              'public'        => [],
          ];
    }

    /**
     * Process an incoming server request.
     *
     * Processes an incoming server request in order to produce a response.
     * If unable to produce the response itself, it may delegate to the provided
     * request handler to do so.
     *
     * @param ServerRequestInterface  $request
     * @param RequestHandlerInterface $handler
     *
     * @return ResponseInterface
     * @throws \Lengbin\Auth\Exception\InvalidTokenException
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $path = $request->getUri()->getPath();
        $isPublic = $this->checkPublicList($path);
        $isWhitelist = $this->checkWhitelist($path);
        $user = $this->getUser($request, $isPublic, $isWhitelist);
        $request->withAttribute($this->requestName, $user);
        return $handler->handle($request);
    }
}