-
Notifications
You must be signed in to change notification settings - Fork 9
/
Provider.php
93 lines (80 loc) · 2.61 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace SocialiteProviders\LaravelPassport;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'LARAVELPASSPORT';
protected $scopeSeparator = ' ';
public static function additionalConfigKeys(): array
{
return [
'host',
'authorize_uri',
'token_uri',
'userinfo_uri',
'userinfo_key',
'user_id',
'user_nickname',
'user_name',
'user_email',
'user_avatar',
'guzzle',
];
}
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase($this->getLaravelPassportUrl('authorize_uri'), $state);
}
protected function getTokenUrl(): string
{
return $this->getLaravelPassportUrl('token_uri');
}
/**
* Get the raw user for the given access token.
*
* @param string $token
* @return array
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->getLaravelPassportUrl('userinfo_uri'), [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode((string) $response->getBody(), true);
}
/**
* Map the raw user array to a Socialite User instance.
*
* @param array $user
* @return \Laravel\Socialite\User
*/
protected function mapUserToObject(array $user)
{
$key = $this->getConfig('userinfo_key');
$data = ($key === null) === true ? $user : Arr::get($user, $key, []);
return (new User)->setRaw($data)->map([
'id' => $this->getUserData($data, 'id'),
'nickname' => $this->getUserData($data, 'nickname'),
'name' => $this->getUserData($data, 'name'),
'email' => $this->getUserData($data, 'email'),
'avatar' => $this->getUserData($data, 'avatar'),
]);
}
protected function getLaravelPassportUrl($type)
{
return rtrim($this->getConfig('host'), '/').'/'.ltrim($this->getConfig($type, Arr::get([
'authorize_uri' => 'oauth/authorize',
'token_uri' => 'oauth/token',
'userinfo_uri' => $this->getConfig('userinfo_uri', 'api/user'),
], $type)), '/');
}
protected function getUserData($user, $key)
{
return Arr::get($user, $this->getConfig('user_'.$key, $key));
}
}