Skip to content

Commit 886630a

Browse files
committed
#194: Move generatedId from arg to inner variable
1 parent 4d3aa24 commit 886630a

4 files changed

Lines changed: 20 additions & 29 deletions

File tree

src/Extension/BaseJwt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function handle($request, Closure $next)
3434
die('JWT token required.');
3535
}
3636
$token = (new Parser())->parse((string)$request->jwt);
37-
if(Jwt::verify($token, $token->getHeader('jti')) === false) {
37+
if(Jwt::verify($token) === false) {
3838
header('HTTP/1.1 403 Forbidden');
3939
die('Access forbidden.');
4040
}

src/Extension/JWTTrait.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ protected function createJwtUser()
3030
);
3131
}
3232

33-
$uniqId = uniqid();
3433
/** @var \Illuminate\Database\Eloquent\Model $model */
3534
$model = $this->getEntity($this->model->id);
36-
$model->jwt = Jwt::create($this->model->id, $uniqId);
35+
$model->jwt = Jwt::create($this->model->id);
3736
$model->password = password_hash($this->model->password, PASSWORD_DEFAULT);
3837
$model->save();
3938
$this->model = $model;
@@ -56,8 +55,8 @@ private function updateJwtUser(&$model, $jsonApiAttributes)
5655
]
5756
);
5857
}
59-
$uniqId = uniqid();
60-
$model->jwt = Jwt::create($model->id, $uniqId);
58+
59+
$model->jwt = Jwt::create($model->id);
6160
unset($model->password);
6261
}
6362
}

src/Helpers/Jwt.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ class Jwt
2020
* Fulfills the token with data and signs it with key
2121
*
2222
* @param int $uid
23-
* @param string $generatedId
2423
*
2524
* @return string
2625
* @throws \BadMethodCallException
2726
*/
28-
public static function create(int $uid, string $generatedId) : string
27+
public static function create(int $uid) : string
2928
{
3029
$signer = new Sha256();
3130

31+
$generatedId = uniqid('', true);
3232
return (new Builder())->setIssuer($_SERVER['HTTP_HOST'])// Configures the issuer (iss claim)
3333
->setAudience($_SERVER['HTTP_HOST'])// Configures the audience (aud claim)
3434
->setId($generatedId, true)// Configures the id (jti claim), replicating as a header item
@@ -50,12 +50,15 @@ public static function create(int $uid, string $generatedId) : string
5050
* @throws \BadMethodCallException
5151
* @throws \OutOfBoundsException
5252
*/
53-
public static function verify(Token $token, string $generatedId)
53+
public static function verify(Token $token): bool
5454
{
55+
$generatedId = $token->getHeader('jti');
56+
5557
$data = new ValidationData(); // It will use the current time to validate (iat, nbf and exp)
5658
$data->setIssuer($_SERVER['HTTP_HOST']);
5759
$data->setAudience($_SERVER['HTTP_HOST']);
5860
$data->setId($generatedId);
61+
5962
$signer = new Sha256();
6063
$uid = $token->getClaim('uid');
6164
return $token->validate($data) && $token->verify($signer, $generatedId . config(self::JWT_SECRETE_KEY) . $uid);

tests/Unit/Helpers/JwtTest.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Lcobucci\JWT\Parser;
77
use Lcobucci\JWT\Token;
88
use SoliDry\Extension\BaseJwt;
9-
use SoliDry\Extension\BaseModel;
10-
use SoliDry\Extension\JWTTrait;
119
use SoliDry\Helpers\Jwt;
1210
use SoliDryTest\Unit\TestCase;
1311

@@ -23,40 +21,31 @@ public function setUp(): void
2321
/**
2422
* @test
2523
*/
26-
public function it_creates_jwt_token() : array
24+
public function it_creates_and_verifies_jwt_token(): string
2725
{
2826
/** @var Token $token */
2927
$id = random_int(1, 1000);
30-
$uniqueId = uniqid('', true);
31-
$jwtString = Jwt::create($id, $uniqueId);
28+
$jwtString = Jwt::create($id);
3229
$token = (new Parser())->parse($jwtString);
30+
3331
$this->assertInstanceOf(Token::class, $token);
3432
$this->assertEquals($token->getClaim('uid'), $id);
35-
return [$token, $uniqueId];
36-
}
33+
$this->assertTrue(Jwt::verify($token));
3734

38-
/**
39-
* @test
40-
* @depends it_creates_jwt_token
41-
* @param array $data
42-
* @return array
43-
*/
44-
public function it_verifies_jwt_token(array $data) : array
45-
{
46-
$this->assertTrue(Jwt::verify($data[0], $data[1]));
47-
return $data;
35+
return $jwtString;
4836
}
4937

5038
/**
5139
* @test
52-
* @depends it_verifies_jwt_token
53-
* @param array $data
40+
* @depends it_creates_and_verifies_jwt_token
41+
* @param string $jwt
5442
*/
55-
public function it_handles_jwt(array $data)
43+
public function it_handles_jwt(string $jwt): void
5644
{
5745
$baseJwt = new BaseJwt();
5846
$request = new Request();
59-
$request->jwt = $data[0];
47+
$request->jwt = $jwt;
48+
6049
$baseJwt->handle($request, function ($request) {
6150
$this->assertInstanceOf(Request::class, $request);
6251
});

0 commit comments

Comments
 (0)