Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
webguosai committed Oct 28, 2022
1 parent b0287e5 commit 9a88012
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Authentication/Authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Webguosai\Authentication;

interface Authentication
{
/**
* 解析
* @return mixed
*/
public function parse();

/**
* 赋值
* @param mixed $data 数据
* @param int $exp 过期时间(秒)
* @return mixed
*/
public function authenticate($data, $exp);
}
90 changes: 90 additions & 0 deletions src/Authentication/Driver/Jwt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Webguosai\Authentication\Driver;

use Webguosai\Authentication\Authentication;
use Webguosai\Authentication\Exception\TokenExpiredException;
use Webguosai\Authentication\Exception\TokenInvalidException;
use Webguosai\Http\Request;
use Webguosai\Util\Jwt as JWTUtil;

class Jwt implements Authentication
{
protected $queryKey = 'token';
public $pre = 'Bearer ';

/**
* 解析
* @return mixed
* @throws TokenExpiredException
* @throws TokenInvalidException
*/
public function parse()
{
$jwt = $this->fromQuery();

if (is_null($jwt)) {
$jwt = $this->fromHeader();
}

return $this->parseJwt($jwt);
}

/**
* 鉴权
* @param mixed $data
* @param int $exp
* @return mixed|string
*/
public function authenticate($data, $exp = 3600)
{
return JWTUtil::encode($data, $exp);
}

/**
* 解析jwt内容
* @param $jwt
* @return mixed
* @throws TokenExpiredException
* @throws TokenInvalidException
*/
protected function parseJwt($jwt)
{
if (is_null($jwt)) {
throw new TokenInvalidException();
}

$data = JWTUtil::decode($jwt);

if (is_null($data)) {
throw new TokenExpiredException();
}

return $data;
}

/**
* 从query中获取鉴权信息
* @return array|mixed|null
*/
protected function fromQuery()
{
return Request::get($this->queryKey, null);
}

/**
* 从header头中获取鉴权信息
* @return string|string[]|null
*/
protected function fromHeader()
{
$jwt = null;

$value = $_SERVER['HTTP_AUTHORIZATION'];
if (!empty($value)) {
$jwt = str_ireplace($this->pre, '', $value);
}

return $jwt;
}
}
23 changes: 23 additions & 0 deletions src/Authentication/Driver/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Webguosai\Authentication\Driver;

use Webguosai\Authentication\Authentication;
use Webguosai\Util\Session as SessionUtil;

class Session implements Authentication
{
protected $sessionKeyName = 'user';

public function parse()
{
return SessionUtil::getInstance()->get($this->sessionKeyName);
}

public function authenticate($data, $exp)
{
SessionUtil::getInstance()->set($this->sessionKeyName, $data);

return true;
}
}
11 changes: 11 additions & 0 deletions src/Authentication/Exception/TokenExpiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Webguosai\Authentication\Exception;

use Exception;

class TokenExpiredException extends Exception
{
protected $message = 'The token has expired';
}

10 changes: 10 additions & 0 deletions src/Authentication/Exception/TokenInvalidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Webguosai\Authentication\Exception;

use Exception;

class TokenInvalidException extends Exception
{
protected $message = 'Token is invalid';
}
1 change: 0 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
// 'avatar' => 'guosai.png',
//], 10));
//try {
//
// dump($jwt->parse());
//} catch (\Exception $e) {
// dump($e->getMessage());
Expand Down

0 comments on commit 9a88012

Please sign in to comment.