-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgithub_oauth.php
executable file
·170 lines (148 loc) · 5.12 KB
/
github_oauth.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
<?php
/* GitHubOauth Library v1.1 (http://github.com/joeworkman/github_oauth)
* Developed by Joe Workman (http://joeworkman.net)
* Copyright (c) 2011-2012, Joe Workman. All rights reserved.
* Requires the PHP cURL extension
*/
class GitHubOauth {
public $shared_key = '';
public $shared_secret = '';
public $scope = '';
private $site = 'https://github.com/';
private $access_token_path = 'login/oauth/access_token';
private $authorize_path = 'login/oauth/authorize';
private $token_file = './github_token';
private $per_page = 100;
private $access_token;
private $redirect_uri;
private $curl;
public function __construct($scope, $key, $secret,$token_file = './github_token') {
$this->scope = $scope;
$this->set_keys($key, $secret);
$this->token_file = $token_file;
$this->redirect_uri = $this->get_current_url();
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
}
// Sets the site consumer key and consumer secret variables
public function set_keys($key, $secret) {
$this->shared_key = $key;
$this->shared_secret = $secret;
}
// Check to see if we already have a token saved.
public function has_access_token() {
return file_exists($this->token_file) ? TRUE : FALSE;
}
// Clear the current access token
public function revoke_access_token() {
$this->access_token = null;
unlink($this->token_file);
return;
}
// Redirect to GitHub and ask for user authorization.
// GitHub should redirect page to the current page with our access code as a page parameter
public function get_user_authorization() {
$query = array(
"client_id" => $this->shared_key,
"redirect_uri" => $this->redirect_uri,
"scope" => $this->scope
);
$url = $this->site . $this->authorize_path . '?' . http_build_query($query);
header("Location: ".$url);
}
// Obtains an access token with the request token passed through.
public function get_access_token() {
if ($this->has_access_token()) {
// If we already have an access token, use it…
$this->access_token = $this->get_saved_token();
}
else {
// Request a new access token…
$query = array(
"client_id" => $this->shared_key,
"client_secret" => $this->shared_secret,
"redirect_uri" => $this->redirect_uri,
"code" => $_GET['code']
);
$url = $this->site . $this->access_token_path . '?' . http_build_query($query);
$request = $this->http_post($url, $query);
$request = explode('&', $request);
$final = array();
foreach ($request as $item) {
$a = explode('=', $item);
$final["$a[0]"] = $a[1];
}
$this->access_token = $final['access_token'];
$this->save_access_token();
}
return $this->access_token;
}
// Make an authenticated request to GitHub
public function request($request, $query = array()) {
$query['access_token'] = $this->access_token;
$query['per_page'] = $this->per_page;
$url = 'https://api.github.com/'.$request;
if (!empty($query)) {
$url .= '?' . http_build_query($query);
}
return json_decode($this->http_get($url));
}
public function post_request($request, $data = array(), $query = array()) {
$query['access_token'] = $this->access_token;
$query['per_page'] = $this->per_page;
$url = 'https://api.github.com/'.$request;
if (!empty($query)) {
$url .= '?' . http_build_query($query);
}
return json_decode($this->http_post($url, json_encode($data)));
}
// Get the current page url
public function get_current_url() {
$page_url = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$page_url .= "s";}
$page_url .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$page_url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["SCRIPT_NAME"];
} else {
$page_url .= $_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];
}
return $page_url;
}
//---------------------------------------
// Private Methods to make HTTP Requests
//---------------------------------------
private function save_access_token() {
$handle = fopen($this->token_file, "w");
fwrite($handle, $this->access_token);
fclose($handle);
return;
}
private function get_saved_token() {
return file_get_contents($this->token_file);
}
private function http_get($url) {
curl_setopt($this->curl, CURLOPT_HTTPGET, TRUE);
return $this->_request($url);
}
private function http_post($url, $data = array()) {
curl_setopt($this->curl, CURLOPT_POST, TRUE);
curl_setopt($this->curl, CURLOPT_POST, count($data));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
return $this->_request($url);
}
private function _request($url) {
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLINFO_HEADER_OUT, true);
curl_setopt($this->curl, CURLOPT_USERAGENT, 'My GitHub App');
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Accept: application/vnd.github-blob.raw'));
return curl_exec($this->curl);
}
private function _build_protocol_string($prot) {
$array = array();
foreach ($prot as $key => $value) {
$array[] = "$key=$value";
}
return implode(", ", $array);
}
} // End GitHubOauth Class
?>