diff --git a/composer.json b/composer.json index 5f58bf4..b05609c 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/config/bitinflow-accounts-api.php b/config/bitinflow-accounts-api.php index ab5c57f..cb82da1 100644 --- a/config/bitinflow-accounts-api.php +++ b/config/bitinflow-accounts-api.php @@ -1,8 +1,8 @@ env('BITINFLOW_ACCOUNTS_KEY', ''), - 'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET', ''), - 'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI', ''), - 'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URI', ''), -]; \ No newline at end of file + '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'), +]; diff --git a/src/GhostZero/BitinflowAccounts/Providers/BitinflowAccountsSsoUserProvider.php b/src/GhostZero/BitinflowAccounts/Providers/BitinflowAccountsSsoUserProvider.php new file mode 100644 index 0000000..336a2b9 --- /dev/null +++ b/src/GhostZero/BitinflowAccounts/Providers/BitinflowAccountsSsoUserProvider.php @@ -0,0 +1,117 @@ +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; + } +}