1
|
|
|
<?php namespace Arcanedev\Socialite\Base; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Socialite\Contracts\Provider; |
4
|
|
|
use Arcanedev\Socialite\OAuth\One\User; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use League\OAuth1\Client\Server\Server; |
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class OAuthOneProvider |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\Socialite\Base |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class OAuthOneProvider 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
|
|
|
public function __construct(Request $request, Server $server) |
46
|
|
|
{ |
47
|
|
|
$this->server = $server; |
48
|
|
|
$this->setRequest($request); |
49
|
|
|
} |
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
|
|
|
public function setRequest(Request $request) |
63
|
|
|
{ |
64
|
|
|
$this->request = $request; |
65
|
|
|
|
66
|
|
|
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
|
|
|
public function redirect() |
79
|
|
|
{ |
80
|
|
|
$this->request->getSession()->set( |
81
|
|
|
'oauth.temp', $temp = $this->server->getTemporaryCredentials() |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
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
|
|
|
public function user() |
95
|
|
|
{ |
96
|
|
|
if ( ! $this->hasNecessaryVerifier()) { |
97
|
|
|
throw new \InvalidArgumentException( |
98
|
|
|
'Invalid request. Missing OAuth verifier.' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$user = $this->server->getUserDetails($token = $this->getToken()); |
103
|
|
|
$instance = (new User)->setRaw($user->extra) |
104
|
|
|
->setToken($token->getIdentifier(), $token->getSecret()); |
105
|
|
|
|
106
|
|
|
return $instance->map([ |
107
|
|
|
'id' => $user->uid, |
108
|
|
|
'nickname' => $user->nickname, |
109
|
|
|
'name' => $user->name, |
110
|
|
|
'email' => $user->email, |
111
|
|
|
'avatar' => $user->imageUrl, |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* ------------------------------------------------------------------------------------------------ |
116
|
|
|
| Other Functions |
117
|
|
|
| ------------------------------------------------------------------------------------------------ |
118
|
|
|
*/ |
119
|
|
|
/** |
120
|
|
|
* Get the token credentials for the request. |
121
|
|
|
* |
122
|
|
|
* @return \League\OAuth1\Client\Credentials\TokenCredentials |
123
|
|
|
*/ |
124
|
|
|
protected function getToken() |
125
|
|
|
{ |
126
|
|
|
$temp = $this->request->getSession()->get('oauth.temp'); |
127
|
|
|
|
128
|
|
|
return $this->server->getTokenCredentials( |
129
|
|
|
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier') |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Determine if the request has the necessary OAuth verifier. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
protected function hasNecessaryVerifier() |
139
|
|
|
{ |
140
|
|
|
return $this->request->has('oauth_token') && $this->request->has('oauth_verifier'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|