From f777bb2d700ad46dcf117e12b4ec6d521bfb1a12 Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:20:06 -0400 Subject: [PATCH 01/15] testing github actions --- .github/workflows/test.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..b223bbf --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,15 @@ +name: test + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - uses: php-actions/composer@master # or alternative dependency management + - name: phpspec + uses: php-actions/phpspec@master + with: + config: phpspec.yml # or wherever your config file is From d6fbf535cf187426220e31da666fb7b95cf22f8a Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:22:39 -0400 Subject: [PATCH 02/15] diff php version --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b223bbf..4268516 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ on: [push] jobs: build: - runs-on: ubuntu-latest + runs-on: php:7.4 steps: - uses: actions/checkout@v1 From 079b21a51303a66edd83fd278d1d4a4c9dabdea1 Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:24:33 -0400 Subject: [PATCH 03/15] try dockerfile version --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4268516..10b0215 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ on: [push] jobs: build: - runs-on: php:7.4 + runs-on: php:7.3-cli steps: - uses: actions/checkout@v1 From 36bfea153b43fa0ba3a08ac05952b3c6c3acdb1f Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:30:57 -0400 Subject: [PATCH 04/15] try with matrix --- .github/workflows/test.yml | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 10b0215..2c12f9e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,12 +4,28 @@ on: [push] jobs: build: - runs-on: php:7.3-cli + name: PHP ${{ matrix.php }} + + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + php: [ 7.1, 7.2, 7.3 ] + os: [ ubuntu-latest ] steps: - - uses: actions/checkout@v1 - - uses: php-actions/composer@master # or alternative dependency management - - name: phpspec + - name: Checkout + uses: actions/checkout@master + + - name: Setup PHP + uses: shivammathur/setup-php@master + with: + php-version: ${{ matrix.php }} + + - uses: php-actions/composer@master + + - name: PHP spec uses: php-actions/phpspec@master with: - config: phpspec.yml # or wherever your config file is + config: phpspec.yml From e63ec6f84bd552615f614c52ccaaa42d2fdb937a Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:34:06 -0400 Subject: [PATCH 05/15] added php version to composer step --- .github/workflows/test.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2c12f9e..7c12b4f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,7 +23,10 @@ jobs: with: php-version: ${{ matrix.php }} - - uses: php-actions/composer@master + - name: Install Dependencies + uses: php-actions/composer@master + with: + php_version: ${{ matrix.php }} - name: PHP spec uses: php-actions/phpspec@master From 65628fe85104f53c8d3274014b79f2792cb551cd Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:37:24 -0400 Subject: [PATCH 06/15] not sure we have built this for 7.1 or 7.2 so will work on that later --- .github/workflows/test.yml | 2 +- spec/JwtTokenSpec.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7c12b4f..7b3ffb9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ 7.1, 7.2, 7.3 ] + php: [ 7.3 ] os: [ ubuntu-latest ] steps: diff --git a/spec/JwtTokenSpec.php b/spec/JwtTokenSpec.php index 2e17f80..5fc9b1b 100644 --- a/spec/JwtTokenSpec.php +++ b/spec/JwtTokenSpec.php @@ -2,6 +2,7 @@ namespace spec\GenTux\Jwt; +use Exception; use Prophecy\Argument; use GenTux\Jwt\JwtToken; use PhpSpec\ObjectBehavior; @@ -26,7 +27,7 @@ public function it_gets_and_sets_the_current_jwt_token() $this->shouldThrow(NoTokenException::class)->during('token'); $this->setToken('foo_token')->shouldReturn($this); - $this->token()->shouldReturn('foo_token'); + $this->token()->shouldReturn('fake-messed-up-token'); } public function it_gets_and_sets_the_jwt_secret() From e950ad7dda328f42737f5c77df22eb22b0dc31bb Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:38:51 -0400 Subject: [PATCH 07/15] fixed broken test test --- spec/JwtTokenSpec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/JwtTokenSpec.php b/spec/JwtTokenSpec.php index 5fc9b1b..08549cc 100644 --- a/spec/JwtTokenSpec.php +++ b/spec/JwtTokenSpec.php @@ -27,7 +27,7 @@ public function it_gets_and_sets_the_current_jwt_token() $this->shouldThrow(NoTokenException::class)->during('token'); $this->setToken('foo_token')->shouldReturn($this); - $this->token()->shouldReturn('fake-messed-up-token'); + $this->token()->shouldReturn('foo_token'); } public function it_gets_and_sets_the_jwt_secret() From b812add135c337759ff5a9d36ee40879145a5d6b Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:39:16 -0400 Subject: [PATCH 08/15] removed travis spec --- .travis.yml | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index df209b9..0000000 --- a/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: php -php: - - '7.2' -install: composer install -script: ./vendor/bin/phpspec run From 1b62b7cfbe8f93cadb06d93a53a935b10c02a7be Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:42:35 -0400 Subject: [PATCH 09/15] i dont think we need both of these --- .github/workflows/test.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7b3ffb9..f306831 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,11 +23,6 @@ jobs: with: php-version: ${{ matrix.php }} - - name: Install Dependencies - uses: php-actions/composer@master - with: - php_version: ${{ matrix.php }} - - name: PHP spec uses: php-actions/phpspec@master with: From 150ee5784f54ba5e8d0df8fb777b743b01d6e81d Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:45:41 -0400 Subject: [PATCH 10/15] removing this caused some messed up things to happen, going to revisit this later --- .github/workflows/test.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f306831..7b3ffb9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,6 +23,11 @@ jobs: with: php-version: ${{ matrix.php }} + - name: Install Dependencies + uses: php-actions/composer@master + with: + php_version: ${{ matrix.php }} + - name: PHP spec uses: php-actions/phpspec@master with: From 02fb15a9aaf51c847da706365bf7b103d8dd72e5 Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:48:56 -0400 Subject: [PATCH 11/15] updated readme badge --- readme.md | 130 +++++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/readme.md b/readme.md index c993fb0..372d897 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # JWT Artisan -[![Build Status](https://travis-ci.org/generationtux/jwt-artisan.svg?branch=master)](https://travis-ci.org/generationtux/jwt-artisan) +![Build Test Status](https://github.com/generationtux/jwt-artisan/actions/workflows/register(GenTux\Jwt\Support\LumenServiceProvider::class); ``` - ## Configure All configuration for this package can be set using environment variables. The reason for using environment variables instead of config files is so that it can be integrated with both Laravel & Lumen as easily as possible. See the table below for the available config options and their defaults. -| Config | Default | Description | -| ------------ | ------- | ---------------------------------------------------------------- | -| `JWT_SECRET` | *null* | The secret key that will be used for sigining/validating tokens. | -| `JWT_ALGO` | *HS256* | The algorithm to use for sigining tokens. | -| `JWT_LEEWAY` | *0* | Seconds of leeway for validating timestamps to account for time differences between systems | -| `JWT_INPUT` | *token* | By default we will look for the token in the `Authorization` header. If it's not found there, then this value will be used to search the sent input from the request to find the token. | -| `JWT_HEADER` | *Authorization* | By default the `Authorization` header key is used. This can be overridden with this value. | +| Config | Default | Description | +| ------------ | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `JWT_SECRET` | _null_ | The secret key that will be used for sigining/validating tokens. | +| `JWT_ALGO` | _HS256_ | The algorithm to use for sigining tokens. | +| `JWT_LEEWAY` | _0_ | Seconds of leeway for validating timestamps to account for time differences between systems | +| `JWT_INPUT` | _token_ | By default we will look for the token in the `Authorization` header. If it's not found there, then this value will be used to search the sent input from the request to find the token. | +| `JWT_HEADER` | _Authorization_ | By default the `Authorization` header key is used. This can be overridden with this value. | If you're using the `JwtExceptionHandler` to handle exceptions, these environment variables can be set to customize the error messages. -*(see below for information on using the exception handler)* - -| Config | Default | Description | -| ------------------------ | --------------------------------------------------------------- | ------------------------------------------------------------------ | -| `JWT_MESSAGE_ERROR` | *There was an error while validating the authorization token.* | `500` A general error occured while working with the token. | -| `JWT_MESSAGE_INVALID` | *Authorization token is not valid.* | `401` The provided token is invalid in some way: expired, mismatched signature, etc. | -| `JWT_MESSAGE_NOTOKEN` | *Authorization token is required.* | `401` There was no token found with the request. | -| `JWT_MESSAGE_NOSECRET` | *No JWT secret defined.* | `500` Unable to find the JWT secret for validating/signing tokens. | +_(see below for information on using the exception handler)_ +| Config | Default | Description | +| ---------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------ | +| `JWT_MESSAGE_ERROR` | _There was an error while validating the authorization token._ | `500` A general error occured while working with the token. | +| `JWT_MESSAGE_INVALID` | _Authorization token is not valid._ | `401` The provided token is invalid in some way: expired, mismatched signature, etc. | +| `JWT_MESSAGE_NOTOKEN` | _Authorization token is required._ | `401` There was no token found with the request. | +| `JWT_MESSAGE_NOSECRET` | _No JWT secret defined._ | `500` Unable to find the JWT secret for validating/signing tokens. | ## Working with Tokens @@ -92,13 +90,13 @@ use GenTux\Jwt\JwtToken; class TokensController extends controller { - public function create(JwtToken $jwt) - { - $payload = ['exp' => time() + 7200]; // expire in 2 hours - $token = $jwt->createToken($payload); // new instance of JwtToken + public function create(JwtToken $jwt) + { + $payload = ["exp" => time() + 7200]; // expire in 2 hours + $token = $jwt->createToken($payload); // new instance of JwtToken - return (string) $token; - } + return (string) $token; + } } ``` @@ -111,16 +109,16 @@ use GenTux\Jwt\JwtPayloadInterface; class User extends Model implements JwtPayloadInterface { - public function getPayload() - { - return [ - 'sub' => $this->id, - 'exp' => time() + 7200, - 'context' => [ - 'email' => $this->email - ] - ]; - } + public function getPayload() + { + return [ + "sub" => $this->id, + "exp" => time() + 7200, + "context" => [ + "email" => $this->email, + ], + ]; + } } ``` @@ -133,13 +131,13 @@ use GenTux\Jwt\JwtToken; class TokensController extends controller { - public function create(JwtToken $jwt) - { - $user = User::find(1); - $token = $jwt->createToken($user); + public function create(JwtToken $jwt) + { + $user = User::find(1); + $token = $jwt->createToken($user); - return $token->payload(); // ['sub' => 1, exp => '...', 'context' => ...] - } + return $token->payload(); // ['sub' => 1, exp => '...', 'context' => ...] + } } ``` @@ -163,14 +161,17 @@ The easiest way to validate a request with a JWT token is to use the provided mi 'jwt'], function() { - Route::post('/foo', 'FooController'); +Route::group(["middleware" => "jwt"], function () { + Route::post("/foo", "FooController"); }); // Lumen -$app->group(['middleware' => 'jwt', 'namespace' => 'App\Http\Controllers'], function($app) { - $app->post('/foo', 'FooController'); -}); +$app->group( + ["middleware" => "jwt", "namespace" => "App\Http\Controllers"], + function ($app) { + $app->post("/foo", "FooController"); + } +); ``` When a token is invalid, `GenTux\Jwt\Exceptions\InvalidTokenException` will be thrown. If no token is provided, @@ -187,12 +188,12 @@ use GenTux\Jwt\GetsJwtToken; class CreateUser extends FormRequest { - use GetsJwtToken; + use GetsJwtToken; - public function authorize() - { - return $this->jwtToken()->validate(); - } + public function authorize() + { + return $this->jwtToken()->validate(); + } } ``` @@ -229,15 +230,14 @@ use GenTux\Jwt\GetsJwtToken; class TokenService { + use GetsJwtToken; - use GetsJwtToken; - - public function getExpires() - { - $payload = $this->jwtPayload(); // shortcut for $this->jwtToken()->payload() + public function getExpires() + { + $payload = $this->jwtPayload(); // shortcut for $this->jwtToken()->payload() - return $payload['exp']; - } + return $payload["exp"]; + } } ``` @@ -250,17 +250,17 @@ use GenTux\Jwt\GetsJwtToken; class TokenService { - use GetsJwtToken; + use GetsJwtToken; - public function getData() - { - // ['exp' => '123', 'context' => ['foo' => 'bar']] + public function getData() + { + // ['exp' => '123', 'context' => ['foo' => 'bar']] - $token = $this->jwtToken(); - $token->payload('exp'); // 123 - $token->payload('context.foo'); // bar - $token->payload('context.baz'); // null - } + $token = $this->jwtToken(); + $token->payload("exp"); // 123 + $token->payload("context.foo"); // bar + $token->payload("context.baz"); // null + } } ``` From d080f862d6bb89a09bc74f12cf7a4957170788ff Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:50:38 -0400 Subject: [PATCH 12/15] fixed typo in name --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 372d897..5687bdf 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # JWT Artisan -![Build Test Status](https://github.com/generationtux/jwt-artisan/actions/workflows/ Date: Wed, 11 May 2022 14:53:28 -0400 Subject: [PATCH 13/15] badge shows status for that branch --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 5687bdf..8d0b261 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # JWT Artisan -![Build Test Status](https://github.com/generationtux/jwt-artisan/actions/workflows/test.yml/badge.svg) +![Build Test Status](https://github.com/generationtux/jwt-artisan/actions/workflows/test.yml/badge.svg?event=push) ## Token auth for Laravel and Lumen web artisans From 1d37b465cde6485da8580fa3c969342b5d48305e Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 14:56:23 -0400 Subject: [PATCH 14/15] changed events --- .github/workflows/test.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7b3ffb9..856cd5c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,8 @@ name: test -on: [push] +on: + pull_request: + types: [opened, reopened, review_requested] jobs: build: From 9ffc543965b5cfbd15c913a4be6fe76ddedeeb46 Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Wed, 11 May 2022 15:22:40 -0400 Subject: [PATCH 15/15] push is just simpler --- .github/workflows/test.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 856cd5c..7b3ffb9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,8 +1,6 @@ name: test -on: - pull_request: - types: [opened, reopened, review_requested] +on: [push] jobs: build: