Skip to content

Commit

Permalink
Some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
numelion committed Oct 21, 2022
1 parent 2e2e0f2 commit 47eebea
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 514 deletions.
53 changes: 10 additions & 43 deletions Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use yii\base\InvalidArgumentException;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Encoding\UnixTimestampDates;
use Lcobucci\JWT\Validation\Constraint\IssuedBy;
use Lcobucci\JWT\Validation\Constraint\RelatedTo;
use Lcobucci\JWT\Validation\Validator;

/**
* JSON Web Token implementation, based on this library:
Expand Down Expand Up @@ -47,12 +50,6 @@ class Jwt extends Component
*/
public $key;

/**
* @var string|array|callable \numelion\jwtJwtValidationData
* @see [[Yii::createObject()]]
*/
public $jwtValidationData = JwtValidationData::class;

/**
* @see [[Lcobucci\JWT\Builder::__construct()]]
* @param Encoder|null $encoder
Expand All @@ -72,16 +69,7 @@ public function getBuilder(Encoder $encoder = null, ClaimFactory $claimFactory =
*/
public function getParser(Decoder $decoder = null, ClaimFactory $claimFactory = null)
{
return new Parser($decoder, $claimFactory);
}

/**
* @see [[Lcobucci\JWT\ValidationData::__construct()]]
* @return ValidationData
*/
public function getValidationData()
{
return Yii::createObject($this->jwtValidationData)->getValidationData();
return new Parser($decoder ?? new JoseEncoder());
}

/**
Expand Down Expand Up @@ -135,10 +123,6 @@ public function loadToken($token, $validate = true, $verify = true)
return null;
}

if ($verify && !$this->verifyToken($token)) {
return null;
}

return $token;
}

Expand All @@ -150,30 +134,13 @@ public function loadToken($token, $validate = true, $verify = true)
*/
public function validateToken(Token $token, $currentTime = null)
{
$validationData = $this->getValidationData();
if ($currentTime !== null) {
$validationData->setCurrentTime($currentTime);
$parser = new Parser(new JoseEncoder());
$validator = new Validator();
$jwtParams = Yii::$app->params['jwt'] ?? [];
if (empty($jwtParams)) {
throw new InvalidArgumentException('JWT params not setted');
}
return $token->validate($validationData);
}

/**
* Validate token
* @param Token $token token object
* @return bool
* @throws \Throwable
*/
public function verifyToken(Token $token)
{
$alg = $token->getHeader('alg');

if (empty($this->supportedAlgs[$alg])) {
throw new InvalidArgumentException('Algorithm not supported');
}

/** @var Signer $signer */
$signer = Yii::createObject($this->supportedAlgs[$alg]);

return $token->verify($signer, $this->key);
return $validator->validate($token, new IssuedBy($jwtParams['issuer']));
}
}
49 changes: 0 additions & 49 deletions JwtValidationData.php

This file was deleted.

Loading

0 comments on commit 47eebea

Please sign in to comment.