From 1b96b87e1dd1ab50834e504fe08a281b4cf36599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurice=20Preu=C3=9F=20=28envoyr=29?= Date: Mon, 28 Apr 2025 05:27:06 +0200 Subject: [PATCH] refactored code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurice Preuß (envoyr) --- README.md | 63 ++++++++++++++----- README.stub | 63 ++++++++++++++----- src/Id/AnikeenId.php | 20 +++--- src/Id/Auth/UserProvider.php | 5 ++ src/Id/Billable.php | 2 +- src/Id/Providers/AnikeenIdSsoUserProvider.php | 26 ++++---- 6 files changed, 119 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index dbbdc76..34848d8 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,10 @@ PHP Anikeen ID API Client for Laravel 11+ 1. [Installation](#installation) 2. [Event Listener](#event-listener) 3. [Configuration](#configuration) -4. [Implementing Auth](#implementing-auth) -5. [General](#general) -6. [Examples](#examples) -7. [Documentation](#documentation) -8. [Development](#Development) +4. [General](#general) +5. [Examples](#examples) +6. [Documentation](#documentation) +7. [Development](#Development) ## Installation @@ -56,13 +55,50 @@ You will need to add an entry to the services configuration file so that after c ], ``` +### Registering Middleware + +Append it to the global middleware stack in your application's `bootstrap/app.php` file: + ```php -$middleware->web(append: [ - \Anikeen\Id\Http\Middleware\CreateFreshApiToken::class, -]); +->withMiddleware(function (Middleware $middleware) { + $middleware->web(append: [ + \Anikeen\Id\Http\Middleware\CreateFreshApiToken::class, + ]); +}) ``` -## Implementing Auth +### Implementing Billable + +To implement the `Billable` trait, you need to add the `Billable` trait to your user model. + +```php +use Anikeen\Id\Billable; + +class User extends Authenticatable +{ + use Billable; + + // Your model code... +} +``` + +then, you can use the `Billable` trait methods in your user model. + +### Change the default access token / refresh token field name + +If you access / refresh token fields differs from the default `anikeen_id_access_token` / `anikeen_id_refresh_token`, you can specify the field name in the 'AppServiceProvider' boot method: + +```php +use Anikeen\Id\AnikeenId; + +public function boot(): void +{ + AnikeenId::useAccessTokenField('anikeen_id_access_token'); + AnikeenId::useRefreshTokenField('anikeen_id_refresh_token'); +} +``` + +### Implementing Auth This method should typically be called in the `boot` method of your `AuthServiceProvider` class: @@ -71,12 +107,7 @@ use Anikeen\Id\AnikeenId; use Anikeen\Id\Providers\AnikeenIdSsoUserProvider; use Illuminate\Http\Request; -/** - * Register any authentication / authorization services. - * - * @return void - */ -public function boot() +public function boot(): void { Auth::provider('sso-users', function ($app, array $config) { return new AnikeenIdSsoUserProvider( @@ -84,7 +115,6 @@ public function boot() $app->make(Request::class), $config['model'], $config['fields'] ?? [], - $config['access_token_field'] ?? null ); }); } @@ -119,7 +149,6 @@ reference the provider in the `providers` configuration of your `auth.php` confi 'driver' => 'sso-users', 'model' => App\Models\User::class, 'fields' => ['first_name', 'last_name', 'email'], - 'access_token_field' => 'sso_access_token', ], ], ``` diff --git a/README.stub b/README.stub index 08a5aee..9b77ed2 100644 --- a/README.stub +++ b/README.stub @@ -11,11 +11,10 @@ PHP Anikeen ID API Client for Laravel 11+ 1. [Installation](#installation) 2. [Event Listener](#event-listener) 3. [Configuration](#configuration) -4. [Implementing Auth](#implementing-auth) -5. [General](#general) -6. [Examples](#examples) -7. [Documentation](#documentation) -8. [Development](#Development) +4. [General](#general) +5. [Examples](#examples) +6. [Documentation](#documentation) +7. [Development](#Development) ## Installation @@ -56,13 +55,50 @@ You will need to add an entry to the services configuration file so that after c ], ``` +### Registering Middleware + +Append it to the global middleware stack in your application's `bootstrap/app.php` file: + ```php -$middleware->web(append: [ - \Anikeen\Id\Http\Middleware\CreateFreshApiToken::class, -]); +->withMiddleware(function (Middleware $middleware) { + $middleware->web(append: [ + \Anikeen\Id\Http\Middleware\CreateFreshApiToken::class, + ]); +}) ``` -## Implementing Auth +### Implementing Billable + +To implement the `Billable` trait, you need to add the `Billable` trait to your user model. + +```php +use Anikeen\Id\Billable; + +class User extends Authenticatable +{ + use Billable; + + // Your model code... +} +``` + +then, you can use the `Billable` trait methods in your user model. + +### Change the default access token / refresh token field name + +If you access / refresh token fields differs from the default `anikeen_id_access_token` / `anikeen_id_refresh_token`, you can specify the field name in the 'AppServiceProvider' boot method: + +```php +use Anikeen\Id\AnikeenId; + +public function boot(): void +{ + AnikeenId::useAccessTokenField('anikeen_id_access_token'); + AnikeenId::useRefreshTokenField('anikeen_id_refresh_token'); +} +``` + +### Implementing Auth This method should typically be called in the `boot` method of your `AuthServiceProvider` class: @@ -71,12 +107,7 @@ use Anikeen\Id\AnikeenId; use Anikeen\Id\Providers\AnikeenIdSsoUserProvider; use Illuminate\Http\Request; -/** - * Register any authentication / authorization services. - * - * @return void - */ -public function boot() +public function boot(): void { Auth::provider('sso-users', function ($app, array $config) { return new AnikeenIdSsoUserProvider( @@ -84,7 +115,6 @@ public function boot() $app->make(Request::class), $config['model'], $config['fields'] ?? [], - $config['access_token_field'] ?? null ); }); } @@ -119,7 +149,6 @@ reference the provider in the `providers` configuration of your `auth.php` confi 'driver' => 'sso-users', 'model' => App\Models\User::class, 'fields' => ['first_name', 'last_name', 'email'], - 'access_token_field' => 'sso_access_token', ], ], ``` diff --git a/src/Id/AnikeenId.php b/src/Id/AnikeenId.php index b995bca..4b3947a 100644 --- a/src/Id/AnikeenId.php +++ b/src/Id/AnikeenId.php @@ -54,12 +54,12 @@ class AnikeenId /** * The key for the access token. */ - private static string $accessTokenKey = 'anikeen_id_token'; + private static string $accessTokenField = 'anikeen_id_access_token'; /** * The key for the access token. */ - private static string $refreshTokenKey = 'anikeen_id_refresh_token'; + private static string $refreshTokenField = 'anikeen_id_refresh_token'; /** * Guzzle is used to make http requests. @@ -123,24 +123,24 @@ class AnikeenId self::$baseUrl = $baseUrl; } - public static function useAccessTokenKey(string $accessTokenKey): void + public static function useAccessTokenField(string $accessTokenField): void { - self::$accessTokenKey = $accessTokenKey; + self::$accessTokenField = $accessTokenField; } - public static function getAccessTokenKey(): string + public static function getAccessTokenField(): string { - return self::$accessTokenKey; + return self::$accessTokenField; } - public static function useRefreshTokenKey(string $refreshTokenKey): void + public static function useRefreshTokenField(string $refreshTokenField): void { - self::$refreshTokenKey = $refreshTokenKey; + self::$refreshTokenField = $refreshTokenField; } - public static function getRefreshTokenKey(): string + public static function getRefreshTokenField(): string { - return self::$refreshTokenKey; + return self::$refreshTokenField; } /** diff --git a/src/Id/Auth/UserProvider.php b/src/Id/Auth/UserProvider.php index e2ccb00..8583185 100644 --- a/src/Id/Auth/UserProvider.php +++ b/src/Id/Auth/UserProvider.php @@ -62,4 +62,9 @@ class UserProvider implements Base { return $this->providerName; } + + public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false) + { + // TODO: Implement rehashPasswordIfRequired() method. + } } diff --git a/src/Id/Billable.php b/src/Id/Billable.php index e70b977..52e4b03 100644 --- a/src/Id/Billable.php +++ b/src/Id/Billable.php @@ -51,7 +51,7 @@ trait Billable protected function request(string $method, string $path, null|array $payload = null, array $parameters = [], Paginator $paginator = null): Result { $anikeenId = new AnikeenId(); - $anikeenId->withToken($this->{AnikeenId::getAccessTokenKey()}); + $anikeenId->withToken($this->{AnikeenId::getAccessTokenField()}); return $anikeenId->request($method, $path, $payload, $parameters, $paginator); } diff --git a/src/Id/Providers/AnikeenIdSsoUserProvider.php b/src/Id/Providers/AnikeenIdSsoUserProvider.php index 1ddd66f..b4fe5e0 100644 --- a/src/Id/Providers/AnikeenIdSsoUserProvider.php +++ b/src/Id/Providers/AnikeenIdSsoUserProvider.php @@ -13,28 +13,19 @@ use Illuminate\Support\Arr; class AnikeenIdSsoUserProvider implements UserProvider { - private AnikeenId $anikeenId; private ?string $accessTokenField = null; - private array $fields; - private string $model; - private Request $request; public function __construct( - AnikeenId $anikeenId, - Request $request, - string $model, - array $fields, - ?string $accessTokenField = null + private AnikeenId $anikeenId, + private Request $request, + private string $model, + private array $fields ) { - $this->request = $request; - $this->model = $model; - $this->fields = $fields; - $this->accessTokenField = $accessTokenField; - $this->anikeenId = $anikeenId; + $this->accessTokenField = AnikeenId::getAccessTokenField(); } - public function retrieveById(mixed $identifier): Builder|Model|null + public function retrieveById(mixed $identifier): ?Authenticatable { $model = $this->createModel(); $token = $this->request->bearerToken(); @@ -114,4 +105,9 @@ class AnikeenIdSsoUserProvider implements UserProvider { return false; } + + public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false) + { + // TODO: Implement rehashPasswordIfRequired() method. + } }