Merge pull request #7 from envoyr/sso

Add sso user provider
This commit is contained in:
René Preuß
2022-01-02 14:25:29 +01:00
committed by GitHub
3 changed files with 123 additions and 6 deletions

View File

@@ -20,7 +20,7 @@
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
"orchestra/testbench": "^6.0",
"codedungeon/phpunit-result-printer": "^0.26.2"
"codedungeon/phpunit-result-printer": "^0.31"
},
"autoload": {
"psr-4": {

View File

@@ -1,8 +1,8 @@
<?php
return [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY', ''),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET', ''),
'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI', ''),
'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URI', ''),
];
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI'),
'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URI'),
];

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