https://github.com/generationtux/jwt-artisan
JWT auth for Laravel and Lumen
https://github.com/generationtux/jwt-artisan
Last synced: about 1 year ago
JSON representation
JWT auth for Laravel and Lumen
- Host: GitHub
- URL: https://github.com/generationtux/jwt-artisan
- Owner: generationtux
- License: mit
- Created: 2015-09-29T19:55:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-03-22T17:37:47.000Z (about 3 years ago)
- Last Synced: 2025-03-29T17:11:37.162Z (about 1 year ago)
- Language: PHP
- Size: 106 KB
- Stars: 141
- Watchers: 22
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-lumen - Jwt Artisan - JWT auth for Laravel and Lumen (Packages and Middleware / Videos)
README
# JWT Artisan

## Token auth for Laravel and Lumen web artisans
[JWT](http://jwt.io/) is a great solution for authenticating API requests between various services. This package
makes working with JWT super easy for both [Laravel](http://laravel.com/) and [Lumen](http://lumen.laravel.com/).
### Why JWT?
Because you have

That need to authenticate with each other so you can turn away bad requests like

Which is why JWT makes you feel like

### Contents
- [Setup](#setup)
- [Configure](#configure)
- [Working with Tokens](#working-with-tokens)
## Setup
Install the package using composer
$ composer require generationtux/jwt-artisan
Add the appropriate service provider for Laravel/Lumen
```php
// Laravel
// config/app.php
'providers' => [
...
GenTux\Jwt\Support\LaravelServiceProvider::class,
]
// Lumen
// bootstrap/app.php
$app->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. |
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. |
## Working with Tokens
- [Creating Tokens](#creating-tokens)
- [Validating Tokens](#validating-tokens)
- [Payloads](#payloads)
- [Handling Errors](#handling-errors)
### Creating Tokens
Inject an instance of `GenTux\Jwt\JwtToken` into your controller or other service to create new tokens.
```php
time() + 7200]; // expire in 2 hours
$token = $jwt->createToken($payload); // new instance of JwtToken
return (string) $token;
}
}
```
Implement `GenTux\Jwt\JwtPayloadInterface` to pass other objects to `createToken` for a more dynamic payload.
```php
$this->id,
"exp" => time() + 7200,
"context" => [
"email" => $this->email,
],
];
}
}
```
Then simply pass that object when creating the token
```php
createToken($user);
return $token->payload(); // ['sub' => 1, exp => '...', 'context' => ...]
}
}
```
You can set a specific `secret` and `algorithm` to use if necessary
```php
public function create(JwtToken $jwt)
{
return $jwt
->setSecret('secret_123')
->setAlgorithm('custom')
->createToken('[...]');
}
```
### Validating Tokens
The easiest way to validate a request with a JWT token is to use the provided middleware.
```php
"jwt"], function () {
Route::post("/foo", "FooController");
});
// Lumen
$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,
then `GenTux\Jwt\Exceptions\NoTokenException` will be thrown.
To manually validate the token, you can get tokens in any class using the trait `GenTux\Jwt\GetsJwtToken`.
For example, in a **Laravel** request object
```php
jwtToken()->validate();
}
}
```
Or in a controller for **Lumen**
```php
jwtToken()->validate()) {
return redirect('/nope');
}
...
}
}
```
### Payloads
Once you have the token, working with the payload is easy.
```php
jwtPayload(); // shortcut for $this->jwtToken()->payload()
return $payload["exp"];
}
}
```
The `payload` method for JwtToken accepts a `path` that can be used to get specific data from the payload.
```php
'123', 'context' => ['foo' => 'bar']]
$token = $this->jwtToken();
$token->payload("exp"); // 123
$token->payload("context.foo"); // bar
$token->payload("context.baz"); // null
}
}
```
### Handling Errors
This package can handle JWT exceptions out of the box if you would like. It will take all JWT exceptions
and return JSON error responses. If you would like to implements your own error handling, you can look
at `GenTux\Jwt\Exceptions\JwtExceptionHandler` for an example.
To implement, add the following inside of `app/Exceptions/Handler.php`
```php
handleJwtException($e);
...
}
}
```