https://github.com/sroehrl/neoan3-stateless
neoan3 app handling stateless authentication flow using JWT
https://github.com/sroehrl/neoan3-stateless
hacktoberfest jwt
Last synced: 12 months ago
JSON representation
neoan3 app handling stateless authentication flow using JWT
- Host: GitHub
- URL: https://github.com/sroehrl/neoan3-stateless
- Owner: sroehrl
- License: mit
- Created: 2019-06-01T18:57:21.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-04-23T23:04:09.000Z (about 2 years ago)
- Last Synced: 2024-04-24T23:51:49.417Z (about 2 years ago)
- Topics: hacktoberfest, jwt
- Language: PHP
- Homepage:
- Size: 60.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://codeclimate.com/github/sroehrl/neoan3-stateless/maintainability)
[](https://codeclimate.com/github/sroehrl/neoan3-stateless/test_coverage)
[](https://travis-ci.com/sroehrl/neoan3-stateless)
# PHP stateless JWT authentication
Easy implementation of JWT authentication & handling in PHP.
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Methods](#methods)
## Installation
`composer require neoan3-apps/stateless`
## Quick Start
```PHP
// static
Neoan3\Apps\Stateless::setSecret('My-super-secure-Key');
// or as object
// (method names are the same as static calls)
$stateless = new Neoan3\Apps\StatelessOOP('my-secure-key');
// create JWT
$jti = 'someId';
$scope = ['read', 'write'];
$payload = ['additional'=>'info']; // optional
$jwt = Neoan3\Apps\Stateless::assign($jti, $scope, $payload);
// validate JWT
try{
$decrypted = Neoan3\Apps\Stateless::validate();
$user = $decrypted['jti'];
} catch(Exception $e) {
die('ups');
}
```
## Methods
### setAuthorization($jwt)
If this method is not used, _Stateless_ will read the Authorization from the $_SERVER variable "HTTP_AUTHORIZATION"
and the following format "bearer _token_"
### setCustomException($class)
Can be used to trigger a custom exception when encountering validation errors.
### setSecret($secret)
Key used for the HS256 algorithm (decryption/encryption/signing). Make sure a key is set prior to any other interactions.
### validate()
Returns the decoded JWT or throws an Exception
### restrict($scope = [])
Accepts a string or an array. Same as _validate_, but additionally checks if the right kind of scope is present.
### setExpiration($time | null)
Accepts strtotime-compatible expression or epoch-stamp.
### assign($jti, $scope, $payload = [])
Generates a JWT.