mirror of
https://github.com/anikeen-com/id.git
synced 2026-03-17 15:46:17 +00:00
first commit
This commit is contained in:
80
src/Id/Providers/AnikeenIdServiceProvider.php
Normal file
80
src/Id/Providers/AnikeenIdServiceProvider.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Providers;
|
||||
|
||||
use Anikeen\Id\AnikeenId;
|
||||
use Anikeen\Id\Auth\TokenGuard;
|
||||
use Anikeen\Id\Auth\UserProvider;
|
||||
use Anikeen\Id\Contracts;
|
||||
use Anikeen\Id\Helpers\JwtParser;
|
||||
use Anikeen\Id\Repository;
|
||||
use Illuminate\Auth\RequestGuard;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AnikeenIdServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->publishes([
|
||||
dirname(__DIR__, 3) . '/config/anikeen-id.php' => config_path('anikeen-id.php'),
|
||||
], 'config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->mergeConfigFrom(dirname(__DIR__, 3) . '/config/anikeen-id.php', 'anikeen-id');
|
||||
$this->app->singleton(Contracts\AppTokenRepository::class, Repository\AppTokenRepository::class);
|
||||
$this->app->singleton(AnikeenId::class, function () {
|
||||
return new AnikeenId;
|
||||
});
|
||||
|
||||
$this->registerGuard();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the token guard.
|
||||
*/
|
||||
protected function registerGuard(): void
|
||||
{
|
||||
Auth::resolved(function ($auth) {
|
||||
$auth->extend('anikeen-id', function ($app, $name, array $config) {
|
||||
return tap($this->makeGuard($config), function ($guard) {
|
||||
$this->app->refresh('request', $guard, 'setRequest');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an instance of the token guard.
|
||||
*/
|
||||
protected function makeGuard(array $config): RequestGuard
|
||||
{
|
||||
return new RequestGuard(function ($request) use ($config) {
|
||||
return (new TokenGuard(
|
||||
new UserProvider(Auth::createUserProvider($config['provider']), $config['provider']),
|
||||
$this->app->make('encrypter'),
|
||||
$this->app->make(JwtParser::class)
|
||||
))->user($request);
|
||||
}, $this->app['request']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [
|
||||
AnikeenId::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
117
src/Id/Providers/AnikeenIdSsoUserProvider.php
Normal file
117
src/Id/Providers/AnikeenIdSsoUserProvider.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Anikeen\Id\Providers;
|
||||
|
||||
use Anikeen\Id\AnikeenId;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
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
|
||||
)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->model = $model;
|
||||
$this->fields = $fields;
|
||||
$this->accessTokenField = $accessTokenField;
|
||||
$this->anikeenId = $anikeenId;
|
||||
}
|
||||
|
||||
public function retrieveById(mixed $identifier): Builder|Model|null
|
||||
{
|
||||
$model = $this->createModel();
|
||||
$token = $this->request->bearerToken();
|
||||
|
||||
$user = $this->newModelQuery($model)
|
||||
->where($model->getAuthIdentifierName(), $identifier)
|
||||
->first();
|
||||
|
||||
// Return user when found
|
||||
if ($user) {
|
||||
// Update access token when updated
|
||||
if ($this->accessTokenField) {
|
||||
$user[$this->accessTokenField] = $token;
|
||||
|
||||
if ($user->isDirty()) {
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
// Create new user
|
||||
$this->anikeenId->setToken($token);
|
||||
$result = $this->anikeenId->getAuthedUser();
|
||||
|
||||
if (!$result->success()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$attributes = Arr::only((array)$result->data(), $this->fields);
|
||||
$attributes[$model->getAuthIdentifierName()] = $result->data->id;
|
||||
|
||||
if ($this->accessTokenField) {
|
||||
$attributes[$this->accessTokenField] = $token;
|
||||
}
|
||||
|
||||
return $this->newModelQuery($model)->create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the model.
|
||||
*/
|
||||
public function createModel(): Model
|
||||
{
|
||||
$class = '\\' . ltrim($this->model, '\\');
|
||||
|
||||
return new $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new query builder for the model instance.
|
||||
*/
|
||||
protected function newModelQuery(?Model $model = null): Builder
|
||||
{
|
||||
return is_null($model)
|
||||
? $this->createModel()->newQuery()
|
||||
: $model->newQuery();
|
||||
}
|
||||
|
||||
public function retrieveByToken($identifier, $token)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function updateRememberToken(Authenticatable $user, $token)
|
||||
{
|
||||
// void
|
||||
}
|
||||
|
||||
public function retrieveByCredentials(array $credentials)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function validateCredentials(Authenticatable $user, array $credentials): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user