-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
144 lines (134 loc) · 3.64 KB
/
auth.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
<?php
require_once('config.php');
require_once('utils.php');
// Static stuff
const STAGE_INITIATE = 1;
const STAGE_COMPLETE = 2;
$auth_stage = isset($_GET['code']) ? STAGE_COMPLETE : STAGE_INITIATE;
if (ENABLE_MAINTENANCE && $auth_stage == STAGE_INITIATE && rand(0,9) === 1) {
statefile_cleanup();
}
function validate_state($state) {
if (! is_string($state) || strlen($state) < 34) {
return false;
}
$file_id = substr($state, 33);
$statevar = substr($state, 0, 32);
$statefile = TEMPDIR.'/'.STATEFILE_PREFIX.$file_id;
$fileage = time() - filemtime($statefile);
$savedstate = $fileage > 0 && $fileage < STATEFILE_EXPIRY ? file_get_contents($statefile) : false;
return file_exists($statefile) && unlink($statefile) && $savedstate === $statevar;
}
function save_state() {
$state = md5(rand());
$statefile = tempnam(TEMPDIR, STATEFILE_PREFIX);
if (file_put_contents($statefile, $state) === strlen($state)) {
return "${state}-".substr(basename($statefile), strlen(STATEFILE_PREFIX));
}
return false;
}
function save_token($code) {
$token = md5($code);
$tokenfile = PRIVDIR."/${token}";
$f = fopen($tokenfile, 'x');
if (! $f) {
return false;
}
if (fwrite($f, $code) !== strlen($code)) {
return fclose($f);
}
return fclose($f) ? $token : false;
}
function auth_content($auth_stage) {
switch ($auth_stage) {
case STAGE_INITIATE:
$state = save_state();
if (! is_string($state)) {
// State saving failed for some unknown reason
return <<<EOT
<p>Badges app failed to do what it should, maybe you should file a bug report?</p>
<p></p>
EOT;
}
$auth_uri = AUTH_SERVER.'?client_id='.CLIENT_ID."&state=${state}&redirect_uri=".RETURN_URI;
return <<<EOT
<p>Badges app must mine and store some of your personal identification data.<br/>Go away if you disagree.</p>
<a class="button" href="${auth_uri}">Authenticate with GitHub</a>
<p></p>
EOT;
case STAGE_COMPLETE:
// We are not going to request access tokens as we are not going to access any user data.
$code = query_param('code', 48, 1, false);
$state = query_param('state', 48, 34, false);
if (! validate_state($state)) {
// State validation failed for some unknown reason
return <<<EOT
<p>Badges app failed to validate your authorization, maybe you should file a bug report?</p>
<p></p>
EOT;
}
$token = save_token($code);
if (! is_string($token)) {
// State validation failed for some unknown reason
return <<<EOT
<p>Badges app failed to finish your authorization, maybe you should file a bug report?</p>
<p></p>
EOT;
}
return <<<EOT
<p>Badges app mined your data and dug up this token, use this token to authenticate with Mineunit Badges.</p>
<span class="button">${token}</span>
<p></p>
EOT;
}
}
$content = auth_content($auth_stage);
?><!DOCTYPE html>
<html>
<head>
<title>Mineunit Badges</title>
<style type="text/css">
.center {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
min-height: 100vh;
font-family: Helvetica,Arial,sans-serif;
}
.window {
min-width: 20em;
min-height: 10em;
padding: 1em;
border: 4px #000 double;
}
.inactive {
background-color: #555;
}
.active {
background-color: #dde;
}
.button {
background-color: #192;
color: #ddf;
padding: 8px 20px;
margin: 1em;
text-decoration:none;
font-weight:bold;
border-radius:5px;
}
p {
max-width: 20em;
}
</style>
</head>
<body class="inactive">
<div class="center">
<div class="active window">
<h1>Mineunit Badges</h1>
<?= $content ?>
</div>
</div>
</body>
</html>