mirror of
https://github.com/anikeen-com/id.git
synced 2026-03-15 14:46:15 +00:00
first commit
This commit is contained in:
88
src/Id/Socialite/Provider.php
Normal file
88
src/Id/Socialite/Provider.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Socialite;
|
||||
|
||||
use Anikeen\Id\Enums\Scope;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Laravel\Socialite\Two\ProviderInterface;
|
||||
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
|
||||
use SocialiteProviders\Manager\OAuth2\User;
|
||||
|
||||
class Provider extends AbstractProvider implements ProviderInterface
|
||||
{
|
||||
/**
|
||||
* Unique Provider Identifier.
|
||||
*/
|
||||
const IDENTIFIER = 'ANIKEEN_ID';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $scopes = [Scope::USER_READ];
|
||||
|
||||
/**
|
||||
* {@inherticdoc}
|
||||
*/
|
||||
protected $scopeSeparator = ' ';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getAuthUrl($state): string
|
||||
{
|
||||
return $this->buildAuthUrlFromBase(
|
||||
'https://id.anikeen.com/oauth/authorize', $state
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getTokenUrl(): string
|
||||
{
|
||||
return 'https://id.anikeen.com/oauth/token';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
protected function getUserByToken($token)
|
||||
{
|
||||
$response = $this->getHttpClient()->get(
|
||||
'https://id.anikeen.com/api/v1/user', [
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
'Authorization' => 'Bearer ' . $token,
|
||||
],
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true)['data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function mapUserToObject(array $user): \Laravel\Socialite\Two\User|User
|
||||
{
|
||||
return (new User())->setRaw($user)->map([
|
||||
'id' => $user['id'],
|
||||
'nickname' => $user['username'],
|
||||
'name' => $user['name'],
|
||||
'email' => Arr::get($user, 'email'),
|
||||
'avatar' => $user['avatar'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getTokenFields($code): array
|
||||
{
|
||||
return array_merge(parent::getTokenFields($code), [
|
||||
'grant_type' => 'authorization_code',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user