Skip to content

Commit

Permalink
fixed wrong namespace and added more types
Browse files Browse the repository at this point in the history
  • Loading branch information
rcerljenko committed Dec 14, 2021
1 parent 55f0303 commit 707eca3
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 87 deletions.
7 changes: 4 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

$finder = Finder::create()
->in([
'config',
'src'
__DIR__ . '/config',
__DIR__ . '/src'
])
->append([
__FILE__
Expand All @@ -19,6 +19,7 @@
->setIndent("\t")
->setLineEnding("\n")
->setRules([
'align_multiline_comment' => ['comment_type' => 'all_multiline'],
'array_indentation' => true,
'array_syntax' => true,
'blank_line_after_namespace' => true,
Expand Down Expand Up @@ -117,7 +118,7 @@
'no_alias_language_construct_call' => true,
'clean_namespace' => true,
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
'types_spaces' => ['space' => 'single'],
'types_spaces' => true,
'empty_loop_body' => true,
'declare_parentheses' => true,
'assign_null_coalescing_to_coalesce_equal' => true,
Expand Down
1 change: 1 addition & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"recommendations": [
"junstyle.php-cs-fixer",
"bmewburn.vscode-intelephense-client"
]
}
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@
"html.validate.styles": true,
"php.suggest.basic": true,
"php.validate.enable": true,
"intelephense.format.enable": false
"intelephense.format.enable": false,
"php-cs-fixer.executablePath": "${workspaceFolder}/vendor/bin/php-cs-fixer",
"php-cs-fixer.executablePathWindows": "${workspaceFolder}\\vendor\\bin\\php-cs-fixer.bat",
"php-cs-fixer.onsave": true,
"php-cs-fixer.rules": "",
"php-cs-fixer.allowRisky": false,
"php-cs-fixer.autoFixByBracket": false,
"php-cs-fixer.autoFixBySemicolon": false,
"php-cs-fixer.formatHtml": false,
"php-cs-fixer.documentFormattingProvider": true
}
106 changes: 53 additions & 53 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
{
"name": "rcerljenko/laravel-paseto",
"version": "1.0.4",
"description": "Simple PASETO Auth for Laravel PHP Framework",
"homepage": "https://github.com/rcerljenko/laravel-paseto",
"authors": [
{
"name": "Ricardo Cerljenko",
"email": "ricardo.cerljenko@gmail.com",
"homepage": "https://github.com/rcerljenko"
}
],
"support": {
"email": "ricardo.cerljenko@gmail.com"
},
"require": {
"php": "^7.4|^8.0",
"illuminate/auth": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/support": "^8.0",
"paragonie/paseto": "^2.1"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.8",
"friendsofphp/php-cs-fixer": "^3.2"
},
"autoload": {
"psr-4": {
"RCerljenko\\LaravelJwt\\": "src/"
}
},
"extra": {
"hooks": {
"pre-commit": "./pre-commit.sh"
},
"laravel": {
"providers": [
"RCerljenko\\LaravelPaseto\\LaravelPasetoServiceProvider"
]
}
},
"scripts": {
"post-autoload-dump": "cghooks update",
"check-format": "php-cs-fixer fix --dry-run",
"format": "php-cs-fixer fix"
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
"name": "rcerljenko/laravel-paseto",
"version": "1.1.0",
"description": "Simple PASETO Auth for Laravel PHP Framework",
"homepage": "https://github.com/rcerljenko/laravel-paseto",
"authors": [
{
"name": "Ricardo Cerljenko",
"email": "ricardo.cerljenko@gmail.com",
"homepage": "https://github.com/rcerljenko"
}
],
"support": {
"email": "ricardo.cerljenko@gmail.com"
},
"require": {
"php": "^7.4|^8.0",
"illuminate/auth": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/support": "^8.0",
"paragonie/paseto": "^2.2"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.8",
"friendsofphp/php-cs-fixer": "^3.4"
},
"autoload": {
"psr-4": {
"RCerljenko\\LaravelJwt\\": "src/"
}
},
"extra": {
"hooks": {
"pre-commit": "./pre-commit.sh"
},
"laravel": {
"providers": [
"RCerljenko\\LaravelPaseto\\LaravelPasetoServiceProvider"
]
}
},
"scripts": {
"post-autoload-dump": "cghooks update",
"check-format": "php-cs-fixer fix --dry-run",
"format": "php-cs-fixer fix"
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
10 changes: 5 additions & 5 deletions config/paseto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Used for token encoding/decoding.
| Has to be a 32 byte long random string.
|
*/
*/

'secret-key' => env('PASETO_SECRET_KEY'),

Expand All @@ -20,7 +20,7 @@
|
| Token expiration time defined in minutes. If null then token has no expiration at all (lifetime token).
|
*/
*/

'expiration' => env('PASETO_EXPIRATION', 60 * 24),

Expand All @@ -31,7 +31,7 @@
|
| Sets the token issuer claim.
|
*/
*/

'issuer' => env('APP_URL'),

Expand All @@ -42,7 +42,7 @@
|
| Sets the token audience claim.
|
*/
*/

'audience' => env('APP_URL'),

Expand All @@ -53,7 +53,7 @@
|
| These claims will be in all tokens.
|
*/
*/

'claims' => [
// 'foo' => 'bar'
Expand Down
23 changes: 10 additions & 13 deletions src/Guard/PasetoGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Auth\Guard;
use RCerljenko\LaravelPaseto\Paseto;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable;

class PasetoGuard implements Guard
{
Expand All @@ -18,19 +19,17 @@ public function __construct(UserProvider $provider)

/**
* Get the currently authenticated user.
*
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
public function user(): ?Authenticatable
{
if ($this->user && !app()->runningUnitTests()) {
if ($this->hasUser() && !app()->runningUnitTests()) {
return $this->user;
}

$decoded = $this->getTokenPayload();

if (!$decoded) {
return;
return null;
}

$this->user = $this->getProvider()->retrieveById($decoded['jti']);
Expand All @@ -40,15 +39,13 @@ public function user()

/**
* Validate a user's credentials.
*
* @return bool
*/
public function validate(array $credentials = [])
public function validate(array $credentials = []): bool
{
return !empty($this->attempt($credentials));
}

public function attempt(array $credentials = [])
public function attempt(array $credentials = []): ?Authenticatable
{
$provider = $this->getProvider();

Expand All @@ -58,23 +55,23 @@ public function attempt(array $credentials = [])
return $this->user;
}

public function getTokenPayload()
public function getTokenPayload(): ?array
{
$token = $this->getTokenFromRequest();

if (!$token) {
return;
return null;
}

$paseto = new Paseto;

return $paseto->decodeToken($token)->getClaims();
}

private function getTokenFromRequest()
private function getTokenFromRequest(): ?string
{
$request = request();

return $request ? ($request->bearerToken() ?? $request->token) : null;
return $request->bearerToken() ?? $request->token;
}
}
8 changes: 4 additions & 4 deletions src/LaravelPasetoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

class LaravelPasetoServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
$this->publishConfig();

$this->extendAuthGuard();
}

public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/paseto.php', 'paseto');
}

private function publishConfig()
private function publishConfig(): void
{
$this->publishes([
__DIR__ . '/../config/paseto.php' => config_path('paseto.php'),
], 'config');
}

private function extendAuthGuard()
private function extendAuthGuard(): void
{
auth()->extend('paseto', function ($app, $name, array $config) {
return new PasetoGuard(auth()->createUserProvider($config['provider']));
Expand Down
5 changes: 3 additions & 2 deletions src/Paseto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ParagonIE\Paseto\Parser;
use ParagonIE\Paseto\Builder;
use ParagonIE\Paseto\Purpose;
use ParagonIE\Paseto\JsonToken;
use ParagonIE\Paseto\Rules\ValidAt;
use ParagonIE\Paseto\Rules\IssuedBy;
use ParagonIE\Paseto\Rules\NotExpired;
Expand All @@ -23,7 +24,7 @@ public function __construct(?string $secretKey = null)
$this->sharedKey = new SymmetricKey($secretKey ?? config('paseto.secret-key'));
}

public function encodeToken(object $user, array $config = [])
public function encodeToken(object $user, array $config = []): string
{
$nbf = $config['valid_from'] ?? $user->getJwtValidFromTime();
$exp = $config['valid_until'] ?? $user->getJwtValidUntilTime();
Expand All @@ -44,7 +45,7 @@ public function encodeToken(object $user, array $config = [])
->toString();
}

public function decodeToken(string $token)
public function decodeToken(string $token): JsonToken
{
$parser = new Parser;

Expand Down
14 changes: 8 additions & 6 deletions src/Traits/HasPaseto.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
<?php

namespace RCerljenko\LaravelJwt\Traits;
namespace RCerljenko\LaravelPaseto\Traits;

use Illuminate\Support\Carbon;
use RCerljenko\LaravelPaseto\Paseto;

trait HasPaseto
{
public function getJwtId()
public function getJwtId(): string
{
return $this->getKey();
}

public function getJwtValidFromTime()
public function getJwtValidFromTime(): ?Carbon
{
return null;
}

public function getJwtValidUntilTime()
public function getJwtValidUntilTime(): ?Carbon
{
$expiration = config('paseto.expiration');

return $expiration ? now()->addMinutes($expiration) : null;
}

public function getJwtCustomClaims()
public function getJwtCustomClaims(): array
{
return [];
}

public function token(array $config = [])
public function token(array $config = []): string
{
$paseto = new Paseto;

Expand Down

0 comments on commit 707eca3

Please sign in to comment.