1
|
|
|
<?php namespace Arcanedev\Socialite\OAuth\One; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Socialite\Contracts\Provider; |
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
use League\OAuth1\Client\Credentials\TokenCredentials; |
6
|
|
|
use League\OAuth1\Client\Server\Server; |
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractProvider |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\Socialite\OAuth\One |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractProvider implements Provider |
16
|
|
|
{ |
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
18
|
|
|
| Properties |
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
20
|
|
|
*/ |
21
|
|
|
/** |
22
|
|
|
* The HTTP request instance. |
23
|
|
|
* |
24
|
|
|
* @var \Illuminate\Http\Request |
25
|
|
|
*/ |
26
|
|
|
protected $request; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The OAuth server implementation. |
30
|
|
|
* |
31
|
|
|
* @var \League\OAuth1\Client\Server\Server |
32
|
|
|
*/ |
33
|
|
|
protected $server; |
34
|
|
|
|
35
|
|
|
/* ------------------------------------------------------------------------------------------------ |
36
|
|
|
| Constructor |
37
|
|
|
| ------------------------------------------------------------------------------------------------ |
38
|
|
|
*/ |
39
|
|
|
/** |
40
|
|
|
* Create a new provider instance. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Http\Request $request |
43
|
|
|
* @param \League\OAuth1\Client\Server\Server $server |
44
|
|
|
*/ |
45
|
18 |
|
public function __construct(Request $request, Server $server) |
46
|
|
|
{ |
47
|
18 |
|
$this->server = $server; |
48
|
18 |
|
$this->setRequest($request); |
49
|
18 |
|
} |
50
|
|
|
|
51
|
|
|
/* ------------------------------------------------------------------------------------------------ |
52
|
|
|
| Getters & Setters |
53
|
|
|
| ------------------------------------------------------------------------------------------------ |
54
|
|
|
*/ |
55
|
|
|
/** |
56
|
|
|
* Set the request instance. |
57
|
|
|
* |
58
|
|
|
* @param \Illuminate\Http\Request $request |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
18 |
|
public function setRequest(Request $request) |
63
|
|
|
{ |
64
|
18 |
|
$this->request = $request; |
65
|
|
|
|
66
|
18 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/* ------------------------------------------------------------------------------------------------ |
70
|
|
|
| Main Functions |
71
|
|
|
| ------------------------------------------------------------------------------------------------ |
72
|
|
|
*/ |
73
|
|
|
/** |
74
|
|
|
* Redirect the user to the authentication page for the provider. |
75
|
|
|
* |
76
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
77
|
|
|
*/ |
78
|
6 |
|
public function redirect() |
79
|
|
|
{ |
80
|
6 |
|
$this->request->session()->set( |
81
|
6 |
|
'oauth.temp', $temp = $this->server->getTemporaryCredentials() |
82
|
3 |
|
); |
83
|
|
|
|
84
|
6 |
|
return new RedirectResponse($this->server->getAuthorizationUrl($temp)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the User instance for the authenticated user. |
89
|
|
|
* |
90
|
|
|
* @throws \InvalidArgumentException |
91
|
|
|
* |
92
|
|
|
* @return \Arcanedev\Socialite\OAuth\One\User |
93
|
|
|
*/ |
94
|
12 |
|
public function user() |
95
|
|
|
{ |
96
|
12 |
|
if ( ! $this->hasNecessaryVerifier()) { |
97
|
6 |
|
throw new \InvalidArgumentException( |
98
|
3 |
|
'Invalid request. Missing OAuth verifier.' |
99
|
3 |
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
$user = $this->server->getUserDetails($token = $this->getToken()); |
103
|
6 |
|
$instance = (new User)->setRaw($user->extra) |
104
|
6 |
|
->setToken($token->getIdentifier(), $token->getSecret()); |
105
|
|
|
|
106
|
6 |
|
return $instance->map([ |
107
|
6 |
|
'id' => $user->uid, |
108
|
6 |
|
'nickname' => $user->nickname, |
109
|
6 |
|
'name' => $user->name, |
110
|
6 |
|
'email' => $user->email, |
111
|
6 |
|
'avatar' => $user->imageUrl, |
112
|
3 |
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get a Social User instance from a known access token and secret. |
117
|
|
|
* |
118
|
|
|
* @param string $token |
119
|
|
|
* @param string $secret |
120
|
|
|
* |
121
|
|
|
* @return \Arcanedev\Socialite\OAuth\One\User |
122
|
|
|
*/ |
123
|
|
|
public function userFromTokenAndSecret($token, $secret) |
124
|
|
|
{ |
125
|
|
|
$tokenCredentials = new TokenCredentials; |
126
|
|
|
$tokenCredentials->setIdentifier($token); |
127
|
|
|
$tokenCredentials->setSecret($secret); |
128
|
|
|
|
129
|
|
|
$user = $this->server->getUserDetails($tokenCredentials); |
130
|
|
|
|
131
|
|
|
$instance = (new User)->setRaw($user->extra) |
132
|
|
|
->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret()); |
133
|
|
|
|
134
|
|
|
return $instance->map([ |
135
|
|
|
'id' => $user->uid, |
136
|
|
|
'nickname' => $user->nickname, |
137
|
|
|
'name' => $user->name, |
138
|
|
|
'email' => $user->email, |
139
|
|
|
'avatar' => $user->imageUrl, |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/* ------------------------------------------------------------------------------------------------ |
144
|
|
|
| Other Functions |
145
|
|
|
| ------------------------------------------------------------------------------------------------ |
146
|
|
|
*/ |
147
|
|
|
/** |
148
|
|
|
* Get the token credentials for the request. |
149
|
|
|
* |
150
|
|
|
* @return \League\OAuth1\Client\Credentials\TokenCredentials |
151
|
|
|
*/ |
152
|
6 |
|
protected function getToken() |
153
|
|
|
{ |
154
|
6 |
|
$temp = $this->request->session()->get('oauth.temp'); |
155
|
|
|
|
156
|
6 |
|
return $this->server->getTokenCredentials( |
157
|
6 |
|
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier') |
158
|
3 |
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Determine if the request has the necessary OAuth verifier. |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
12 |
|
protected function hasNecessaryVerifier() |
167
|
|
|
{ |
168
|
12 |
|
return $this->request->has('oauth_token') && $this->request->has('oauth_verifier'); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|