-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoauth.php
45 lines (40 loc) · 1.27 KB
/
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
<?php
require_once 'config.php';
require_once 'auth.php';
if (!isset($_GET['code'])) {
header("Location: ".$oauth_request_url, true, 303);
exit();
}
$code = $_GET['code'];
$parameters = 'client_id='.$oauth_client_id.'&code='.$code.'&grant_type=authorization_code&redirect_uri='.$oauth_redirect_url.'&client_secret='.$oauth_client_secret;
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $parameters,
],
];
$context = stream_context_create($options);
$result = file_get_contents($oauth_token_url, false, $context);
if ($result === false) {
http_response_code(401);
die("auth failed");
}
$access_token = json_decode($result)->{'access_token'};
$options = [
'http' => [
'header' => "Authorization: Bearer ".$access_token."\r\n",
'method' => 'GET'
],
];
$context = stream_context_create($options);
$result = file_get_contents($oauth_userinfo_url, false, $context);
if ($result === false) {
http_response_code(500);
die("request failed");
};
$data = json_decode($result, true);
$token = create_token($data['sub'], $data['preferred_username'], $data['name']);
setcookie("auth", $token, time() + 60*60*24*31,"/");
header("Location: /");
exit();