-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters