@canmingir/link-express 1.6.10 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/.eslintrc.ts +29 -0
  2. package/index.ts +2 -0
  3. package/package.json +55 -12
  4. package/{prepare.js → prepare.ts} +1 -1
  5. package/src/{authorization.js → authorization.ts} +31 -9
  6. package/src/config.ts +112 -0
  7. package/src/dynamodb.ts +24 -0
  8. package/src/{error.js → error.ts} +30 -11
  9. package/src/express.ts +66 -0
  10. package/src/lib/{settings.js → settings.ts} +16 -4
  11. package/src/lib/test.ts +68 -0
  12. package/src/logger.ts +36 -0
  13. package/src/metrics/{dbMetrics.js → dbMetrics.ts} +37 -14
  14. package/src/models/Organization.model.ts +27 -0
  15. package/src/models/Permission.model.ts +48 -0
  16. package/src/models/Project.model.ts +50 -0
  17. package/src/models/Settings.model.ts +31 -0
  18. package/src/models/{index.js → index.ts} +8 -8
  19. package/src/platform.ts +55 -0
  20. package/src/postgres.ts +309 -0
  21. package/src/routes/index.ts +8 -0
  22. package/src/routes/metrics.ts +13 -0
  23. package/src/routes/oauth.ts +267 -0
  24. package/src/routes/{organizations.js → organizations.ts} +10 -8
  25. package/src/routes/{permissions.js → permissions.ts} +8 -6
  26. package/src/routes/{projects.js → projects.ts} +22 -16
  27. package/src/routes/settings.ts +31 -0
  28. package/src/schemas/{Organization.js → Organization.ts} +2 -2
  29. package/src/schemas/{Permission.js → Permission.ts} +2 -2
  30. package/src/schemas/{Project.js → Project.ts} +2 -2
  31. package/src/schemas/index.ts +5 -0
  32. package/src/sequelize.ts +13 -0
  33. package/src/{test.js → test.ts} +11 -13
  34. package/src/types/Organization.ts +9 -0
  35. package/src/types/Permission.ts +13 -0
  36. package/src/types/Project.ts +14 -0
  37. package/src/types/index.ts +5 -0
  38. package/tsconfig.json +32 -0
  39. package/.eslintrc.js +0 -20
  40. package/index.js +0 -1
  41. package/src/config.js +0 -21
  42. package/src/dynamodb.js +0 -18
  43. package/src/express.js +0 -58
  44. package/src/lib/test.js +0 -69
  45. package/src/models/Organization.js +0 -17
  46. package/src/models/Permission.js +0 -33
  47. package/src/models/Project.js +0 -37
  48. package/src/models/Settings.js +0 -21
  49. package/src/openapi.js +0 -40
  50. package/src/platform.js +0 -46
  51. package/src/postgres.js +0 -308
  52. package/src/routes/index.js +0 -15
  53. package/src/routes/metrics.js +0 -12
  54. package/src/routes/oauth.js +0 -213
  55. package/src/routes/settings.js +0 -25
  56. package/src/schemas/index.js +0 -5
@@ -1,213 +0,0 @@
1
- const Joi = require("joi");
2
- const router = require("express").Router();
3
- const jwt = require("jsonwebtoken");
4
- const axios = require("axios");
5
- const config = require("../config");
6
- const { AuthenticationError, AuthorizationError } = require("../error");
7
- const Permission = require("../models/Permission");
8
- const { project } = config();
9
-
10
- router.post("/", async (req, res) => {
11
- let { appId, projectId, code, refreshToken, redirectUri, provider } =
12
- Joi.attempt(
13
- req.body,
14
- Joi.object({
15
- appId: Joi.string().required(),
16
- projectId: Joi.string().optional(),
17
- code: Joi.string().optional(),
18
- refreshToken: Joi.string().optional(),
19
- redirectUri: Joi.string().optional(),
20
- provider: Joi.string().required(),
21
- })
22
- .required()
23
- .options({ stripUnknown: true })
24
- );
25
-
26
- if (!code && !refreshToken) {
27
- return res.status(400).send("Missing OAuth Code and Refresh Token");
28
- }
29
-
30
- const providerConfig = project.oauth.providers[provider];
31
- if (!providerConfig) {
32
- return res.status(400).send("Unsupported OAuth provider");
33
- }
34
-
35
- let accessTokenForAPI;
36
- let newRefreshToken = refreshToken;
37
-
38
- if (code && redirectUri) {
39
- const params = new URLSearchParams();
40
- params.append("grant_type", "authorization_code");
41
- params.append("client_id", providerConfig.clientId);
42
- params.append(
43
- "client_secret",
44
- process.env[`${provider.toUpperCase()}_CLIENT_SECRET`]
45
- );
46
- params.append("code", code);
47
- params.append("redirect_uri", redirectUri);
48
-
49
- const tokenResponse = await axios.post(
50
- providerConfig.tokenUrl,
51
- params.toString(),
52
- {
53
- headers: {
54
- "Content-Type": "application/x-www-form-urlencoded",
55
- Accept: "application/json",
56
- },
57
- timeout: 10000,
58
- }
59
- );
60
-
61
- if (tokenResponse.data.error) {
62
- throw new AuthorizationError(
63
- tokenResponse.data.error_description || tokenResponse.data.error
64
- );
65
- }
66
-
67
- if (!tokenResponse.data.access_token) {
68
- throw new AuthenticationError(
69
- "No access token received from OAuth provider"
70
- );
71
- }
72
-
73
- accessTokenForAPI = tokenResponse.data.access_token;
74
- newRefreshToken =
75
- tokenResponse.data.refresh_token || tokenResponse.data.access_token;
76
- } else {
77
- accessTokenForAPI = refreshToken;
78
- }
79
-
80
- let userResponse = await axios.get(providerConfig.userUrl, {
81
- headers: {
82
- Authorization: `Bearer ${accessTokenForAPI}`,
83
- Accept: "application/json",
84
- },
85
- timeout: 10000,
86
- });
87
-
88
- console.log("User info response:", userResponse.data);
89
-
90
- let userId;
91
- const identifierField = providerConfig.userIdentifier;
92
-
93
- if (userResponse.data[identifierField]) {
94
- userId = userResponse.data[identifierField].toString();
95
- } else if (userResponse.data[project.oauth.jwt.identifier]) {
96
- userId = userResponse.data[project.oauth.jwt.identifier].toString();
97
- } else {
98
- console.error("User identifier extraction failed:", {
99
- availableFields: Object.keys(userResponse.data),
100
- expectedField: identifierField,
101
- fallbackField: project.oauth.jwt.identifier,
102
- });
103
- throw new Error(
104
- `Cannot find user identifier in ${provider} OAuth response`
105
- );
106
- }
107
-
108
- let accessToken;
109
-
110
- if (projectId) {
111
- const permissions = await Permission.findAll({
112
- where: { userId, projectId, appId },
113
- });
114
-
115
- if (!permissions.length) {
116
- accessToken = jwt.sign(
117
- {
118
- sub: userId,
119
- iss: "nuc",
120
- aid: appId,
121
- provider: provider,
122
- iat: Math.floor(Date.now() / 1000),
123
- },
124
- process.env.JWT_SECRET,
125
- { expiresIn: "12h" }
126
- );
127
- } else {
128
- accessToken = jwt.sign(
129
- {
130
- sub: userId,
131
- iss: "nuc",
132
- aud: projectId,
133
- oid: permissions[0].organizationId,
134
- aid: appId,
135
- rls: permissions.map((permission) => permission.role),
136
- provider: provider,
137
- iat: Math.floor(Date.now() / 1000),
138
- },
139
- process.env.JWT_SECRET,
140
- { expiresIn: "12h" }
141
- );
142
- }
143
- } else {
144
- accessToken = jwt.sign(
145
- {
146
- sub: userId,
147
- iss: "nuc",
148
- aid: appId,
149
- provider: provider,
150
- iat: Math.floor(Date.now() / 1000),
151
- },
152
- process.env.JWT_SECRET,
153
- { expiresIn: "12h" }
154
- );
155
- }
156
-
157
- res.status(200).json({
158
- accessToken,
159
- refreshToken: newRefreshToken,
160
- });
161
- });
162
-
163
- router.get("/user", async (req, res) => {
164
- const authHeader = req.headers.authorization;
165
- const refreshTokenHeader = req.headers["x-refresh-token"];
166
-
167
- if (!authHeader || !authHeader.startsWith("Bearer ")) {
168
- return res.status(401).end();
169
- }
170
-
171
- if (!refreshTokenHeader) {
172
- return res.status(400).send("Missing refresh token");
173
- }
174
-
175
- const token = authHeader.split(" ")[1];
176
-
177
- const decoded = jwt.verify(token, process.env.JWT_SECRET);
178
- const userId = decoded.sub;
179
- const provider = decoded.provider;
180
-
181
- if (!userId || !provider) {
182
- return res.status(401).end();
183
- }
184
-
185
- const providerConfig = project.oauth.providers[provider];
186
- if (!providerConfig) {
187
- return res.status(400).send("Unsupported OAuth provider");
188
- }
189
-
190
- const userResponse = await axios.get(providerConfig.userUrl, {
191
- headers: {
192
- Authorization: `Bearer ${refreshTokenHeader}`,
193
- Accept: "application/json",
194
- },
195
- timeout: 10000,
196
- });
197
-
198
- const userFieldMapping = providerConfig.userFields;
199
- const userDetails = {
200
- id: userId,
201
- provider: provider,
202
- name: userResponse.data[userFieldMapping.name] || null,
203
- displayName: userResponse.data[userFieldMapping.displayName] || null,
204
- avatarUrl: userResponse.data[userFieldMapping.avatarUrl] || null,
205
- email: userResponse.data[userFieldMapping.email] || null,
206
- };
207
-
208
- res.status(200).json({
209
- user: userDetails,
210
- });
211
- });
212
-
213
- module.exports = router;
@@ -1,25 +0,0 @@
1
- const express = require("express");
2
- const router = express.Router({ mergeParams: true });
3
-
4
- const settings = require("../lib/settings");
5
- const { AuthorizationError } = require("../error");
6
-
7
- router.get("/", async (req, res) => {
8
- if (req.params.projectId !== req.session.projectId) {
9
- throw new AuthorizationError();
10
- }
11
-
12
- const setting = await settings.get(req.session, req.params);
13
- res.json(setting);
14
- });
15
-
16
- router.patch("/", async (req, res) => {
17
- if (req.params.projectId !== req.session.projectId) {
18
- throw new AuthorizationError();
19
- }
20
-
21
- await settings.upsert(req.session, req.body, req.params);
22
- res.end();
23
- });
24
-
25
- module.exports = router;
@@ -1,5 +0,0 @@
1
- const Permission = require("./Permission");
2
- const Organization = require("./Organization");
3
- const Project = require("./Project");
4
-
5
- module.exports = { Permission, Organization, Project };