-
Notifications
You must be signed in to change notification settings - Fork 0
/
sip-dial-plan.ts
145 lines (121 loc) · 3.89 KB
/
sip-dial-plan.ts
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
// ----------------------------------------------------------------------------
// sip-dial-plan.ts
// ----------------------------------------------------------------------------
import { HOSTNAME, PORT, TOKEN_ALGORITHM, TOKEN_SECRET } from "./config.ts";
import { STATUS_CODE } from "https://deno.land/[email protected]/http/status.ts";
import { verify } from "https://deno.land/x/[email protected]/mod.ts";
import { type Payload } from "https://deno.land/x/[email protected]/mod.ts";
const DIAL_PLAN = "./dial-plan.json";
// ----------------------------------------------------------------------------
function methodNotAllowed(): Response {
return new Response("Method Not Allowed", {
status: STATUS_CODE.MethodNotAllowed,
});
}
// ----------------------------------------------------------------------------
function notFound(): Response {
return new Response("Not Found", {
status: STATUS_CODE.NotFound,
});
}
// ----------------------------------------------------------------------------
function emptyList(): Response {
return new Response("[]", {
status: STATUS_CODE.OK,
headers: {
"Access-Control-Allow-Origin": "*",
},
});
}
// ----------------------------------------------------------------------------
function ok(body: string): Response {
return new Response(body, {
status: STATUS_CODE.OK,
headers: {
"Access-Control-Allow-Origin": "*",
},
});
}
// ----------------------------------------------------------------------------
async function getCryptoKey(secret: string, hash: string): Promise<CryptoKey> {
const encoder = new TextEncoder();
const keyData = encoder.encode(secret);
const cryptoKey = await crypto.subtle.importKey(
"raw",
keyData,
{
name: "HMAC",
hash: hash,
},
true,
["sign", "verify"],
);
return cryptoKey;
}
// ----------------------------------------------------------------------------
async function verifyToken(token: string): Promise<Payload> {
let hash = "SHA-256";
// @ts-expect-error TS2367
if (TOKEN_ALGORITHM === "HS512") hash = "SHA-512";
const cryptoKey = await getCryptoKey(TOKEN_SECRET, hash);
const jwt = await verify(token, cryptoKey);
return jwt;
}
// ----------------------------------------------------------------------------
async function readDialPlan(): Promise<Response> {
try {
const json = await Deno.readTextFile(DIAL_PLAN);
return ok(json);
} catch {
return emptyList();
}
}
// ----------------------------------------------------------------------------
async function getDialPlan(req: Request): Promise<Response> {
const authHeader = req.headers.get("authorization");
if (!authHeader) return emptyList();
const token = authHeader.replace("Bearer ", "");
if (!token) return emptyList();
let jwt: Payload;
try {
jwt = await verifyToken(token);
} catch {
return emptyList();
}
try {
// @ts-expect-error TS18046
if (jwt.context.user.affiliation === "owner") return readDialPlan();
} catch {
// no affiliation field in token
}
try {
if (
// @ts-expect-error TS18046
jwt.context.user.moderator === "true" ||
// @ts-expect-error TS18046
jwt.context.user.moderator === true
) {
return readDialPlan();
}
} catch {
// no moderator field in token
}
return emptyList();
}
// ----------------------------------------------------------------------------
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
const path = url.pathname;
if (req.method !== "GET" && req.method !== "HEAD") return methodNotAllowed();
if (path !== "/get-dial-plan") return notFound();
return await getDialPlan(req);
}
// ----------------------------------------------------------------------------
function main() {
Deno.serve({
hostname: HOSTNAME,
port: PORT,
}, handler);
}
// ----------------------------------------------------------------------------
main();