This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcallback.php
194 lines (162 loc) · 5.71 KB
/
callback.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
<?php
//*********************************************************
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the ""License"");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS
// OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language
// governing permissions and limitations under the License.
//*********************************************************
define('AUTHCOOKIE', 'wl_auth');
define('ERRORCODE', 'error');
define('ERRORDESC', 'error_description');
define('ACCESSTOKEN', 'access_token');
define('AUTHENTICATION_TOKEN', 'authentication_token');
define('CODE', 'code');
define('SCOPE', 'scope');
define('EXPIRESIN', 'expires_in');
define('REFRESHTOKEN', 'refresh_token');
// Update the following values
define('CLIENTID', '%CLIENT_ID%');
define('CLIENTSECRET', '%CLIENT_SECRET%');
// Make sure this is identical to the redirect_uri parameter passed in WL.init() call.
define('CALLBACK', '%REDIRECT_URI_PATH%/callback.php');
define('OAUTHURL', 'https://login.live.com/oauth20_token.srf');
function buildQueryString($array) {
return http_build_query($array);
}
function parseQueryString($query) {
$result = [];
parse_str($query,$result);
return $result;
}
function sendRequest($url, $method = 'GET', $data = [], $headers = null) {
if($headers === null) {
$headers = array('Content-type: application/x-www-form-urlencoded;charset=UTF-8'));
}
$context = stream_context_create([
'http' => [
'method' => $method,
'header' => $headers,
'content' => buildQueryString($data)
]
]);
return file_get_contents($url, false, $context);
}
function requestAccessToken($content) {
$response = sendRequest(OAUTHURL, 'POST', $content);
if ($response !== false) {
$authToken = json_decode($response);
if (!empty($authToken) && !empty($authToken->{ACCESSTOKEN})) {
return $authToken;
}
}
return false;
}
function requestAccessTokenByVerifier($verifier) {
return requestAccessToken([
'client_id' => CLIENTID,
'redirect_uri' => CALLBACK,
'client_secret' => CLIENTSECRET,
'code' => $verifier,
'grant_type' => 'authorization_code'
]);
}
function requestAccessTokenByRefreshToken($refreshToken) {
return requestAccessToken([
'client_id' => CLIENTID,
'redirect_uri' => CALLBACK,
'client_secret' => CLIENTSECRET,
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token'
]);
}
function handlePageRequest() {
if (!empty($_GET[ACCESSTOKEN])) {
// There is a token available already. It should be the token flow. Ignore it.
return;
}
$verifier = $_GET[CODE];
if (!empty($verifier)) {
$token = requestAccessTokenByVerifier($verifier);
if ($token !== false) {
handleTokenResponse($token);
} else {
handleTokenResponse(null, [
ERRORCODE => 'request_failed',
ERRORDESC => 'Failed to retrieve user access token.'
]);
}
return;
}
$refreshToken = readRefreshToken();
if (!empty($refreshToken)) {
$token = requestAccessTokenByRefreshToken($refreshToken);
if ($token !== false) {
handleTokenResponse($token);
} else {
handleTokenResponse(null, [
ERRORCODE => 'request_failed',
ERRORDESC => 'Failed to retrieve user access token.')
];
}
return;
}
$errorCode = isset($_GET[ERRORCODE]) ? $_GET[ERRORCODE] : null;
$errorDesc = isset($_GET[ERRORDESC]) ? $_GET[ERRORDESC] : null;
if (!empty($errorCode)) {
handleTokenResponse(null, [
ERRORCODE => $errorCode,
ERRORDESC => $errorDesc
]);
}
}
function readRefreshToken() {
// read refresh token of the user identified by the site.
return null;
}
function saveRefreshToken($refreshToken) {
// save the refresh token and associate it with the user identified by your site credential system.
}
function handleTokenResponse($token, $error = null) {
$authCookie = isset($_COOKIE[AUTHCOOKIE]) ? $_COOKIE[AUTHCOOKIE] : '';
$cookieValues = parseQueryString($authCookie);
if (!empty($token)) {
$cookieValues[ACCESSTOKEN] = $token->{ACCESSTOKEN};
$cookieValues[AUTHENTICATION_TOKEN] = $token->{AUTHENTICATION_TOKEN};
$cookieValues[SCOPE] = $token->{SCOPE};
$cookieValues[EXPIRESIN] = $token->{EXPIRESIN};
if (!empty($token->{REFRESHTOKEN}))
{
saveRefreshToken($token->{REFRESHTOKEN});
}
}
if (!empty($error)) {
$cookieValues[ERRORCODE] = $error[ERRORCODE];
$cookieValues[ERRORDESC] = $error[ERRORDESC];
}
setrawcookie(AUTHCOOKIE, buildQueryString($cookieValues), 0, '/', $_SERVER[SERVER_NAME]);
}
handlePageRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:msgr="http://messenger.live.com/2009/ui-tags">
<head>
<title>Live SDK Callback Page</title>
<script src="//js.live.net/v5.0/wl.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>