Skip to content

Commit

Permalink
Changing order of TokenDecoded constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
nowakowskir committed Oct 26, 2020
1 parent 1214661 commit 7982ac2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ If you need more detailed information about why your validation process has fail

Exception Class | Description
------------ | -------------
Nowakowskir\JWT\Exceptions\IntegrityViolationException | Token is not trusted. Either invalid key was provided or token was tampered.
Nowakowskir\JWT\Exceptions\AlgorithmMismatchException | If the algorithm you decided to use to validate the token is different from the algorithm specified in the token's header.
Nowakowskir\JWT\Exceptions\TokenExpiredException | Token has expired (if ```exp``` was set by issuer).
Nowakowskir\JWT\Exceptions\TokenInactiveException | Token is not yet active (if ```nbf``` was set by issuer).
``Nowakowskir\JWT\Exceptions\IntegrityViolationException`` | Token is not trusted. Either invalid key was provided or token was tampered.
``Nowakowskir\JWT\Exceptions\AlgorithmMismatchException`` | If the algorithm you decided to use to validate the token is different from the algorithm specified in the token's header.
``Nowakowskir\JWT\Exceptions\TokenExpiredException`` | Token has expired (if ```exp``` was set by issuer).
``Nowakowskir\JWT\Exceptions\TokenInactiveException`` | Token is not yet active (if ```nbf``` was set by issuer).


### Building the new JWT with expiration date (exp)

If you want your token to expire at some date, you can use ```exp``` flag.

```
$tokenDecoded = new TokenDecoded(null, ['exp' => time() + 1000]);
$tokenDecoded = new TokenDecoded(['exp' => time() + 1000]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_RS256);
```

Expand All @@ -133,7 +133,7 @@ $tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_RS256);
If you want your token to be not active until reach some date, you can use ```nbf``` flag.

```
$tokenDecoded = new TokenDecoded(null, ['nbf' => time() + 1000]);
$tokenDecoded = new TokenDecoded(['nbf' => time() + 1000]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_RS256);
```

Expand Down
4 changes: 2 additions & 2 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class JWT
public static function decode(TokenEncoded $tokenEncoded): TokenDecoded
{
return new TokenDecoded(
json_decode(Base64Url::decode($tokenEncoded->getHeader()), true),
json_decode(Base64Url::decode($tokenEncoded->getPayload()), true)
json_decode(Base64Url::decode($tokenEncoded->getPayload()), true),
json_decode(Base64Url::decode($tokenEncoded->getHeader()), true)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/TokenDecoded.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class TokenDecoded
protected $payload;

/**
* @param array|null $header
* @param array|null $payload
* @param array|null $header
*/
public function __construct(?array $header = null, ?array $payload = null)
public function __construct(?array $payload = null, ?array $header = null)
{
$this->payload = $payload ?? [];
$this->header = $header ?? [];
Expand Down
36 changes: 18 additions & 18 deletions tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TokenEncodedTest extends TokenBaseTest
{
public function test_providing_different_algorithm_in_token_header_throws_an_exception()
{
$tokenDecoded = new TokenDecoded(['alg' => JWT::ALGORITHM_RS512]);
$tokenDecoded = new TokenDecoded(null, ['alg' => JWT::ALGORITHM_RS512]);

$key = file_get_contents('./tests/keys/private.key');

Expand Down Expand Up @@ -82,7 +82,7 @@ public function test_bypassing_would_be_possible_without_algorithm_forcing(): vo
} catch (IntegrityViolationException $e) {
$exception = true;
}

$this->assertFalse($exception);
}

Expand Down Expand Up @@ -411,7 +411,7 @@ public function test_encoding_with_wrong_exp_claim_type(): void

$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['exp' => 'string']);
$tokenDecoded = new TokenDecoded(['exp' => 'string']);
$tokenDecoded->encode($key, JWT::ALGORITHM_HS256);
}

Expand All @@ -428,7 +428,7 @@ public function test_encoding_with_wrong_nbf_claim_type(): void

$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['nbf' => 'string']);
$tokenDecoded = new TokenDecoded(['nbf' => 'string']);
$tokenDecoded->encode($key, JWT::ALGORITHM_HS256);
}

Expand All @@ -445,7 +445,7 @@ public function test_encoding_with_wrong_iat_claim_type(): void

$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['iat' => 'string']);
$tokenDecoded = new TokenDecoded(['iat' => 'string']);
$tokenDecoded->encode($key, JWT::ALGORITHM_HS256);
}

Expand All @@ -462,7 +462,7 @@ public function test_encoding_with_wrong_iss_claim_type(): void

$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['iss' => 1]);
$tokenDecoded = new TokenDecoded(['iss' => 1]);
$tokenDecoded->encode($key, JWT::ALGORITHM_HS256);
}

Expand All @@ -479,7 +479,7 @@ public function test_encoding_with_wrong_sub_claim_type(): void

$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['sub' => 1]);
$tokenDecoded = new TokenDecoded(['sub' => 1]);
$tokenDecoded->encode($key, JWT::ALGORITHM_HS256);
}

Expand All @@ -496,7 +496,7 @@ public function test_encoding_with_wrong_aud_claim_type(): void

$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['aud' => 1]);
$tokenDecoded = new TokenDecoded(['aud' => 1]);
$tokenDecoded->encode($key, JWT::ALGORITHM_HS256);
}

Expand All @@ -513,7 +513,7 @@ public function test_encoding_with_wrong_jti_claim_type(): void

$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['jti' => 1]);
$tokenDecoded = new TokenDecoded(['jti' => 1]);
$tokenDecoded->encode($key, JWT::ALGORITHM_HS256);
}

Expand Down Expand Up @@ -594,7 +594,7 @@ public function test_decoding_payload(): void
{
$key = ']V@IaC1%fU,DrVI';

$tokenDecoded = new TokenDecoded(null, ['success' => 1]);
$tokenDecoded = new TokenDecoded(['success' => 1]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$payload = $tokenEncoded->decode()->getPayload();
Expand Down Expand Up @@ -729,7 +729,7 @@ public function test_validation_expiration_date_valid(): void
$exception = false;

try {
$tokenDecoded = new TokenDecoded(null, ['exp' => $timestamp]);
$tokenDecoded = new TokenDecoded(['exp' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256);
Expand All @@ -753,7 +753,7 @@ public function test_validation_with_expiration_date_invalid(): void

$timestamp = time() - 100;

$tokenDecoded = new TokenDecoded(null, ['exp' => $timestamp]);
$tokenDecoded = new TokenDecoded(['exp' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256);
Expand All @@ -771,7 +771,7 @@ public function test_validation_with_expiration_date_invalid_leeway_valid(): voi
$exception = false;

try {
$tokenDecoded = new TokenDecoded(null, ['exp' => $timestamp]);
$tokenDecoded = new TokenDecoded(['exp' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256, 101);
Expand All @@ -795,7 +795,7 @@ public function test_validation_with_expiration_date_invalid_leeway_invalid(): v

$timestamp = time() - 100;

$tokenDecoded = new TokenDecoded(null, ['exp' => $timestamp]);
$tokenDecoded = new TokenDecoded(['exp' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256, 100);
Expand All @@ -813,7 +813,7 @@ public function test_validation_with_not_before_date_valid(): void
$exception = false;

try {
$tokenDecoded = new TokenDecoded(null, ['nbf' => $timestamp]);
$tokenDecoded = new TokenDecoded(['nbf' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256);
Expand All @@ -837,7 +837,7 @@ public function test_validation_with_not_before_date_invalid(): void

$timestamp = time() + 100;

$tokenDecoded = new TokenDecoded(null, ['nbf' => $timestamp]);
$tokenDecoded = new TokenDecoded(['nbf' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256);
Expand All @@ -855,7 +855,7 @@ public function test_validation_with_not_before_date_invalid_leeway_valid(): voi
$exception = false;

try {
$tokenDecoded = new TokenDecoded(null, ['nbf' => $timestamp]);
$tokenDecoded = new TokenDecoded(['nbf' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256, 100);
Expand All @@ -881,7 +881,7 @@ public function test_validation_with_not_before_date_invalid_leeway_invalid(): v

$exception = false;

$tokenDecoded = new TokenDecoded(null, ['nbf' => $timestamp]);
$tokenDecoded = new TokenDecoded(['nbf' => $timestamp]);
$tokenEncoded = $tokenDecoded->encode($key, JWT::ALGORITHM_HS256);

$tokenEncoded->validate($key, JWT::ALGORITHM_HS256, 99);
Expand Down

0 comments on commit 7982ac2

Please sign in to comment.