This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
212 lines (180 loc) · 5.61 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import "https://deno.land/[email protected]/dotenv/load.ts";
import { logger } from "https://deno.land/x/[email protected]/middleware.ts";
import { Hono } from "https://deno.land/x/[email protected]/mod.ts";
const FILESTASH_URL = Deno.env.get("FILESTASH_URL")!;
const FILESTASH_API_KEY = Deno.env.get("FILESTASH_API_KEY")!;
const API_PREFIX = Deno.env.get("API_PREFIX")!;
const OIDC_CONFIG_URL = Deno.env.get("OIDC_CONFIG_URL")!;
const OIDC_CLIENT_ID = Deno.env.get("OIDC_CLIENT_ID")!;
const OIDC_CLIENT_SECRET = Deno.env.get("OIDC_CLIENT_SECRET")!;
const SFTPGO_WEB_URL = Deno.env.get("SFTPGO_WEB_URL")!;
const SFTPGO_ADMIN_BASICAUTH = Deno.env.get("SFTPGO_ADMIN_BASICAUTH")!;
const SFTPGO_SFTP_HOST = Deno.env.get("SFTPGO_SFTP_HOST")!;
const SFTPGO_SFTP_PORT = Deno.env.get("SFTPGO_SFTP_PORT")!;
const PUBKEY_FILE = Deno.env.get("PUBKEY_FILE")!;
const PRIVKEY_FILE = Deno.env.get("PRIVKEY_FILE")!;
const PUBKEY = await Deno.readTextFile(PUBKEY_FILE);
const PRIVKEY = await Deno.readTextFile(PRIVKEY_FILE);
const FILESTASH_REDIRECT_URI = `${FILESTASH_URL}${API_PREFIX}/callback`;
const app = new Hono();
app.use("*", logger());
app.get("/login", (c) => {
return c.redirect(`${API_PREFIX}/login`);
});
app.get(`${API_PREFIX}/login`, async (c) => {
const config = await getOIDCConfig();
const authUrl = new URL(config.authorization_endpoint);
authUrl.searchParams.set("client_id", OIDC_CLIENT_ID);
authUrl.searchParams.set("redirect_uri", FILESTASH_REDIRECT_URI);
authUrl.searchParams.set("response_type", "code");
authUrl.searchParams.set("scope", "openid profile groups");
return c.redirect(authUrl.toString());
});
app.get(`${API_PREFIX}/callback`, async (c) => {
const accessToken = await getOIDCAccessToken(c.req.query("code")!);
console.debug(accessToken);
const data = await getUserinfo(accessToken);
console.debug(data);
const { preferred_username: username, groups: oidc_groups } = data as {
preferred_username: string;
groups: string[];
};
const groups = oidc_groups.map((g: string) => ({
name: g.replace("discord-", ""),
type: 2,
}));
const apiToken = await createToken();
const user = await findUser(apiToken, username);
console.debug(user);
if (!user) {
await createUser(apiToken, username, groups);
} else {
await updateUser(apiToken, user, groups);
}
const setCookie = await createFilestashSession(username);
c.res.headers.set("Set-Cookie", setCookie);
return c.redirect("/");
});
let oidcConfig: {
authorization_endpoint: string;
token_endpoint: string;
userinfo_endpoint: string;
};
async function getOIDCConfig() {
if (!oidcConfig) {
const resp = await fetch(OIDC_CONFIG_URL);
oidcConfig = await resp.json();
}
return oidcConfig;
}
async function getOIDCAccessToken(code: string) {
const form = new URLSearchParams();
form.append("client_id", OIDC_CLIENT_ID);
form.append("client_secret", OIDC_CLIENT_SECRET);
form.append("grant_type", "authorization_code");
form.append("code", code);
form.append("redirect_uri", FILESTASH_REDIRECT_URI);
const config = await getOIDCConfig();
const resp = await fetch(config.token_endpoint, {
method: "POST",
body: form,
});
const json = await resp.json();
return json.access_token as string;
}
async function getUserinfo(accessToken: string) {
const config = await getOIDCConfig();
const resp = await fetch(config.userinfo_endpoint, {
headers: { Authorization: `Bearer ${accessToken}` },
});
return await resp.json();
}
async function createToken() {
const resp = await fetch(`${SFTPGO_WEB_URL}/api/v2/token`, {
headers: { Authorization: `Basic ${SFTPGO_ADMIN_BASICAUTH}` },
});
const json = await resp.json();
return json.access_token as string;
}
async function findUser(apiToken: string, username: string) {
const resp = await fetch(
`${SFTPGO_WEB_URL}/api/v2/users/${encodeURIComponent(username)}`,
{
headers: { Authorization: `Bearer ${apiToken}` },
}
);
if (resp.ok) {
return await resp.json();
} else {
return undefined;
}
}
interface SFTPGoGroup {
name: string;
type: number;
}
async function createUser(
apiToken: string,
username: string,
groups: SFTPGoGroup[]
) {
const user = {
status: 1,
username,
groups,
permissions: {
"/": ["list"],
},
public_keys: [PUBKEY],
};
const resp = await fetch(`${SFTPGO_WEB_URL}/api/v2/users`, {
method: "POST",
headers: { Authorization: `Bearer ${apiToken}` },
body: JSON.stringify(user),
});
if (!resp.ok) {
console.error(await resp.text());
}
}
// deno-lint-ignore no-explicit-any
async function updateUser(apiToken: string, user: any, groups: SFTPGoGroup[]) {
const public_keys = user.public_keys ?? [];
if (!public_keys.includes(PUBKEY)) {
public_keys.push(PUBKEY);
}
const payload = {
...user,
status: 1,
groups,
public_keys,
};
const resp = await fetch(
`${SFTPGO_WEB_URL}/api/v2/users/${encodeURIComponent(user.username)}`,
{
method: "PUT",
headers: { Authorization: `Bearer ${apiToken}` },
body: JSON.stringify(payload),
}
);
if (!resp.ok) {
console.error(await resp.text());
}
}
async function createFilestashSession(username: string) {
const sessionUrl = new URL("/api/session", FILESTASH_URL);
sessionUrl.searchParams.set("key", FILESTASH_API_KEY);
const payload = {
type: "sftp",
hostname: SFTPGO_SFTP_HOST,
port: SFTPGO_SFTP_PORT,
username,
password: PRIVKEY,
};
const resp = await fetch(sessionUrl, {
method: "POST",
body: JSON.stringify(payload),
});
const setCookie = resp.headers.get("Set-Cookie")!;
return setCookie;
}
Deno.serve(app.fetch);