Add sso user provider

This commit is contained in:
2022-01-02 14:04:01 +01:00
parent 3760e312cd
commit 8f962d1e06
3 changed files with 123 additions and 6 deletions

View File

@@ -0,0 +1,117 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Providers;
use GhostZero\BitinflowAccounts\BitinflowAccounts;
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 BitinflowAccountsSsoUserProvider implements UserProvider
{
private $bitinflowAccounts;
private $accessTokenField = null;
private $fields;
private $model;
private $request;
public function __construct(
BitinflowAccounts $bitinflowAccounts,
Request $request,
string $model,
array $fields,
?string $accessTokenField = null
) {
$this->request = $request;
$this->model = $model;
$this->fields = $fields;
$this->accessTokenField = $accessTokenField;
$this->bitinflowAccounts = $bitinflowAccounts;
}
/**
* @param mixed $identifier
* @return Builder|Model|object|null
*/
public function retrieveById($identifier)
{
$model = $this->createModel();
$user = $this->newModelQuery($model)
->where($model->getAuthIdentifierName(), $identifier)
->first();
if ($user) {
return $user;
}
$token = $this->request->bearerToken();
$this->bitinflowAccounts->setToken($token);
$result = $this->bitinflowAccounts->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.
*
* @return Model
*/
public function createModel(): Model
{
$class = '\\'.ltrim($this->model, '\\');
return new $class;
}
/**
* Get a new query builder for the model instance.
*
* @param Model|null $model
* @return Builder
*/
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)
{
return false;
}
}