-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProvider.php
123 lines (103 loc) · 3.23 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace SocialiteProviders\Line;
use GuzzleHttp\RequestOptions;
use Laravel\Socialite\Two\InvalidStateException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'LINE';
protected $scopeSeparator = ' ';
protected $scopes = [
'openid',
'profile',
'email',
];
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase('https://access.line.me/oauth2/v2.1/authorize', $state);
}
protected function getTokenUrl(): string
{
return 'https://api.line.me/oauth2/v2.1/token';
}
/**
* Get the raw user for the given access token.
*
* @param string $token
* @return array
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://api.line.me/v2/profile',
[
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)
{
return (new User)->setRaw($user)->map([
'id' => $user['userId'] ?? $user['sub'] ?? null,
'nickname' => null,
'name' => $user['displayName'] ?? $user['name'] ?? null,
'avatar' => $user['pictureUrl'] ?? $user['picture'] ?? null,
'email' => $user['email'] ?? null,
]);
}
/**
* @return \SocialiteProviders\Manager\OAuth2\User
*/
public function user()
{
if ($this->hasInvalidState()) {
throw new InvalidStateException;
}
$response = $this->getAccessTokenResponse($this->getCode());
if ($jwt = $response['id_token'] ?? null) {
$bodyb64 = explode('.', $jwt)[1];
$user = $this->mapUserToObject(json_decode(base64_decode(strtr($bodyb64, '-_', '+/')), true));
} else {
$user = $this->mapUserToObject($this->getUserByToken(
$token = $this->parseAccessToken($response)
));
}
$this->credentialsResponseBody = $response;
if ($user instanceof User) {
$user->setAccessTokenResponseBody($this->credentialsResponseBody);
}
return $user->setToken($this->parseAccessToken($response))
->setRefreshToken($this->parseRefreshToken($response))
->setExpiresIn($this->parseExpiresIn($response));
}
public static function additionalConfigKeys(): array
{
return [
'bot_prompt',
];
}
/**
* {@inheritdoc}
*
* @throws \InvalidArgumentException
*/
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);
$botPrompt = $this->getConfig('bot_prompt');
if (! empty($botPrompt)) {
$fields['bot_prompt'] = $botPrompt;
}
return $fields;
}
}