forked from librespeed/speedtest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
54 lines (46 loc) · 1.65 KB
/
auth.js
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
import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
const u='rio'
const p='456'
let cacheControl = {
browserTTL: null, // do not set cache control ttl on responses
edgeTTL: 2 * 60 * 60 * 24, // 2 days
bypassCache: true, // do not bypass Cloudflare's cache
}
async function authhandle(request) {
const has=request.request.headers.has('Authorization')
if (has) {
const { user, pass } = await basicAuthentication(request);
if (u === user && p===pass) {
return getAssetFromKV(request,{
cacheControl,
defaultMimeType:'application/octet-stream'
})
}
}
// Not authenticated.
return new Response('You need to login.', {
status: 401,
headers: {
// Prompts the user for credentials.
'WWW-Authenticate': 'Basic realm="my scope", charset="UTF-8"',
},
});
}
function basicAuthentication(request) {
const Authorization = request.request.headers.get('Authorization');
const [scheme, encoded] = Authorization.split(' ');
if (!encoded || scheme !== 'Basic') {
throw new BadRequestException('Malformed authorization header.');
}
const buffer = Uint8Array.from(atob(encoded), character => character.charCodeAt(0));
const decoded = new TextDecoder().decode(buffer).normalize();
const index = decoded.indexOf(':');
if (index === -1 || /[\0-\x1F\x7F]/.test(decoded)) {
throw new BadRequestException('Invalid authorization value.');
}
return {
user: decoded.substring(0, index),
pass: decoded.substring(index + 1),
};
}
export {authhandle}