forked from SocialiteProviders/Naver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaverProvider.php
More file actions
117 lines (104 loc) · 2.91 KB
/
NaverProvider.php
File metadata and controls
117 lines (104 loc) · 2.91 KB
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
<?php
namespace SocialiteProviders\Naver;
use Laravel\Socialite\Two\ProviderInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
use Illuminate\Support\Arr;
class NaverProvider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'NAVER';
/**
* Get the authentication URL for the provider.
*
* @param string $state
*
* @return string
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://nid.naver.com/oauth2.0/authorize', $state);
}
/**
* Get the token URL for the provider.
*
* @return string
*/
protected function getTokenUrl()
{
return 'https://nid.naver.com/oauth2.0/token';
}
/**
* Get the access token for the given code.
*
* @param string $code
*
* @return string
*/
public function getAccessToken($code)
{
$response = $this->getHttpClient()->request('POST', $this->getTokenUrl(), [
'headers' => ['Accept' => 'application/json'],
'form_params' => $this->getTokenFields($code),
]);
$this->credentialsResponseBody = json_decode($response->getBody(), true);
return $this->parseAccessToken($response->getBody());
}
/**
* Get the POST fields for the token request.
*
* @param string $code
*
* @return array
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
/**
* Get the raw user for the given access token.
*
* @param string $token
*
* @return array
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->request('GET', 'https://openapi.naver.com/v1/nid/getUserProfile.xml', [
'headers' => ['Authorization' => 'Bearer '.$token],
]);
return $this->parseXML($response->getBody()->getContents())['response'];
}
/**
* 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' => Arr::get($user, 'id'),
'name' => Arr::get($user, 'name'),
'nickname' => Arr::get($user, 'nickname'),
'email' => Arr::get($user, 'email'),
'avatar' => Arr::get($user, 'profile_image'),
]);
}
/**
* XML -> array 형식 변환.
*
* @param string $data
*
* @return array
*/
private function parseXML($data)
{
return json_decode(json_encode(simplexml_load_string($data, null, LIBXML_NOCDATA)), true);
}
}