Skip to content

Commit

Permalink
fix asymmetric encry
Browse files Browse the repository at this point in the history
  • Loading branch information
QThans committed Apr 12, 2023
1 parent fe05cd4 commit dfa90bb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@
> 备注:dsa 512位
> 重要:RSA和DSA 都是非对称加密方式,除了修改参数ALGO外,需要配置:PUBLIC_KEY、PRIVATE_KEY两个参数,
> 这两个参数支持文本(不要开头、结尾和换行)或密钥文件路径。如果密钥设置了密码,请配置好参数:PASSWORD
> 这两个参数**只支持**密钥文件路径。如果密钥设置了密码,请配置好参数:PASSWORD
> env文件不支持内容有等于号,遇到这种情况:
>1、使用路径 2、生成没有等于号的密钥。
## 安装

第一步:
Expand Down
11 changes: 6 additions & 5 deletions src/provider/JWT/Lcobucci.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,24 @@ public function encode(array $payload)
$e
);
}

return (string) $builder->getToken();
}

public function decode($token)
{
try {
$jwt = $this->configuration->parser()->parse($token);
$token = $this->configuration->parser()->parse($token);
} catch (Exception $e) {
throw new TokenInvalidException('Could not decode token: '
. $e->getMessage(), $e->getCode(), $e);
}
if (!$this->configuration->validator()->validate($jwt, ...$this->configuration->validationConstraints())) {

if (!$this->configuration->validator()->validate($token, ...$this->configuration->validationConstraints())) {
throw new TokenInvalidException('Token Signature could not be verified.');
}

$claims = [];
foreach ($jwt->claims()->all() as $key => $claim) {
foreach ($token->claims()->all() as $key => $claim) {
if ($claim instanceof DateTimeInterface) {
$claims[$key] = (int) $claim->getTimestamp();
} else {
Expand Down Expand Up @@ -167,7 +168,7 @@ protected function getSigningKey()
throw new JWTException('Private key is not set.');
}

return $this->getKey($privateKey, $this->getPassphrase() ?? '');
return $this->getKey($privateKey, $this->getPassword() ?? '');
}

if (!$secret = $this->getSecret()) {
Expand Down
18 changes: 4 additions & 14 deletions src/provider/JWT/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,17 @@ class Provider
public function getPublicKey()
{
if (is_file($this->keys['public'])) {
return $this->keys['public'];
return 'file://'. $this->keys['public'];
}

return '-----BEGIN PUBLIC KEY-----'.PHP_EOL.implode(PHP_EOL, str_split($this->keys['public'], 64)).PHP_EOL
.'-----END PUBLIC KEY-----';
throw new JWTException('Please set public key as the path of pem file.');
}

public function getPrivateKey()
{
$header = '-----BEGIN PRIVATE KEY-----';
$footer = '-----END PRIVATE KEY-----';
if (is_file($this->keys['private'])) {
return $this->keys['private'];
}
if ($this->keys['password'] != '') {
$header = '-----BEGIN ENCRYPTED PRIVATE KEY-----';
$footer = '-----END ENCRYPTED PRIVATE KEY-----';
return 'file://'.$this->keys['private'];
}

return $header.PHP_EOL.implode(PHP_EOL, str_split($this->keys['private'], 64)).PHP_EOL
.$footer;
throw new JWTException('Please set private key as the path of pem file.');
}

public function getSecret()
Expand Down

0 comments on commit dfa90bb

Please sign in to comment.