forked from d-sak/gauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauth.php
125 lines (107 loc) · 3.64 KB
/
gauth.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
<?php
//https://code.google.com/apis/console/
/*
$options = array(
'client_id' => '',
'client_secret' => '',
'doc_id' => '',
'redirect_uri' => "",
'onSucceed' => '',
);
*/
class gauth
{
public static function login(array $options)
{
$scope = array(
"https://docs.google.com/feeds/default/private/full/{$options['doc_id']}",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
);
$scope = implode(' ', $scope);
$url = 'https://accounts.google.com/o/oauth2/auth?client_id='.urlencode($options['client_id']).
'&redirect_uri='.urlencode($options['redirect_uri']).'&scope='.urlencode($scope).'&response_type=code';
header('Location: '.$url);
exit;
}
public static function auth($code, array $options)
{
$options['grant_type'] = 'authorization_code';
$options['code'] = $code;
$access_token = self::getAccessToken($options);
self::checkACL($access_token, $options);
$_SESSION['GAUTH_USERINFO'] = self::getUserInfo($access_token);
$_SESSION['GAUTH_TOKEN'] = $access_token;
header('Location: '.$options['onSucceed']);
exit;
}
private static function getAccessToken(array $options)
{
$uri = 'https://accounts.google.com/o/oauth2/token';
$var = self::post($uri, $options);
if (empty($var)) {
throw new RuntimeException('token api failed');
}
$var = json_decode($var, true);
if (empty($var['access_token'])) {
throw new RuntimeException('parse token failed');
}
return $var['access_token'];
}
private static function checkACL($access_token, array $options)
{
//ACL check
$uri = "https://docs.google.com/feeds/default/private/full/{$options['doc_id']}";
$head = array(
'Authorization' => 'Bearer '.$access_token,
'GData-Version' => '3.0',
);
$doc = self::get($uri, array(), $head);
if (empty($doc)) {
throw new RuntimeException('doc access failed');
}
$doc = simplexml_load_string($doc);
if(empty($doc->title)){
throw new RuntimeException('parse doc failed');
}
}
private static function getUserInfo($access_token)
{
$uri = 'https://www.googleapis.com/oauth2/v1/userinfo';
$header = array(
'Authorization' => 'Bearer '.$access_token,
);
$result = self::get($uri, array(), $header);
return json_decode($result, true);
}
private static function post($url, $post_data = array())
{
$data = http_build_query($post_data);
$header = array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".strlen($data)
);
$context = array(
"http" => array(
"method" => "POST",
"header" => implode("\r\n", $header),
"content" => $data
)
);
return file_get_contents($url, false, stream_context_create($context));
}
private static function get($url, $data, $headers=array()){
$data = http_build_query($data);
$head = array();
foreach($headers as $key=>$val){
$head[] = $key . ': ' . $val;
}
$context = array(
"http" => array(
"method" => "GET",
"header" => implode("\r\n", $head),
)
);
return file_get_contents($url . '?' . $data, false, stream_context_create($context));
}
}