-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathapiAuthMiddleware.ts
More file actions
125 lines (107 loc) · 3.08 KB
/
apiAuthMiddleware.ts
File metadata and controls
125 lines (107 loc) · 3.08 KB
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
import { AuthType, ErrCode } from "@autumn/shared";
import { verifyKey } from "@/internal/dev/api-keys/apiKeyUtils.js";
import { isDashboardOrigin } from "@/utils/constants.js";
import RecaseError from "@/utils/errorUtils.js";
import { withOrgAuth } from "./authMiddleware.js";
import { verifyBearerPublishableKey } from "./publicAuthMiddleware.js";
import { trmnlAuthMiddleware, trmnlExclusions } from "./trmnlAuthMiddleware.js";
const maskApiKey = (apiKey: string) => {
return apiKey.slice(0, 15) + apiKey.slice(15).replace(/./g, "*");
};
const verifySecretKey = async (req: any, res: any, next: any) => {
const authHeader = req.headers.authorization || req.headers.Authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
const origin = req.get("origin");
if (isDashboardOrigin({ origin })) {
return withOrgAuth(req, res, next);
} else {
throw new RecaseError({
message: "Secret key not found in Authorization header",
code: ErrCode.NoSecretKey,
statusCode: 401,
});
}
}
const apiKey = authHeader.split(" ")[1];
if (!apiKey.startsWith("am_")) {
throw new RecaseError({
message: "Invalid secret key",
code: ErrCode.InvalidSecretKey,
statusCode: 401,
});
}
if (apiKey.startsWith("am_pk")) {
console.log("Verifying publishable key");
return await verifyBearerPublishableKey(apiKey, req, res, next);
}
const { valid, data } = await verifyKey({
db: req.db,
key: apiKey,
});
if (!valid || !data) {
throw new RecaseError({
message: "Invalid secret key",
code: ErrCode.InvalidSecretKey,
statusCode: 401,
});
}
const { org, features, env, userId } = data;
req.orgId = org.id;
req.env = env;
req.minOrg = {
id: org.id,
slug: org.slug,
};
req.org = org;
req.features = features;
req.authType = AuthType.SecretKey;
req.userId = userId;
const orgConfig = await req.headers["org-config"];
if (orgConfig) {
console.log("Org config found!: ", orgConfig);
const newConfigFields = JSON.parse(orgConfig);
try {
req.org.config = {
...org.config,
...newConfigFields,
};
} catch {
// Ignore parsing errors
}
}
next();
};
export const apiAuthMiddleware = async (req: any, res: any, next: any) => {
const logger = req.logger;
if (trmnlExclusions.includes(req.path)) {
logger.info(
`exluding TRMNL from auth middleware for device with ID ${req.headers["x-trmnl-id"] || "unknown"}`,
);
return await trmnlAuthMiddleware(req, res, next);
}
try {
await verifySecretKey(req, res, next);
return;
} catch (error: any) {
if (error instanceof RecaseError) {
if (error.code === ErrCode.InvalidSecretKey) {
const apiKey = req.headers.authorization?.split(" ")[1];
error.message = `Invalid secret key: ${maskApiKey(apiKey)}`;
}
logger.warn(`auth warning: ${error.message}`);
res.status(error.statusCode).json({
message: error.message,
code: error.code,
});
} else {
logger.error(`auth error: ${error.message}`, {
error,
});
res.status(500).json({
message: `Failed to verify secret key: ${error.message}`,
code: ErrCode.InternalError,
});
}
return;
}
};