fiber-firebase-functions 1.0.2 → 1.0.3
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.
- package/lib/auth/otp.js +95 -0
- package/lib/auth/otp.js.map +1 -0
- package/lib/auth/update_password.js +18 -7
- package/lib/auth/update_password.js.map +1 -1
- package/lib/common/config.js +60 -0
- package/lib/common/config.js.map +1 -0
- package/lib/email/send_email.js +81 -0
- package/lib/email/send_email.js.map +1 -0
- package/lib/middleware/rate_limiter.js +19 -6
- package/lib/middleware/rate_limiter.js.map +1 -1
- package/package.json +1 -1
- package/src/auth/otp.ts +70 -0
- package/src/auth/update_password.ts +20 -7
- package/src/common/config.ts +74 -0
- package/src/email/send_email.ts +48 -0
- package/src/middleware/rate_limiter.ts +25 -6
package/lib/auth/otp.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (C) 2025 Fiber
|
|
4
|
+
*
|
|
5
|
+
* All rights reserved. This script, including its code and logic, is the
|
|
6
|
+
* exclusive property of Fiber. Redistribution, reproduction,
|
|
7
|
+
* or modification of any part of this script is strictly prohibited
|
|
8
|
+
* without prior written permission from Fiber.
|
|
9
|
+
*
|
|
10
|
+
* Conditions of use:
|
|
11
|
+
* - The code may not be copied, duplicated, or used, in whole or in part,
|
|
12
|
+
* for any purpose without explicit authorization.
|
|
13
|
+
* - Redistribution of this code, with or without modification, is not
|
|
14
|
+
* permitted unless expressly agreed upon by Fiber.
|
|
15
|
+
* - The name "Fiber" and any associated branding, logos, or
|
|
16
|
+
* trademarks may not be used to endorse or promote derived products
|
|
17
|
+
* or services without prior written approval.
|
|
18
|
+
*
|
|
19
|
+
* Disclaimer:
|
|
20
|
+
* THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
23
|
+
* FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
|
|
25
|
+
* DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
|
|
26
|
+
* OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
*
|
|
28
|
+
* Unauthorized copying or reproduction of this script, in whole or in part,
|
|
29
|
+
* is a violation of applicable intellectual property laws and will result
|
|
30
|
+
* in legal action.
|
|
31
|
+
*/
|
|
32
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
33
|
+
if (k2 === undefined) k2 = k;
|
|
34
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
35
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
36
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
37
|
+
}
|
|
38
|
+
Object.defineProperty(o, k2, desc);
|
|
39
|
+
}) : (function(o, m, k, k2) {
|
|
40
|
+
if (k2 === undefined) k2 = k;
|
|
41
|
+
o[k2] = m[k];
|
|
42
|
+
}));
|
|
43
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
44
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
45
|
+
}) : function(o, v) {
|
|
46
|
+
o["default"] = v;
|
|
47
|
+
});
|
|
48
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
49
|
+
var ownKeys = function(o) {
|
|
50
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
51
|
+
var ar = [];
|
|
52
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
53
|
+
return ar;
|
|
54
|
+
};
|
|
55
|
+
return ownKeys(o);
|
|
56
|
+
};
|
|
57
|
+
return function (mod) {
|
|
58
|
+
if (mod && mod.__esModule) return mod;
|
|
59
|
+
var result = {};
|
|
60
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
61
|
+
__setModuleDefault(result, mod);
|
|
62
|
+
return result;
|
|
63
|
+
};
|
|
64
|
+
})();
|
|
65
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
|
+
exports.GenerateOTPStatus = void 0;
|
|
67
|
+
const admin = __importStar(require("firebase-admin"));
|
|
68
|
+
if (admin.apps.length === 0) {
|
|
69
|
+
admin.initializeApp();
|
|
70
|
+
}
|
|
71
|
+
var GenerateOTPStatus;
|
|
72
|
+
(function (GenerateOTPStatus) {
|
|
73
|
+
GenerateOTPStatus["MISSING_DATABASE_CONFIG"] = "MISSING_DATABASE_CONFIG";
|
|
74
|
+
GenerateOTPStatus["TOO_MANY_REQUEST"] = "TOO_MANY_REQUEST";
|
|
75
|
+
GenerateOTPStatus["SUCCESS"] = "SUCCESS";
|
|
76
|
+
GenerateOTPStatus["INTERNAL_ERROR"] = "INTERNAL_ERROR";
|
|
77
|
+
})(GenerateOTPStatus || (exports.GenerateOTPStatus = GenerateOTPStatus = {}));
|
|
78
|
+
// export async function generateOTP(identifier: GenerateOTPIdentifier): Promise<GenerateOTPStatus> {
|
|
79
|
+
// const config = appInitialize();
|
|
80
|
+
// const security = config.security;
|
|
81
|
+
// if (!security) return GenerateOTPStatus.MISSING_DATABASE_CONFIG;
|
|
82
|
+
// if (await isRateLimited(identifier, rule, databaseConfig) !== RateLimitCheckStatus.LIMIT_NOT_FOUND) {
|
|
83
|
+
// return GenerateOTPStatus.TOO_MANY_REQUEST;
|
|
84
|
+
// }
|
|
85
|
+
// await recordRateLimitHit(identifier, rule, databaseConfig);
|
|
86
|
+
// try {
|
|
87
|
+
// const database = realtimeDatabase(databaseConfig);
|
|
88
|
+
// const reference = database.ref("__fbs__otp").child("feature");
|
|
89
|
+
// const query = await reference.get();
|
|
90
|
+
// return GenerateOTPStatus.SUCCESS;
|
|
91
|
+
// } catch (e) {
|
|
92
|
+
// return GenerateOTPStatus.INTERNAL_ERROR;
|
|
93
|
+
// }
|
|
94
|
+
// }
|
|
95
|
+
//# sourceMappingURL=otp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp.js","sourceRoot":"","sources":["../../src/auth/otp.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,sDAAwC;AAExC,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;IAC1B,KAAK,CAAC,aAAa,EAAE,CAAC;AAC1B,CAAC;AAED,IAAY,iBAKX;AALD,WAAY,iBAAiB;IACzB,wEAAmD,CAAA;IACnD,0DAAqC,CAAA;IACrC,wCAAmB,CAAA;IACnB,sDAAiC,CAAA;AACrC,CAAC,EALW,iBAAiB,iCAAjB,iBAAiB,QAK5B;AAOD,qGAAqG;AACrG,sCAAsC;AACtC,wCAAwC;AAExC,uEAAuE;AAEvE,4GAA4G;AAC5G,qDAAqD;AACrD,QAAQ;AACR,kEAAkE;AAElE,YAAY;AACZ,6DAA6D;AAC7D,yEAAyE;AACzE,+CAA+C;AAE/C,4CAA4C;AAC5C,oBAAoB;AACpB,mDAAmD;AACnD,QAAQ;AACR,IAAI"}
|
|
@@ -67,6 +67,7 @@ exports.ResetPasswordByIdStatus = exports.ResetPasswordByEmailStatus = void 0;
|
|
|
67
67
|
exports.resetPasswordByEmail = resetPasswordByEmail;
|
|
68
68
|
exports.resetPasswordById = resetPasswordById;
|
|
69
69
|
const admin = __importStar(require("firebase-admin"));
|
|
70
|
+
const config_1 = require("../common/config");
|
|
70
71
|
const rate_limiter_1 = require("../middleware/rate_limiter");
|
|
71
72
|
const is_user_disabled_1 = require("./is_user_disabled");
|
|
72
73
|
const is_user_exists_1 = require("./is_user_exists");
|
|
@@ -76,6 +77,7 @@ if (admin.apps.length === 0) {
|
|
|
76
77
|
}
|
|
77
78
|
var ResetPasswordByEmailStatus;
|
|
78
79
|
(function (ResetPasswordByEmailStatus) {
|
|
80
|
+
ResetPasswordByEmailStatus["MISSING_DATABASE_CONFIG"] = "MISSING_DATABASE_CONFIG";
|
|
79
81
|
ResetPasswordByEmailStatus["MISSING_USER_EMAIL"] = "MISSING_USER_EMAIL";
|
|
80
82
|
ResetPasswordByEmailStatus["MISSING_NEW_PASSWORD"] = "MISSING_NEW_PASSWORD";
|
|
81
83
|
ResetPasswordByEmailStatus["MISSING_CONFIRM_NEW_PASSWORD"] = "MISSING_CONFIRM_NEW_PASSWORD";
|
|
@@ -94,7 +96,8 @@ var ResetPasswordByEmailStatus;
|
|
|
94
96
|
})(ResetPasswordByEmailStatus || (exports.ResetPasswordByEmailStatus = ResetPasswordByEmailStatus = {}));
|
|
95
97
|
var ResetPasswordByIdStatus;
|
|
96
98
|
(function (ResetPasswordByIdStatus) {
|
|
97
|
-
ResetPasswordByIdStatus["
|
|
99
|
+
ResetPasswordByIdStatus["MISSING_DATABASE_CONFIG"] = "MISSING_DATABASE_CONFIG";
|
|
100
|
+
ResetPasswordByIdStatus["MISSING_USER_ID"] = "MISSING_USER_ID";
|
|
98
101
|
ResetPasswordByIdStatus["MISSING_NEW_PASSWORD"] = "MISSING_NEW_PASSWORD";
|
|
99
102
|
ResetPasswordByIdStatus["MISSING_CONFIRM_NEW_PASSWORD"] = "MISSING_CONFIRM_NEW_PASSWORD";
|
|
100
103
|
ResetPasswordByIdStatus["MISSING_PASSWORD_POLICY"] = "MISSING_PASSWORD_POLICY";
|
|
@@ -110,7 +113,11 @@ var ResetPasswordByIdStatus;
|
|
|
110
113
|
ResetPasswordByIdStatus["SUCCESS"] = "SUCCESS";
|
|
111
114
|
ResetPasswordByIdStatus["INTERNAL_ERROR"] = "INTERNAL_ERROR";
|
|
112
115
|
})(ResetPasswordByIdStatus || (exports.ResetPasswordByIdStatus = ResetPasswordByIdStatus = {}));
|
|
113
|
-
async function resetPasswordByEmail(email, password
|
|
116
|
+
async function resetPasswordByEmail(email, password) {
|
|
117
|
+
const config = (0, config_1.appInitialize)();
|
|
118
|
+
const middleware = config.middleware;
|
|
119
|
+
if (!middleware)
|
|
120
|
+
return ResetPasswordByEmailStatus.MISSING_DATABASE_CONFIG;
|
|
114
121
|
email = email.trim();
|
|
115
122
|
if (!email)
|
|
116
123
|
return ResetPasswordByEmailStatus.MISSING_USER_EMAIL;
|
|
@@ -136,10 +143,10 @@ async function resetPasswordByEmail(email, password, databaseConfig) {
|
|
|
136
143
|
windowMs: 3 * 60 * 1000,
|
|
137
144
|
maxHits: 5,
|
|
138
145
|
};
|
|
139
|
-
if (await (0, rate_limiter_1.isRateLimited)(identifier, rule
|
|
146
|
+
if (await (0, rate_limiter_1.isRateLimited)(identifier, rule) !== rate_limiter_1.RateLimitCheckStatus.LIMIT_NOT_FOUND) {
|
|
140
147
|
return ResetPasswordByEmailStatus.TOO_MANY_REQUEST;
|
|
141
148
|
}
|
|
142
|
-
await (0, rate_limiter_1.recordRateLimitHit)(identifier, rule
|
|
149
|
+
await (0, rate_limiter_1.recordRateLimitHit)(identifier, rule);
|
|
143
150
|
if (await (0, is_user_disabled_1.isUserDisabledById)(user.uid))
|
|
144
151
|
return ResetPasswordByEmailStatus.USER_DISABLED;
|
|
145
152
|
if (newPassword !== confirmNewPassword)
|
|
@@ -166,9 +173,13 @@ async function resetPasswordByEmail(email, password, databaseConfig) {
|
|
|
166
173
|
}
|
|
167
174
|
}
|
|
168
175
|
async function resetPasswordById(userId, password, databaseConfig) {
|
|
176
|
+
const config = (0, config_1.appInitialize)();
|
|
177
|
+
const middleware = config.middleware;
|
|
178
|
+
if (!middleware)
|
|
179
|
+
return ResetPasswordByIdStatus.MISSING_DATABASE_CONFIG;
|
|
169
180
|
userId = userId.trim();
|
|
170
181
|
if (!userId)
|
|
171
|
-
return ResetPasswordByIdStatus.
|
|
182
|
+
return ResetPasswordByIdStatus.MISSING_USER_ID;
|
|
172
183
|
const newPassword = password.newPassword.trim();
|
|
173
184
|
const confirmNewPassword = password.confirmNewPassword.trim();
|
|
174
185
|
if (!newPassword || newPassword === "")
|
|
@@ -187,10 +198,10 @@ async function resetPasswordById(userId, password, databaseConfig) {
|
|
|
187
198
|
windowMs: 3 * 60 * 1000,
|
|
188
199
|
maxHits: 5,
|
|
189
200
|
};
|
|
190
|
-
if (await (0, rate_limiter_1.isRateLimited)(identifier, rule
|
|
201
|
+
if (await (0, rate_limiter_1.isRateLimited)(identifier, rule) !== rate_limiter_1.RateLimitCheckStatus.LIMIT_NOT_FOUND) {
|
|
191
202
|
return ResetPasswordByIdStatus.TOO_MANY_REQUEST;
|
|
192
203
|
}
|
|
193
|
-
await (0, rate_limiter_1.recordRateLimitHit)(identifier, rule
|
|
204
|
+
await (0, rate_limiter_1.recordRateLimitHit)(identifier, rule);
|
|
194
205
|
if (await (0, is_user_exists_1.isUserExistsById)(userId))
|
|
195
206
|
return ResetPasswordByIdStatus.USER_NOT_FOUND;
|
|
196
207
|
if (await (0, is_user_disabled_1.isUserDisabledById)(userId))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update_password.js","sourceRoot":"","sources":["../../src/auth/update_password.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"update_password.js","sourceRoot":"","sources":["../../src/auth/update_password.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEH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AAC,IAAI,CAAC,WAAW,CAAC;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC;IACzE,CAAC;IAED,IAAI,CAAC;QACD,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC,CAAC;QACjE,OAAO,uBAAuB,CAAC,OAAO,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAU,EAAE,CAAC;QAClB,OAAO,uBAAuB,CAAC,cAAc,CAAC;IAClD,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (C) 2025 Fiber
|
|
4
|
+
*
|
|
5
|
+
* All rights reserved. This script, including its code and logic, is the
|
|
6
|
+
* exclusive property of Fiber. Redistribution, reproduction,
|
|
7
|
+
* or modification of any part of this script is strictly prohibited
|
|
8
|
+
* without prior written permission from Fiber.
|
|
9
|
+
*
|
|
10
|
+
* Conditions of use:
|
|
11
|
+
* - The code may not be copied, duplicated, or used, in whole or in part,
|
|
12
|
+
* for any purpose without explicit authorization.
|
|
13
|
+
* - Redistribution of this code, with or without modification, is not
|
|
14
|
+
* permitted unless expressly agreed upon by Fiber.
|
|
15
|
+
* - The name "Fiber" and any associated branding, logos, or
|
|
16
|
+
* trademarks may not be used to endorse or promote derived products
|
|
17
|
+
* or services without prior written approval.
|
|
18
|
+
*
|
|
19
|
+
* Disclaimer:
|
|
20
|
+
* THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
23
|
+
* FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
|
|
25
|
+
* DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
|
|
26
|
+
* OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
*
|
|
28
|
+
* Unauthorized copying or reproduction of this script, in whole or in part,
|
|
29
|
+
* is a violation of applicable intellectual property laws and will result
|
|
30
|
+
* in legal action.
|
|
31
|
+
*/
|
|
32
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
33
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
34
|
+
};
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.appInitialize = appInitialize;
|
|
37
|
+
const fs_1 = __importDefault(require("fs"));
|
|
38
|
+
const path_1 = __importDefault(require("path"));
|
|
39
|
+
let cachedConfig = null;
|
|
40
|
+
function appInitialize() {
|
|
41
|
+
if (cachedConfig)
|
|
42
|
+
return cachedConfig;
|
|
43
|
+
const configPath = path_1.default.resolve(process.cwd(), "config", "app.json");
|
|
44
|
+
if (!fs_1.default.existsSync(configPath)) {
|
|
45
|
+
throw new Error(`Missing configuration file at: ${configPath}`);
|
|
46
|
+
}
|
|
47
|
+
const raw = fs_1.default.readFileSync(configPath, "utf-8");
|
|
48
|
+
const parsed = JSON.parse(raw);
|
|
49
|
+
cachedConfig = {
|
|
50
|
+
middleware: {
|
|
51
|
+
appName: parsed.middleware.app_name,
|
|
52
|
+
url: parsed.middleware.url
|
|
53
|
+
},
|
|
54
|
+
security: {
|
|
55
|
+
url: parsed.security.url,
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
return cachedConfig;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/common/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;AAqBH,sCAuBC;AA1CD,4CAAoB;AACpB,gDAAwB;AAgBxB,IAAI,YAAY,GAAqB,IAAI,CAAC;AAE1C,SAAgB,aAAa;IACzB,IAAI,YAAY;QAAE,OAAO,YAAY,CAAC;IAEtC,MAAM,UAAU,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC;IAErE,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,kCAAkC,UAAU,EAAE,CAAC,CAAC;IACpE,CAAC;IAED,MAAM,GAAG,GAAG,YAAE,CAAC,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE/B,YAAY,GAAG;QACX,UAAU,EAAE;YACR,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,QAAQ;YACnC,GAAG,EAAE,MAAM,CAAC,UAAU,CAAC,GAAG;SAC7B;QACD,QAAQ,EAAE;YACN,GAAG,EAAE,MAAM,CAAC,QAAQ,CAAC,GAAG;SAC3B;KACJ,CAAC;IAEF,OAAO,YAAY,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (C) 2025 Fiber
|
|
4
|
+
*
|
|
5
|
+
* All rights reserved. This script, including its code and logic, is the
|
|
6
|
+
* exclusive property of Fiber. Redistribution, reproduction,
|
|
7
|
+
* or modification of any part of this script is strictly prohibited
|
|
8
|
+
* without prior written permission from Fiber.
|
|
9
|
+
*
|
|
10
|
+
* Conditions of use:
|
|
11
|
+
* - The code may not be copied, duplicated, or used, in whole or in part,
|
|
12
|
+
* for any purpose without explicit authorization.
|
|
13
|
+
* - Redistribution of this code, with or without modification, is not
|
|
14
|
+
* permitted unless expressly agreed upon by Fiber.
|
|
15
|
+
* - The name "Fiber" and any associated branding, logos, or
|
|
16
|
+
* trademarks may not be used to endorse or promote derived products
|
|
17
|
+
* or services without prior written approval.
|
|
18
|
+
*
|
|
19
|
+
* Disclaimer:
|
|
20
|
+
* THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
23
|
+
* FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
|
|
25
|
+
* DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
|
|
26
|
+
* OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
*
|
|
28
|
+
* Unauthorized copying or reproduction of this script, in whole or in part,
|
|
29
|
+
* is a violation of applicable intellectual property laws and will result
|
|
30
|
+
* in legal action.
|
|
31
|
+
*/
|
|
32
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
33
|
+
if (k2 === undefined) k2 = k;
|
|
34
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
35
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
36
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
37
|
+
}
|
|
38
|
+
Object.defineProperty(o, k2, desc);
|
|
39
|
+
}) : (function(o, m, k, k2) {
|
|
40
|
+
if (k2 === undefined) k2 = k;
|
|
41
|
+
o[k2] = m[k];
|
|
42
|
+
}));
|
|
43
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
44
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
45
|
+
}) : function(o, v) {
|
|
46
|
+
o["default"] = v;
|
|
47
|
+
});
|
|
48
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
49
|
+
var ownKeys = function(o) {
|
|
50
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
51
|
+
var ar = [];
|
|
52
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
53
|
+
return ar;
|
|
54
|
+
};
|
|
55
|
+
return ownKeys(o);
|
|
56
|
+
};
|
|
57
|
+
return function (mod) {
|
|
58
|
+
if (mod && mod.__esModule) return mod;
|
|
59
|
+
var result = {};
|
|
60
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
61
|
+
__setModuleDefault(result, mod);
|
|
62
|
+
return result;
|
|
63
|
+
};
|
|
64
|
+
})();
|
|
65
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
|
+
exports.sendEmail = sendEmail;
|
|
67
|
+
const admin = __importStar(require("firebase-admin"));
|
|
68
|
+
if (admin.apps.length === 0) {
|
|
69
|
+
admin.initializeApp();
|
|
70
|
+
}
|
|
71
|
+
async function sendEmail(collection, to, subject, message) {
|
|
72
|
+
const firestore = admin.firestore();
|
|
73
|
+
await firestore.collection(collection).add({
|
|
74
|
+
to: [to],
|
|
75
|
+
message: {
|
|
76
|
+
subject: subject,
|
|
77
|
+
text: message,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=send_email.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send_email.js","sourceRoot":"","sources":["../../src/email/send_email.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQH,8BAUC;AAhBD,sDAAwC;AAExC,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;IAC1B,KAAK,CAAC,aAAa,EAAE,CAAC;AAC1B,CAAC;AAEM,KAAK,UAAU,SAAS,CAAC,UAAkB,EAAE,EAAU,EAAE,OAAe,EAAE,OAAe;IAC5F,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;IAEpC,MAAM,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC;QACvC,EAAE,EAAE,CAAC,EAAE,CAAC;QACR,OAAO,EAAE;YACL,OAAO,EAAE,OAAO;YAChB,IAAI,EAAE,OAAO;SAChB;KACJ,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -67,6 +67,7 @@ exports.SetRateLimitStatus = exports.RateLimitCheckStatus = void 0;
|
|
|
67
67
|
exports.isRateLimited = isRateLimited;
|
|
68
68
|
exports.recordRateLimitHit = recordRateLimitHit;
|
|
69
69
|
const admin = __importStar(require("firebase-admin"));
|
|
70
|
+
const config_1 = require("../common/config");
|
|
70
71
|
const realtime_database_1 = require("../common/realtime_database");
|
|
71
72
|
if (!admin.apps.length) {
|
|
72
73
|
admin.initializeApp();
|
|
@@ -93,7 +94,7 @@ var SetRateLimitStatus;
|
|
|
93
94
|
SetRateLimitStatus["ALLOWED"] = "ALLOWED";
|
|
94
95
|
SetRateLimitStatus["INTERNAL_ERROR"] = "INTERNAL_ERROR";
|
|
95
96
|
})(SetRateLimitStatus || (exports.SetRateLimitStatus = SetRateLimitStatus = {}));
|
|
96
|
-
async function isRateLimited(identifier, rule
|
|
97
|
+
async function isRateLimited(identifier, rule) {
|
|
97
98
|
if (!identifier)
|
|
98
99
|
return RateLimitCheckStatus.MISSING_IDENTIFIER;
|
|
99
100
|
if (!identifier.id)
|
|
@@ -102,11 +103,17 @@ async function isRateLimited(identifier, rule, databaseConfig) {
|
|
|
102
103
|
return RateLimitCheckStatus.MISSING_IDENTIFIER_TARGET;
|
|
103
104
|
if (!rule)
|
|
104
105
|
return RateLimitCheckStatus.MISSING_RULE;
|
|
105
|
-
|
|
106
|
+
const config = (0, config_1.appInitialize)();
|
|
107
|
+
const middleware = config.middleware;
|
|
108
|
+
if (!middleware)
|
|
106
109
|
return RateLimitCheckStatus.MISSING_DATABASE_CONFIG;
|
|
107
110
|
try {
|
|
111
|
+
const databaseConfig = {
|
|
112
|
+
appName: middleware.appName,
|
|
113
|
+
url: middleware.url,
|
|
114
|
+
};
|
|
108
115
|
const database = (0, realtime_database_1.realtimeDatabase)(databaseConfig);
|
|
109
|
-
const reference = database.ref(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
116
|
+
const reference = database.ref("__fbs__limiter").child(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
110
117
|
const query = await reference.get();
|
|
111
118
|
if (!query.exists())
|
|
112
119
|
return RateLimitCheckStatus.LIMIT_NOT_FOUND;
|
|
@@ -125,7 +132,7 @@ async function isRateLimited(identifier, rule, databaseConfig) {
|
|
|
125
132
|
return RateLimitCheckStatus.INTERNAL_ERROR;
|
|
126
133
|
}
|
|
127
134
|
}
|
|
128
|
-
async function recordRateLimitHit(identifier, rule
|
|
135
|
+
async function recordRateLimitHit(identifier, rule) {
|
|
129
136
|
if (!identifier)
|
|
130
137
|
return SetRateLimitStatus.MISSING_IDENTIFIER;
|
|
131
138
|
if (!identifier.id)
|
|
@@ -134,11 +141,17 @@ async function recordRateLimitHit(identifier, rule, databaseConfig) {
|
|
|
134
141
|
return SetRateLimitStatus.MISSING_IDENTIFIER_TARGET;
|
|
135
142
|
if (!rule)
|
|
136
143
|
return SetRateLimitStatus.MISSING_RULE;
|
|
137
|
-
|
|
144
|
+
const config = (0, config_1.appInitialize)();
|
|
145
|
+
const middleware = config.middleware;
|
|
146
|
+
if (!middleware)
|
|
138
147
|
return SetRateLimitStatus.MISSING_DATABASE_CONFIG;
|
|
139
148
|
try {
|
|
149
|
+
const databaseConfig = {
|
|
150
|
+
appName: middleware.appName,
|
|
151
|
+
url: middleware.url,
|
|
152
|
+
};
|
|
140
153
|
const database = (0, realtime_database_1.realtimeDatabase)(databaseConfig);
|
|
141
|
-
const reference = database.ref(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
154
|
+
const reference = database.ref("__fbs__limiter").child(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
142
155
|
const query = await reference.get();
|
|
143
156
|
let timestamps = query.exists() ? query.val() : [];
|
|
144
157
|
const now = Date.now();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate_limiter.js","sourceRoot":"","sources":["../../src/middleware/rate_limiter.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"rate_limiter.js","sourceRoot":"","sources":["../../src/middleware/rate_limiter.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CH,sCAyCC;AAED,gDAqCC;AAzHD,sDAAwC;AACxC,6CAAiD;AACjD,mEAAiF;AAEjF,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;IACrB,KAAK,CAAC,aAAa,EAAE,CAAC;AAC1B,CAAC;AAED,IAAY,oBASX;AATD,WAAY,oBAAoB;IAC5B,iEAAyC,CAAA;IACzC,uEAA+C,CAAA;IAC/C,+EAAuD,CAAA;IACvD,qDAA6B,CAAA;IAC7B,2EAAmD,CAAA;IACnD,qDAA6B,CAAA;IAC7B,2DAAmC,CAAA;IACnC,yDAAiC,CAAA;AACrC,CAAC,EATW,oBAAoB,o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rC,OAAO,EAAE,UAAU,CAAC,OAAO;YAC3B,GAAG,EAAE,UAAU,CAAC,GAAG;SACtB,CAAC;QAEF,MAAM,QAAQ,GAAG,IAAA,oCAAgB,EAAC,cAAc,CAAC,CAAC;QAClD,MAAM,SAAS,GAAG,QAAQ,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAC1H,MAAM,KAAK,GAAG,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC;QAEpC,IAAI,UAAU,GAAa,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7D,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAEvB,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;QAE/D,IAAI,UAAU,CAAC,MAAM,IAAI,IAAI,CAAC,OAAO;YAAE,OAAO,kBAAkB,CAAC,YAAY,CAAC;QAE9E,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACrB,MAAM,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAEhC,OAAO,kBAAkB,CAAC,OAAO,CAAC;IACtC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,kBAAkB,CAAC,cAAc,CAAC;IAC7C,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fiber-firebase-functions",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "A collection of ready-to-use Firebase Cloud Functions utilities and wrappers designed for any application built by Fiber. Provides reusable helpers, common patterns, and production-grade modules to streamline backend development across all Fiber projects.",
|
|
5
5
|
"author": "Fiber",
|
|
6
6
|
"license": "FIBER-PROPRIETARY",
|
package/src/auth/otp.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Fiber
|
|
3
|
+
*
|
|
4
|
+
* All rights reserved. This script, including its code and logic, is the
|
|
5
|
+
* exclusive property of Fiber. Redistribution, reproduction,
|
|
6
|
+
* or modification of any part of this script is strictly prohibited
|
|
7
|
+
* without prior written permission from Fiber.
|
|
8
|
+
*
|
|
9
|
+
* Conditions of use:
|
|
10
|
+
* - The code may not be copied, duplicated, or used, in whole or in part,
|
|
11
|
+
* for any purpose without explicit authorization.
|
|
12
|
+
* - Redistribution of this code, with or without modification, is not
|
|
13
|
+
* permitted unless expressly agreed upon by Fiber.
|
|
14
|
+
* - The name "Fiber" and any associated branding, logos, or
|
|
15
|
+
* trademarks may not be used to endorse or promote derived products
|
|
16
|
+
* or services without prior written approval.
|
|
17
|
+
*
|
|
18
|
+
* Disclaimer:
|
|
19
|
+
* THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
22
|
+
* FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
|
|
24
|
+
* DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
|
|
25
|
+
* OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
+
*
|
|
27
|
+
* Unauthorized copying or reproduction of this script, in whole or in part,
|
|
28
|
+
* is a violation of applicable intellectual property laws and will result
|
|
29
|
+
* in legal action.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import * as admin from "firebase-admin";
|
|
33
|
+
|
|
34
|
+
if (admin.apps.length === 0) {
|
|
35
|
+
admin.initializeApp();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export enum GenerateOTPStatus {
|
|
39
|
+
MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
|
|
40
|
+
TOO_MANY_REQUEST = "TOO_MANY_REQUEST",
|
|
41
|
+
SUCCESS = "SUCCESS",
|
|
42
|
+
INTERNAL_ERROR = "INTERNAL_ERROR",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface GenerateOTPIdentifier {
|
|
46
|
+
id: string;
|
|
47
|
+
target: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// export async function generateOTP(identifier: GenerateOTPIdentifier): Promise<GenerateOTPStatus> {
|
|
51
|
+
// const config = appInitialize();
|
|
52
|
+
// const security = config.security;
|
|
53
|
+
|
|
54
|
+
// if (!security) return GenerateOTPStatus.MISSING_DATABASE_CONFIG;
|
|
55
|
+
|
|
56
|
+
// if (await isRateLimited(identifier, rule, databaseConfig) !== RateLimitCheckStatus.LIMIT_NOT_FOUND) {
|
|
57
|
+
// return GenerateOTPStatus.TOO_MANY_REQUEST;
|
|
58
|
+
// }
|
|
59
|
+
// await recordRateLimitHit(identifier, rule, databaseConfig);
|
|
60
|
+
|
|
61
|
+
// try {
|
|
62
|
+
// const database = realtimeDatabase(databaseConfig);
|
|
63
|
+
// const reference = database.ref("__fbs__otp").child("feature");
|
|
64
|
+
// const query = await reference.get();
|
|
65
|
+
|
|
66
|
+
// return GenerateOTPStatus.SUCCESS;
|
|
67
|
+
// } catch (e) {
|
|
68
|
+
// return GenerateOTPStatus.INTERNAL_ERROR;
|
|
69
|
+
// }
|
|
70
|
+
// }
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import * as admin from "firebase-admin";
|
|
33
|
+
import { appInitialize } from "../common/config";
|
|
33
34
|
import { RealtimeDatabase } from "../common/realtime_database";
|
|
34
35
|
import { isRateLimited, RateLimitCheckStatus, RateLimitIdentifier, RateLimitRule, recordRateLimitHit } from "../middleware/rate_limiter";
|
|
35
36
|
import { isUserDisabledById } from "./is_user_disabled";
|
|
@@ -41,6 +42,7 @@ if (admin.apps.length === 0) {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
export enum ResetPasswordByEmailStatus {
|
|
45
|
+
MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
|
|
44
46
|
MISSING_USER_EMAIL = "MISSING_USER_EMAIL",
|
|
45
47
|
MISSING_NEW_PASSWORD = "MISSING_NEW_PASSWORD",
|
|
46
48
|
MISSING_CONFIRM_NEW_PASSWORD = "MISSING_CONFIRM_NEW_PASSWORD",
|
|
@@ -59,7 +61,8 @@ export enum ResetPasswordByEmailStatus {
|
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
export enum ResetPasswordByIdStatus {
|
|
62
|
-
|
|
64
|
+
MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
|
|
65
|
+
MISSING_USER_ID = "MISSING_USER_ID",
|
|
63
66
|
MISSING_NEW_PASSWORD = "MISSING_NEW_PASSWORD",
|
|
64
67
|
MISSING_CONFIRM_NEW_PASSWORD = "MISSING_CONFIRM_NEW_PASSWORD",
|
|
65
68
|
MISSING_PASSWORD_POLICY = "MISSING_PASSWORD_POLICY",
|
|
@@ -90,7 +93,12 @@ export interface ResetPassword {
|
|
|
90
93
|
passwordPolicy: PasswordPolicy;
|
|
91
94
|
}
|
|
92
95
|
|
|
93
|
-
export async function resetPasswordByEmail(email: string, password: ResetPassword
|
|
96
|
+
export async function resetPasswordByEmail(email: string, password: ResetPassword): Promise<ResetPasswordByEmailStatus> {
|
|
97
|
+
const config = appInitialize();
|
|
98
|
+
const middleware = config.middleware;
|
|
99
|
+
|
|
100
|
+
if (!middleware) return ResetPasswordByEmailStatus.MISSING_DATABASE_CONFIG;
|
|
101
|
+
|
|
94
102
|
email = email.trim();
|
|
95
103
|
|
|
96
104
|
if (!email) return ResetPasswordByEmailStatus.MISSING_USER_EMAIL;
|
|
@@ -121,10 +129,10 @@ export async function resetPasswordByEmail(email: string, password: ResetPasswor
|
|
|
121
129
|
maxHits: 5,
|
|
122
130
|
};
|
|
123
131
|
|
|
124
|
-
if (await isRateLimited(identifier, rule
|
|
132
|
+
if (await isRateLimited(identifier, rule) !== RateLimitCheckStatus.LIMIT_NOT_FOUND) {
|
|
125
133
|
return ResetPasswordByEmailStatus.TOO_MANY_REQUEST;
|
|
126
134
|
}
|
|
127
|
-
await recordRateLimitHit(identifier, rule
|
|
135
|
+
await recordRateLimitHit(identifier, rule);
|
|
128
136
|
|
|
129
137
|
if (await isUserDisabledById(user.uid)) return ResetPasswordByEmailStatus.USER_DISABLED;
|
|
130
138
|
|
|
@@ -153,9 +161,14 @@ export async function resetPasswordByEmail(email: string, password: ResetPasswor
|
|
|
153
161
|
}
|
|
154
162
|
|
|
155
163
|
export async function resetPasswordById(userId: string, password: ResetPassword, databaseConfig: RealtimeDatabase): Promise<ResetPasswordByIdStatus> {
|
|
164
|
+
const config = appInitialize();
|
|
165
|
+
const middleware = config.middleware;
|
|
166
|
+
|
|
167
|
+
if (!middleware) return ResetPasswordByIdStatus.MISSING_DATABASE_CONFIG;
|
|
168
|
+
|
|
156
169
|
userId = userId.trim();
|
|
157
170
|
|
|
158
|
-
if (!userId) return ResetPasswordByIdStatus.
|
|
171
|
+
if (!userId) return ResetPasswordByIdStatus.MISSING_USER_ID;
|
|
159
172
|
|
|
160
173
|
const newPassword = password.newPassword.trim();
|
|
161
174
|
const confirmNewPassword = password.confirmNewPassword.trim();
|
|
@@ -178,10 +191,10 @@ export async function resetPasswordById(userId: string, password: ResetPassword,
|
|
|
178
191
|
maxHits: 5,
|
|
179
192
|
};
|
|
180
193
|
|
|
181
|
-
if (await isRateLimited(identifier, rule
|
|
194
|
+
if (await isRateLimited(identifier, rule) !== RateLimitCheckStatus.LIMIT_NOT_FOUND) {
|
|
182
195
|
return ResetPasswordByIdStatus.TOO_MANY_REQUEST;
|
|
183
196
|
}
|
|
184
|
-
await recordRateLimitHit(identifier, rule
|
|
197
|
+
await recordRateLimitHit(identifier, rule);
|
|
185
198
|
|
|
186
199
|
if (await isUserExistsById(userId)) return ResetPasswordByIdStatus.USER_NOT_FOUND;
|
|
187
200
|
if (await isUserDisabledById(userId)) return ResetPasswordByIdStatus.USER_DISABLED;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Fiber
|
|
3
|
+
*
|
|
4
|
+
* All rights reserved. This script, including its code and logic, is the
|
|
5
|
+
* exclusive property of Fiber. Redistribution, reproduction,
|
|
6
|
+
* or modification of any part of this script is strictly prohibited
|
|
7
|
+
* without prior written permission from Fiber.
|
|
8
|
+
*
|
|
9
|
+
* Conditions of use:
|
|
10
|
+
* - The code may not be copied, duplicated, or used, in whole or in part,
|
|
11
|
+
* for any purpose without explicit authorization.
|
|
12
|
+
* - Redistribution of this code, with or without modification, is not
|
|
13
|
+
* permitted unless expressly agreed upon by Fiber.
|
|
14
|
+
* - The name "Fiber" and any associated branding, logos, or
|
|
15
|
+
* trademarks may not be used to endorse or promote derived products
|
|
16
|
+
* or services without prior written approval.
|
|
17
|
+
*
|
|
18
|
+
* Disclaimer:
|
|
19
|
+
* THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
22
|
+
* FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
|
|
24
|
+
* DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
|
|
25
|
+
* OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
+
*
|
|
27
|
+
* Unauthorized copying or reproduction of this script, in whole or in part,
|
|
28
|
+
* is a violation of applicable intellectual property laws and will result
|
|
29
|
+
* in legal action.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import fs from "fs";
|
|
33
|
+
import path from "path";
|
|
34
|
+
|
|
35
|
+
export interface AppConfig {
|
|
36
|
+
middleware: Middleware;
|
|
37
|
+
security: Security;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface Middleware {
|
|
41
|
+
appName: string;
|
|
42
|
+
url: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Security {
|
|
46
|
+
url: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let cachedConfig: AppConfig | null = null;
|
|
50
|
+
|
|
51
|
+
export function appInitialize(): AppConfig {
|
|
52
|
+
if (cachedConfig) return cachedConfig;
|
|
53
|
+
|
|
54
|
+
const configPath = path.resolve(process.cwd(), "config", "app.json");
|
|
55
|
+
|
|
56
|
+
if (!fs.existsSync(configPath)) {
|
|
57
|
+
throw new Error(`Missing configuration file at: ${configPath}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const raw = fs.readFileSync(configPath, "utf-8");
|
|
61
|
+
const parsed = JSON.parse(raw);
|
|
62
|
+
|
|
63
|
+
cachedConfig = {
|
|
64
|
+
middleware: {
|
|
65
|
+
appName: parsed.middleware.app_name,
|
|
66
|
+
url: parsed.middleware.url
|
|
67
|
+
},
|
|
68
|
+
security: {
|
|
69
|
+
url: parsed.security.url,
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return cachedConfig;
|
|
74
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Fiber
|
|
3
|
+
*
|
|
4
|
+
* All rights reserved. This script, including its code and logic, is the
|
|
5
|
+
* exclusive property of Fiber. Redistribution, reproduction,
|
|
6
|
+
* or modification of any part of this script is strictly prohibited
|
|
7
|
+
* without prior written permission from Fiber.
|
|
8
|
+
*
|
|
9
|
+
* Conditions of use:
|
|
10
|
+
* - The code may not be copied, duplicated, or used, in whole or in part,
|
|
11
|
+
* for any purpose without explicit authorization.
|
|
12
|
+
* - Redistribution of this code, with or without modification, is not
|
|
13
|
+
* permitted unless expressly agreed upon by Fiber.
|
|
14
|
+
* - The name "Fiber" and any associated branding, logos, or
|
|
15
|
+
* trademarks may not be used to endorse or promote derived products
|
|
16
|
+
* or services without prior written approval.
|
|
17
|
+
*
|
|
18
|
+
* Disclaimer:
|
|
19
|
+
* THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
22
|
+
* FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
|
|
24
|
+
* DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
|
|
25
|
+
* OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
+
*
|
|
27
|
+
* Unauthorized copying or reproduction of this script, in whole or in part,
|
|
28
|
+
* is a violation of applicable intellectual property laws and will result
|
|
29
|
+
* in legal action.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import * as admin from "firebase-admin";
|
|
33
|
+
|
|
34
|
+
if (admin.apps.length === 0) {
|
|
35
|
+
admin.initializeApp();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function sendEmail(collection: string, to: string, subject: string, message: string): Promise<void> {
|
|
39
|
+
const firestore = admin.firestore();
|
|
40
|
+
|
|
41
|
+
await firestore.collection(collection).add({
|
|
42
|
+
to: [to],
|
|
43
|
+
message: {
|
|
44
|
+
subject: subject,
|
|
45
|
+
text: message,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import * as admin from 'firebase-admin';
|
|
33
|
+
import { appInitialize } from '../common/config';
|
|
33
34
|
import { RealtimeDatabase, realtimeDatabase } from '../common/realtime_database';
|
|
34
35
|
|
|
35
36
|
if (!admin.apps.length) {
|
|
@@ -69,18 +70,27 @@ export interface RateLimitRule {
|
|
|
69
70
|
maxHits: number;
|
|
70
71
|
}
|
|
71
72
|
|
|
72
|
-
export async function isRateLimited(identifier: RateLimitIdentifier, rule: RateLimitRule
|
|
73
|
+
export async function isRateLimited(identifier: RateLimitIdentifier, rule: RateLimitRule): Promise<RateLimitCheckStatus> {
|
|
73
74
|
if (!identifier) return RateLimitCheckStatus.MISSING_IDENTIFIER;
|
|
74
75
|
|
|
75
76
|
if (!identifier.id) return RateLimitCheckStatus.MISSING_IDENTIFIER_ID;
|
|
76
77
|
if (!identifier.target) return RateLimitCheckStatus.MISSING_IDENTIFIER_TARGET;
|
|
77
78
|
|
|
78
79
|
if (!rule) return RateLimitCheckStatus.MISSING_RULE;
|
|
79
|
-
|
|
80
|
+
|
|
81
|
+
const config = appInitialize();
|
|
82
|
+
const middleware = config.middleware;
|
|
83
|
+
|
|
84
|
+
if (!middleware) return RateLimitCheckStatus.MISSING_DATABASE_CONFIG;
|
|
80
85
|
|
|
81
86
|
try {
|
|
87
|
+
const databaseConfig: RealtimeDatabase = {
|
|
88
|
+
appName: middleware.appName,
|
|
89
|
+
url: middleware.url,
|
|
90
|
+
};
|
|
91
|
+
|
|
82
92
|
const database = realtimeDatabase(databaseConfig);
|
|
83
|
-
const reference = database.ref(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
93
|
+
const reference = database.ref("__fbs__limiter").child(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
84
94
|
const query = await reference.get();
|
|
85
95
|
|
|
86
96
|
if (!query.exists()) return RateLimitCheckStatus.LIMIT_NOT_FOUND;
|
|
@@ -103,18 +113,27 @@ export async function isRateLimited(identifier: RateLimitIdentifier, rule: RateL
|
|
|
103
113
|
}
|
|
104
114
|
}
|
|
105
115
|
|
|
106
|
-
export async function recordRateLimitHit(identifier: RateLimitIdentifier, rule: RateLimitRule
|
|
116
|
+
export async function recordRateLimitHit(identifier: RateLimitIdentifier, rule: RateLimitRule): Promise<SetRateLimitStatus> {
|
|
107
117
|
if (!identifier) return SetRateLimitStatus.MISSING_IDENTIFIER;
|
|
108
118
|
|
|
109
119
|
if (!identifier.id) return SetRateLimitStatus.MISSING_IDENTIFIER_ID;
|
|
110
120
|
if (!identifier.target) return SetRateLimitStatus.MISSING_IDENTIFIER_TARGET;
|
|
111
121
|
|
|
112
122
|
if (!rule) return SetRateLimitStatus.MISSING_RULE;
|
|
113
|
-
|
|
123
|
+
|
|
124
|
+
const config = appInitialize();
|
|
125
|
+
const middleware = config.middleware;
|
|
126
|
+
|
|
127
|
+
if (!middleware) return SetRateLimitStatus.MISSING_DATABASE_CONFIG;
|
|
114
128
|
|
|
115
129
|
try {
|
|
130
|
+
const databaseConfig: RealtimeDatabase = {
|
|
131
|
+
appName: middleware.appName,
|
|
132
|
+
url: middleware.url,
|
|
133
|
+
};
|
|
134
|
+
|
|
116
135
|
const database = realtimeDatabase(databaseConfig);
|
|
117
|
-
const reference = database.ref(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
136
|
+
const reference = database.ref("__fbs__limiter").child(identifier.id).child(identifier.target).child("__fbs__timestamps");
|
|
118
137
|
const query = await reference.get();
|
|
119
138
|
|
|
120
139
|
let timestamps: number[] = query.exists() ? query.val() : [];
|