This repository was archived by the owner on Nov 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2.php
228 lines (154 loc) · 6.17 KB
/
oauth2.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
# namespace NeoGeek\OverseerFramework;
/* ------------------------------------------------------------
Overseer Framework, build 5, 2014-01-25
http://overseerframework.com/
Copyright (c) 2013 Neo Geek
Dual-licensed under both MIT and BSD licenses.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
------------------------------------------------------------ */
/**
* OAuth2
* A simple PHP class for establishing OAuth 2.0 connections.
* @author Neo Geek <[email protected]>
* @copyright Copyright (c) 2013, Neo Geek
*/
if (!class_exists('OAuth2')) {
class OAuth2
{
public $id;
public $secret;
public $callback;
public $scope;
public $state;
public $url_authorize;
public $url_access_token;
public $user_agent_string = 'OAuth2 Developer App';
/**
* __construct
* Sets up a new OAuth2 object.
* @method object OAuth2 (string $id, string $secret, string $callback);
* @param string $id
* @param string $secret
* @param string $callback
* @return object
* @example $OAuth2 = new OAuth2('ID', 'SECRET', 'http://www.example.com/callback.php');
* @author Neo Geek <[email protected]>
* @copyright Copyright (c) 2013, Neo Geek
*/
final public function __construct ($id, $secret, $callback) {
$this->id = $id;
$this->secret = $secret;
$this->callback = rawurlencode($callback);
}
/**
* authenticate
* Redirects the user to the specified authorize URL.
* @method void OAuth2::authenticate ();
* @return void
* @example $OAuth2->authenticate();
* @author Neo Geek <[email protected]>
* @copyright Copyright (c) 2013, Neo Geek
*/
final public function authenticate () {
$url = sprintf($this->url_authorize, $this->id, $this->callback, rawurlencode($this->scope), $this->state);
header('Location: ' . $url); exit;
}
/**
* callback
* Returns the callback response based on OAuth2 callback GET variables.
* @method void OAuth2::callback (function $func);
* @param function func
* @return void
* @example $OAuth2->callback(function ($return) { echo OAuth2::parseToken($return); });
* @author Neo Geek <[email protected]>
* @copyright Copyright (c) 2013, Neo Geek
*/
final public function callback ($func) {
$url = sprintf($this->url_access_token, $this->id, $this->secret, $this->callback, $_GET['code']);
call_user_func($func, $this->request($url, true));
}
/**
* parseToken
* Returns the access_token from a OAuth2 callback request.
* @method string|boolean OAuth2::parseToken (string $string);
* @param string $string
* @return string|boolean
* @example echo OAuth2::parseToken($return);
* @author Neo Geek <[email protected]>
* @copyright Copyright (c) 2013, Neo Geek
*/
final public static function parseToken ($string) {
if (preg_match('/access_token=([^&]+)/', $string, $matches)) {
return $matches[1];
} else if ($string = json_decode($string, true)) {
return $string['access_token'];
}
return false;
}
/**
* request
* Initiates a curl request using either GET or POST.
* @method string OAuth2::request (string $url [, boolean $post]);
* @param string $url
* @param boolean $post (optional)
* @return string
* @example echo OAuth2::request('https://api.example.com/request');
* @example echo OAuth2::request('https://api.example.com/request?response=json', true);
* @author Neo Geek <[email protected]>
* @copyright Copyright (c) 2013, Neo Geek
*/
final public function request ($url, $post = false) {
$ch = curl_init($url);
if (strtolower(parse_url($url, PHP_URL_SCHEME)) == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
if ($this->user_agent_string) {
curl_setopt($ch,CURLOPT_USERAGENT, $this->user_agent_string);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, parse_url($url, PHP_URL_QUERY));
$url = preg_replace('/\?.+$/', '', $url);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
}
if (!class_exists('Facebook_OAuth2')) {
class Facebook_OAuth2 extends OAuth2 {
public $url_authorize = 'https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=%s&state=%s';
public $url_access_token = 'https://graph.facebook.com/oauth/access_token?client_id=%s&client_secret=%s&redirect_uri=%s&code=%s';
}
}
if (!class_exists('GitHub_OAuth2')) {
class GitHub_OAuth2 extends OAuth2 {
public $url_authorize = 'https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s&scope=%s&state=%s';
public $url_access_token = 'https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&redirect_uri=%s&code=%s';
}
}
if (!class_exists('GooglePlus_OAuth2')) {
class GooglePlus_OAuth2 extends OAuth2 {
public $scope = 'https://www.googleapis.com/auth/plus.me';
public $url_authorize = 'https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=%s&redirect_uri=%s&scope=%s&state=%s';
public $url_access_token = 'https://accounts.google.com/o/oauth2/token?grant_type=authorization_code&client_id=%s&client_secret=%s&redirect_uri=%s&code=%s';
}
}