-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
120 lines (108 loc) · 3.43 KB
/
index.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
// import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
//import { authorize as makeAuth, logout, handleRedirect, cookieKey } from './auth0'
import { globals, cookieKey } from "./auth0";
import { Session, State } from "./durables.js";
export { Session, State };
import cookie from "cookie";
const { log } = console;
export function authorizedCookie(event: Request) {
const cookieHeader = event.headers.get("Cookie");
if (cookieHeader && cookieHeader.includes(cookieKey)) {
console.log("bad cookie", cookieHeader);
const cookies = cookie.parse(cookieHeader);
if (cookies[cookieKey] != "") return true;
}
return false;
}
//point of this is to have a function that doesnt demand user be logged in, unlike for navbar profile button for example
export function authorized(event: Request, env: env) {
return authorize(event, env, true);
}
//event is actually request
export async function authorize(event, env, verifyOnly = false) {
const { MODE } = env;
const { authorize: makeAuth, logout, handleRedirect } = globals(env, event);
const stubs = {
user: {
authorization: {
userInfo: {
sub: "testSubId",
},
},
authorized: true,
},
};
const isTest = MODE == "test";
if (isTest) return stubs.user;
try {
//original coded expected worker event
//and wanted non-worktop event
// let {body, ...other} = event
// let request = new Request(event.url, {
// // ...event, body: await event.body()})
// // request = new Request(event.url, {
// ...other})
// event = request
let request = event;
console.log("makeauth");
const [authorized, authorization, redirectUrl] = await makeAuth(event);
console.log("stuck makeauth?");
if (authorized && authorization.accessToken) {
request = new Request(request, {
headers: {
Authorization: `Bearer ${authorization.accessToken}`,
},
});
}
let response = new Response(null);
const url = new URL(request.url);
console.log("auth", url.pathname);
if (url.pathname === "/auth") {
log("auth true");
const authorizedResponse = await handleRedirect(event);
if (!authorizedResponse) {
log("failed");
return new Response("Unauthorized", { status: 401 });
}
console.log(authorizedResponse, response.body);
response = new Response(response.body, {
...response,
...authorizedResponse,
});
return response;
}
if (!authorized && !verifyOnly) {
const url = new URL(event.url);
let path = url.pathname + url.search;
if (path == "/login") {
path = "/";
}
console.log("setting cookie", 33333, path);
return new Response("", {
status: 302,
headers: {
Location: "https://" + redirectUrl,
"Set-cookie": `loginRedirect="${path}"; HttpOnly; Secure; SameSite=Lax; Path=/`,
},
});
}
const logoutHeaders = { Location: "/" };
if (url.pathname === "/logout") {
const { headers } = logout(event);
const merged = Object.assign({}, logoutHeaders, headers);
console.log(merged);
return new Response(response.body, {
status: 302,
headers: merged,
});
}
if (authorized) {
return { authorized, authorization };
} else {
return { authorized };
}
} catch (err) {
console.log(err);
return new Response(err.toString());
}
}