refactored code

Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
This commit is contained in:
2025-04-28 05:27:06 +02:00
parent 7f908f4e6a
commit 1b96b87e1d
6 changed files with 119 additions and 60 deletions

View File

@@ -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',
],
],
```

View File

@@ -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',
],
],
```

View File

@@ -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;
}
/**

View File

@@ -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.
}
}

View File

@@ -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);
}

View File

@@ -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.
}
}