-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkry.ts
154 lines (146 loc) · 4.44 KB
/
linkry.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
import { FastifyPluginAsync } from "fastify";
import { z } from "zod";
import { AppRoles } from "../../common/roles.js";
import {
BaseError,
DatabaseFetchError,
NotFoundError,
NotImplementedError,
} from "../../common/errors/index.js";
import { intersection } from "../plugins/auth.js";
import { NoDataRequest } from "../types.js";
import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb";
import { genericConfig } from "../../common/config.js";
import { unmarshall } from "@aws-sdk/util-dynamodb";
type LinkrySlugOnlyRequest = {
Params: { id: string };
Querystring: undefined;
Body: undefined;
};
const rawRequest = {
slug: z.string().min(1),
redirect: z.string().url().min(1),
groups: z.optional(z.array(z.string()).min(1)),
};
const createRequest = z.object(rawRequest);
const patchRequest = z.object({ redirect: z.string().url().min(1) });
type LinkyCreateRequest = {
Params: undefined;
Querystring: undefined;
Body: z.infer<typeof createRequest>;
};
type LinkryPatchRequest = {
Params: { id: string };
Querystring: undefined;
Body: z.infer<typeof patchRequest>;
};
const dynamoClient = new DynamoDBClient({
region: genericConfig.AwsRegion,
});
const linkryRoutes: FastifyPluginAsync = async (fastify, _options) => {
fastify.get<LinkrySlugOnlyRequest>("/redir/:id", async (request, reply) => {
const id = request.params.id;
const command = new QueryCommand({
TableName: genericConfig.LinkryDynamoTableName,
KeyConditionExpression:
"#slug = :slugVal AND begins_with(#access, :accessVal)",
ExpressionAttributeNames: {
"#slug": "slug",
"#access": "access",
},
ExpressionAttributeValues: {
":slugVal": { S: id },
":accessVal": { S: "OWNER#" },
},
});
try {
const result = await dynamoClient.send(command);
if (!result || !result.Items || result.Items.length === 0) {
return reply
.headers({ "content-type": "text/html" })
.status(404)
.sendFile("404.html");
}
return reply.redirect(unmarshall(result.Items[0]).redirect);
} catch (e) {
if (e instanceof BaseError) {
throw e;
}
request.log.error(e);
throw new DatabaseFetchError({
message: "Could not retrieve mapping, please try again later.",
});
}
});
fastify.post<LinkyCreateRequest>(
"/redir",
{
preValidation: async (request, reply) => {
await fastify.zodValidateBody(request, reply, createRequest);
},
onRequest: async (request, reply) => {
await fastify.authorize(request, reply, [
AppRoles.LINKS_MANAGER,
AppRoles.LINKS_ADMIN,
]);
},
},
async (request, reply) => {
throw new NotImplementedError({});
},
);
fastify.patch<LinkryPatchRequest>(
"/redir/:id",
{
preValidation: async (request, reply) => {
await fastify.zodValidateBody(request, reply, patchRequest);
},
onRequest: async (request, reply) => {
await fastify.authorize(request, reply, [
AppRoles.LINKS_MANAGER,
AppRoles.LINKS_ADMIN,
]);
},
},
async (request, reply) => {
// make sure that a user can manage this link, either via owning or being in a group that has access to it, or is a LINKS_ADMIN.
// you can only change the URL it redirects to
throw new NotImplementedError({});
},
);
fastify.delete<LinkrySlugOnlyRequest>(
"/redir/:id",
{
preValidation: async (request, reply) => {
await fastify.zodValidateBody(request, reply, createRequest);
},
onRequest: async (request, reply) => {
await fastify.authorize(request, reply, [
AppRoles.LINKS_MANAGER,
AppRoles.LINKS_ADMIN,
]);
},
},
async (request, reply) => {
// make sure that a user can manage this link, either via owning or being in a group that has access to it, or is a LINKS_ADMIN.
throw new NotImplementedError({});
},
);
fastify.get<NoDataRequest>(
"/redir",
{
onRequest: async (request, reply) => {
await fastify.authorize(request, reply, [
AppRoles.LINKS_MANAGER,
AppRoles.LINKS_ADMIN,
]);
},
},
async (request, reply) => {
// if an admin, show all links
// if a links manager, show all my links + links I can manage
throw new NotImplementedError({});
},
);
};
export default linkryRoutes;