@appweaver/core 1.0.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.
- package/LICENSE +1 -0
- package/README.md +7 -0
- package/app/application.d.ts +43 -0
- package/app/application.js +112 -0
- package/app/create-app.d.ts +32 -0
- package/app/create-app.js +38 -0
- package/app/index.d.ts +2 -0
- package/app/index.js +18 -0
- package/app/lifecycle-manager.d.ts +20 -0
- package/app/lifecycle-manager.js +42 -0
- package/app/load-modules.d.ts +10 -0
- package/app/load-modules.js +50 -0
- package/app/load-providers.d.ts +8 -0
- package/app/load-providers.js +33 -0
- package/cache/cache-service.d.ts +72 -0
- package/cache/cache-service.js +202 -0
- package/cache/cache.d.ts +12 -0
- package/cache/cache.js +184 -0
- package/cache/caching.d.ts +8 -0
- package/cache/caching.js +72 -0
- package/cache/eviction/eviction-index.d.ts +35 -0
- package/cache/eviction/eviction-index.js +2 -0
- package/cache/eviction/fifo-eviction-index.d.ts +7 -0
- package/cache/eviction/fifo-eviction-index.js +28 -0
- package/cache/eviction/index.d.ts +4 -0
- package/cache/eviction/index.js +20 -0
- package/cache/eviction/lfu-eviction-index.d.ts +8 -0
- package/cache/eviction/lfu-eviction-index.js +82 -0
- package/cache/eviction/lru-eviction-index.d.ts +8 -0
- package/cache/eviction/lru-eviction-index.js +81 -0
- package/cache/index.d.ts +2 -0
- package/cache/index.js +18 -0
- package/cache/memory-cache.d.ts +4 -0
- package/cache/memory-cache.js +12 -0
- package/cache/redis-cache.d.ts +4 -0
- package/cache/redis-cache.js +12 -0
- package/context/context.d.ts +2 -0
- package/context/context.js +13 -0
- package/context/dependency-injection.d.ts +98 -0
- package/context/dependency-injection.js +276 -0
- package/context/index.d.ts +2 -0
- package/context/index.js +18 -0
- package/database/create-client.d.ts +11 -0
- package/database/create-client.js +142 -0
- package/database/index.d.ts +1 -0
- package/database/index.js +17 -0
- package/database/prisma-database.d.ts +15 -0
- package/database/prisma-database.js +53 -0
- package/errors/error-handler.d.ts +3 -0
- package/errors/error-handler.js +24 -0
- package/errors/error-schema.d.ts +32 -0
- package/errors/error-schema.js +31 -0
- package/errors/http-error.d.ts +21 -0
- package/errors/http-error.js +33 -0
- package/errors/index.d.ts +3 -0
- package/errors/index.js +19 -0
- package/events/node-events.d.ts +7 -0
- package/events/node-events.js +39 -0
- package/export/export-service.d.ts +19 -0
- package/export/export-service.js +154 -0
- package/export/index.d.ts +1 -0
- package/export/index.js +17 -0
- package/factory/create-model.d.ts +2 -0
- package/factory/create-model.js +366 -0
- package/factory/create-policy.d.ts +2 -0
- package/factory/create-policy.js +16 -0
- package/factory/create-routes.d.ts +2 -0
- package/factory/create-routes.js +21 -0
- package/factory/create-service.d.ts +3 -0
- package/factory/create-service.js +97 -0
- package/factory/index.d.ts +4 -0
- package/factory/index.js +20 -0
- package/health/health-routes.d.ts +2 -0
- package/health/health-routes.js +36 -0
- package/health/health-schema.d.ts +45 -0
- package/health/health-schema.js +52 -0
- package/health/health-service.d.ts +38 -0
- package/health/health-service.js +82 -0
- package/health/index.d.ts +2 -0
- package/health/index.js +18 -0
- package/index.d.ts +15 -0
- package/index.js +31 -0
- package/mailer/email-service.d.ts +36 -0
- package/mailer/email-service.js +89 -0
- package/mailer/index.d.ts +1 -0
- package/mailer/index.js +17 -0
- package/mailer/json-mailer.d.ts +7 -0
- package/mailer/json-mailer.js +32 -0
- package/mailer/smtp-mailer.d.ts +7 -0
- package/mailer/smtp-mailer.js +43 -0
- package/memory/in-memory.d.ts +24 -0
- package/memory/in-memory.js +164 -0
- package/memory/redis.d.ts +26 -0
- package/memory/redis.js +158 -0
- package/package.json +68 -0
- package/prisma/client/browser.d.ts +34 -0
- package/prisma/client/browser.js +56 -0
- package/prisma/client/client.d.ts +53 -0
- package/prisma/client/client.js +70 -0
- package/prisma/client/commonInputTypes.d.ts +395 -0
- package/prisma/client/commonInputTypes.js +11 -0
- package/prisma/client/enums.d.ts +1 -0
- package/prisma/client/enums.js +11 -0
- package/prisma/client/internal/class.d.ts +187 -0
- package/prisma/client/internal/class.js +85 -0
- package/prisma/client/internal/prismaNamespace.d.ts +987 -0
- package/prisma/client/internal/prismaNamespace.js +193 -0
- package/prisma/client/internal/prismaNamespaceBrowser.d.ts +125 -0
- package/prisma/client/internal/prismaNamespaceBrowser.js +164 -0
- package/prisma/client/models/ApiKey.d.ts +1167 -0
- package/prisma/client/models/ApiKey.js +2 -0
- package/prisma/client/models/File.d.ts +1248 -0
- package/prisma/client/models/File.js +2 -0
- package/prisma/client/models/OneTimeToken.d.ts +1083 -0
- package/prisma/client/models/OneTimeToken.js +2 -0
- package/prisma/client/models/Permission.d.ts +1209 -0
- package/prisma/client/models/Permission.js +2 -0
- package/prisma/client/models/Role.d.ts +1209 -0
- package/prisma/client/models/Role.js +2 -0
- package/prisma/client/models/Seeder.d.ts +1023 -0
- package/prisma/client/models/Seeder.js +2 -0
- package/prisma/client/models.d.ts +7 -0
- package/prisma/client/models.js +2 -0
- package/queue/bull-queue.d.ts +37 -0
- package/queue/bull-queue.js +207 -0
- package/queue/memory-queue.d.ts +54 -0
- package/queue/memory-queue.js +283 -0
- package/resource/index.d.ts +4 -0
- package/resource/index.js +20 -0
- package/resource/resource-loader.d.ts +26 -0
- package/resource/resource-loader.js +161 -0
- package/resource/resource-routes.d.ts +3 -0
- package/resource/resource-routes.js +174 -0
- package/resource/resource-schema.d.ts +33 -0
- package/resource/resource-schema.js +186 -0
- package/resource/resource-service.d.ts +89 -0
- package/resource/resource-service.js +772 -0
- package/resources.d.ts +9 -0
- package/resources.js +24 -0
- package/scheduler/cron-scheduler.d.ts +12 -0
- package/scheduler/cron-scheduler.js +73 -0
- package/security/account/account-routes.d.ts +2 -0
- package/security/account/account-routes.js +104 -0
- package/security/account/account-schema.d.ts +290 -0
- package/security/account/account-schema.js +166 -0
- package/security/account/account-service.d.ts +88 -0
- package/security/account/account-service.js +218 -0
- package/security/account/index.d.ts +3 -0
- package/security/account/index.js +19 -0
- package/security/api-key/api-key-auth.d.ts +4 -0
- package/security/api-key/api-key-auth.js +66 -0
- package/security/api-key/index.d.ts +1 -0
- package/security/api-key/index.js +17 -0
- package/security/auth-routes.d.ts +2 -0
- package/security/auth-routes.js +80 -0
- package/security/auth-schema.d.ts +215 -0
- package/security/auth-schema.js +129 -0
- package/security/auth-service.d.ts +117 -0
- package/security/auth-service.js +272 -0
- package/security/auth.d.ts +3 -0
- package/security/auth.js +127 -0
- package/security/basic/basic-auth.d.ts +4 -0
- package/security/basic/basic-auth.js +32 -0
- package/security/basic/index.d.ts +1 -0
- package/security/basic/index.js +17 -0
- package/security/create-auth-resources.d.ts +6 -0
- package/security/create-auth-resources.js +129 -0
- package/security/helper.d.ts +127 -0
- package/security/helper.js +329 -0
- package/security/index.d.ts +4 -0
- package/security/index.js +20 -0
- package/security/jwt/index.d.ts +2 -0
- package/security/jwt/index.js +18 -0
- package/security/jwt/jwt-auth.d.ts +4 -0
- package/security/jwt/jwt-auth.js +57 -0
- package/security/jwt/jwt-keys.d.ts +7 -0
- package/security/jwt/jwt-keys.js +43 -0
- package/security/oauth2/create-oauth2-plugin.d.ts +17 -0
- package/security/oauth2/create-oauth2-plugin.js +85 -0
- package/security/oauth2/index.d.ts +3 -0
- package/security/oauth2/index.js +19 -0
- package/security/oauth2/oauth2-custom.d.ts +7 -0
- package/security/oauth2/oauth2-custom.js +40 -0
- package/security/oauth2/oauth2-facebook.d.ts +1 -0
- package/security/oauth2/oauth2-facebook.js +30 -0
- package/security/oauth2/oauth2-google.d.ts +1 -0
- package/security/oauth2/oauth2-google.js +28 -0
- package/security/oauth2/oauth2-schema.d.ts +10 -0
- package/security/oauth2/oauth2-schema.js +53 -0
- package/security/recaptcha/index.d.ts +2 -0
- package/security/recaptcha/index.js +18 -0
- package/security/recaptcha/recaptcha-verify.d.ts +1 -0
- package/security/recaptcha/recaptcha-verify.js +33 -0
- package/security/recaptcha/recaptcha.d.ts +2 -0
- package/security/recaptcha/recaptcha.js +22 -0
- package/security/resources/api-key/model.d.ts +2 -0
- package/security/resources/api-key/model.js +63 -0
- package/security/resources/api-key/policy.d.ts +2 -0
- package/security/resources/api-key/policy.js +26 -0
- package/security/resources/api-key/routes.d.ts +2 -0
- package/security/resources/api-key/routes.js +15 -0
- package/security/resources/api-key/service.d.ts +3 -0
- package/security/resources/api-key/service.js +33 -0
- package/security/resources/one-time-token/model.d.ts +2 -0
- package/security/resources/one-time-token/model.js +32 -0
- package/security/resources/permission/model.d.ts +2 -0
- package/security/resources/permission/model.js +16 -0
- package/security/resources/role/model.d.ts +2 -0
- package/security/resources/role/model.js +22 -0
- package/security/store/database-security-store.d.ts +5 -0
- package/security/store/database-security-store.js +51 -0
- package/security/store/redis-security-store.d.ts +5 -0
- package/security/store/redis-security-store.js +32 -0
- package/seeder/create-seeder.d.ts +21 -0
- package/seeder/create-seeder.js +17 -0
- package/seeder/index.d.ts +2 -0
- package/seeder/index.js +18 -0
- package/seeder/resources/seeder/model.d.ts +2 -0
- package/seeder/resources/seeder/model.js +38 -0
- package/seeder/seeder.d.ts +20 -0
- package/seeder/seeder.js +175 -0
- package/server/create-server.d.ts +20 -0
- package/server/create-server.js +160 -0
- package/server/index.d.ts +4 -0
- package/server/index.js +20 -0
- package/server/info-route.d.ts +2 -0
- package/server/info-route.js +18 -0
- package/server/info-schema.d.ts +37 -0
- package/server/info-schema.js +19 -0
- package/server/register-model.d.ts +2 -0
- package/server/register-model.js +12 -0
- package/server/register-plugin.d.ts +2 -0
- package/server/register-plugin.js +13 -0
- package/server/register-route.d.ts +3 -0
- package/server/register-route.js +74 -0
- package/server/schemas.d.ts +3 -0
- package/server/schemas.js +30 -0
- package/server/swagger.d.ts +3 -0
- package/server/swagger.js +122 -0
- package/storage/file-route.d.ts +2 -0
- package/storage/file-route.js +39 -0
- package/storage/file-schema.d.ts +9 -0
- package/storage/file-schema.js +42 -0
- package/storage/file-service.d.ts +81 -0
- package/storage/file-service.js +384 -0
- package/storage/filesystem-storage.d.ts +10 -0
- package/storage/filesystem-storage.js +128 -0
- package/storage/index.d.ts +2 -0
- package/storage/index.js +18 -0
- package/storage/resources/file/model.d.ts +2 -0
- package/storage/resources/file/model.js +75 -0
- package/types/application-context.d.ts +20 -0
- package/types/application-context.js +2 -0
- package/types/auth.d.ts +31 -0
- package/types/auth.js +2 -0
- package/types/fastify-extensions.d.ts +24 -0
- package/types/fastify-extensions.js +4 -0
- package/types/generated.d.ts +193 -0
- package/types/generated.js +3 -0
- package/types/index.d.ts +4 -0
- package/types/index.js +20 -0
- package/types/server.d.ts +4 -0
- package/types/server.js +2 -0
- package/utils/export-util.d.ts +8 -0
- package/utils/export-util.js +25 -0
- package/utils/file-util.d.ts +64 -0
- package/utils/file-util.js +141 -0
- package/utils/index.d.ts +3 -0
- package/utils/index.js +19 -0
- package/utils/schema-util.d.ts +20 -0
- package/utils/schema-util.js +37 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.verify2FACodeSchema = exports.send2FACodeSchema = exports.resetPasswordSchema = exports.sendResetPasswordSchema = exports.verifyEmailRedirectSchema = exports.verifyEmailSchema = exports.sendEmailVerificationSchema = exports.AccountStatusResponse = exports.AccountVerify2FAResponse = exports.AccountVerify2FARequest = exports.AccountSend2FAResponse = exports.AccountSend2FACodeRequest = exports.AccountResetPasswordRequest = exports.AccountSendResetPasswordRequest = exports.AccountEmailVerificationRequest = exports.AccountSendEmailVerificationRequest = void 0;
|
|
4
|
+
const typebox_1 = require("@sinclair/typebox");
|
|
5
|
+
const common_1 = require("@appweaver/common");
|
|
6
|
+
const errors_1 = require("../../errors");
|
|
7
|
+
const auth_schema_1 = require("../auth-schema");
|
|
8
|
+
const utils_1 = require("../../utils");
|
|
9
|
+
const account_service_1 = require("./account-service");
|
|
10
|
+
exports.AccountSendEmailVerificationRequest = typebox_1.Type.Object({
|
|
11
|
+
redirectToUrl: typebox_1.Type.String({
|
|
12
|
+
format: 'uri',
|
|
13
|
+
description: 'A URL to redirect to with token or status code after email verification is completed',
|
|
14
|
+
example: 'https://example.com/account'
|
|
15
|
+
}),
|
|
16
|
+
verificationType: typebox_1.Type.Optional((0, common_1.StringEnum)(account_service_1.VerificationType, {
|
|
17
|
+
description: 'auto (default) - token is verified automatically before redirecting to provided URL with a status message, ' +
|
|
18
|
+
'manual - client must manually verify received token on provided URL',
|
|
19
|
+
example: 'auto'
|
|
20
|
+
}))
|
|
21
|
+
}, { $id: 'AccountSendEmailVerificationRequest' });
|
|
22
|
+
exports.AccountEmailVerificationRequest = typebox_1.Type.Object({
|
|
23
|
+
token: typebox_1.Type.String({
|
|
24
|
+
description: 'Verification token sent to the user email',
|
|
25
|
+
example: 'aBcDeFgHijkLMnO123456789'
|
|
26
|
+
})
|
|
27
|
+
}, { $id: 'AccountEmailVerificationRequest' });
|
|
28
|
+
exports.AccountSendResetPasswordRequest = typebox_1.Type.Object({
|
|
29
|
+
redirectToUrl: typebox_1.Type.String({
|
|
30
|
+
format: 'uri',
|
|
31
|
+
description: 'A URL to redirect to where the user will perform the password reset' +
|
|
32
|
+
' with one-time-token in the request',
|
|
33
|
+
example: 'https://example.com/reset-password'
|
|
34
|
+
}),
|
|
35
|
+
email: typebox_1.Type.String({
|
|
36
|
+
format: 'email',
|
|
37
|
+
description: 'A email address for which to perform password reset',
|
|
38
|
+
example: 'account@example.com'
|
|
39
|
+
})
|
|
40
|
+
}, { $id: 'AccountSendResetPasswordRequest' });
|
|
41
|
+
exports.AccountResetPasswordRequest = typebox_1.Type.Object({
|
|
42
|
+
token: typebox_1.Type.String({
|
|
43
|
+
description: 'Password reset token sent to the user email',
|
|
44
|
+
example: 'aBcDeFgHijkLMnO123456789'
|
|
45
|
+
}),
|
|
46
|
+
newPassword: typebox_1.Type.String({
|
|
47
|
+
description: 'The new password to set for this user',
|
|
48
|
+
example: 'yourNewPassword123'
|
|
49
|
+
})
|
|
50
|
+
}, { $id: 'AccountResetPasswordRequest' });
|
|
51
|
+
exports.AccountSend2FACodeRequest = typebox_1.Type.Object({
|
|
52
|
+
purpose: typebox_1.Type.Optional(typebox_1.Type.String({
|
|
53
|
+
description: 'A purpose for using two-factor authentication method',
|
|
54
|
+
example: 'authentication'
|
|
55
|
+
}))
|
|
56
|
+
}, { $id: 'AccountSend2FACodeRequest' });
|
|
57
|
+
exports.AccountSend2FAResponse = typebox_1.Type.Object({
|
|
58
|
+
challengeId: typebox_1.Type.String({
|
|
59
|
+
description: 'The ID for currently requested 2FA',
|
|
60
|
+
example: 'aBcDeFgHijkLMnO123456789'
|
|
61
|
+
}),
|
|
62
|
+
expiresIn: typebox_1.Type.Integer({
|
|
63
|
+
description: 'The expiration time in milliseconds for the 2FA challenge',
|
|
64
|
+
example: 300000
|
|
65
|
+
})
|
|
66
|
+
}, { $id: 'AccountSend2FAResponse' });
|
|
67
|
+
exports.AccountVerify2FARequest = typebox_1.Type.Object({
|
|
68
|
+
challengeId: typebox_1.Type.String({
|
|
69
|
+
description: 'The ID value received on sending 2FA code request',
|
|
70
|
+
example: 'aBcDeFgHijkLMnO123456789'
|
|
71
|
+
}),
|
|
72
|
+
code: typebox_1.Type.String({
|
|
73
|
+
description: 'The 2FA code to verify',
|
|
74
|
+
example: '123456'
|
|
75
|
+
})
|
|
76
|
+
}, { $id: 'AccountVerify2FARequest' });
|
|
77
|
+
exports.AccountVerify2FAResponse = typebox_1.Type.Object({
|
|
78
|
+
token: typebox_1.Type.String({
|
|
79
|
+
description: 'The one-time-token for requested purpose (e.g., authentication)',
|
|
80
|
+
example: 'aBcDeFgHijkLMnO123456789'
|
|
81
|
+
}),
|
|
82
|
+
expiresIn: typebox_1.Type.Integer({
|
|
83
|
+
description: 'The expiration time in milliseconds for the 2FA authentication token',
|
|
84
|
+
example: 120000
|
|
85
|
+
})
|
|
86
|
+
}, { $id: 'AccountVerify2FAResponse' });
|
|
87
|
+
exports.AccountStatusResponse = typebox_1.Type.Object({
|
|
88
|
+
message: typebox_1.Type.String({ example: 'Operation finished successfully' })
|
|
89
|
+
}, { $id: 'AccountStatusResponse' });
|
|
90
|
+
exports.sendEmailVerificationSchema = {
|
|
91
|
+
tags: ['Account'],
|
|
92
|
+
security: (0, auth_schema_1.authSchema)(),
|
|
93
|
+
summary: 'Send verification email',
|
|
94
|
+
description: 'Send verification email',
|
|
95
|
+
response: {
|
|
96
|
+
200: (0, utils_1.createSchemaModel)(exports.AccountStatusResponse),
|
|
97
|
+
...errors_1.AllErrorResponses
|
|
98
|
+
},
|
|
99
|
+
body: (0, utils_1.createSchemaModel)(exports.AccountSendEmailVerificationRequest)
|
|
100
|
+
};
|
|
101
|
+
exports.verifyEmailSchema = {
|
|
102
|
+
tags: ['Account'],
|
|
103
|
+
summary: 'Verify email token',
|
|
104
|
+
description: 'Verify email token',
|
|
105
|
+
response: {
|
|
106
|
+
200: (0, utils_1.createSchemaModel)(exports.AccountStatusResponse),
|
|
107
|
+
...errors_1.AllErrorResponses
|
|
108
|
+
},
|
|
109
|
+
body: (0, utils_1.createSchemaModel)(exports.AccountEmailVerificationRequest)
|
|
110
|
+
};
|
|
111
|
+
exports.verifyEmailRedirectSchema = {
|
|
112
|
+
tags: ['Account'],
|
|
113
|
+
summary: 'Verify email token and redirect to a provided URL with status message',
|
|
114
|
+
description: 'Verify email token and redirect to a provided URL with status message',
|
|
115
|
+
querystring: exports.AccountEmailVerificationRequest,
|
|
116
|
+
response: {
|
|
117
|
+
302: {
|
|
118
|
+
description: `Redirect to URL provided in the request for sending verification email`
|
|
119
|
+
},
|
|
120
|
+
...errors_1.AllErrorResponses
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
exports.sendResetPasswordSchema = {
|
|
124
|
+
tags: ['Account'],
|
|
125
|
+
header: (0, auth_schema_1.recaptchaHeaderSchema)({ recaptchaAction: 'send-reset-password' }),
|
|
126
|
+
summary: 'Send password reset link to provided email address',
|
|
127
|
+
description: 'Send password reset link to provided email address',
|
|
128
|
+
response: {
|
|
129
|
+
200: (0, utils_1.createSchemaModel)(exports.AccountStatusResponse),
|
|
130
|
+
...errors_1.AllErrorResponses
|
|
131
|
+
},
|
|
132
|
+
body: (0, utils_1.createSchemaModel)(exports.AccountSendResetPasswordRequest)
|
|
133
|
+
};
|
|
134
|
+
exports.resetPasswordSchema = {
|
|
135
|
+
tags: ['Account'],
|
|
136
|
+
header: (0, auth_schema_1.recaptchaHeaderSchema)({ recaptchaAction: 'reset-password' }),
|
|
137
|
+
summary: 'Reset the user password to a new value',
|
|
138
|
+
description: 'Reset the user password to a new value',
|
|
139
|
+
response: {
|
|
140
|
+
200: (0, utils_1.createSchemaModel)(exports.AccountStatusResponse),
|
|
141
|
+
...errors_1.AllErrorResponses
|
|
142
|
+
},
|
|
143
|
+
body: (0, utils_1.createSchemaModel)(exports.AccountResetPasswordRequest)
|
|
144
|
+
};
|
|
145
|
+
exports.send2FACodeSchema = {
|
|
146
|
+
tags: ['Account'],
|
|
147
|
+
security: (0, auth_schema_1.authSchema)([common_1.AuthType.Jwt]),
|
|
148
|
+
summary: 'Send 2FA code for currently logged-in user',
|
|
149
|
+
description: 'Send 2FA code for currently logged-in user',
|
|
150
|
+
response: {
|
|
151
|
+
200: (0, utils_1.createSchemaModel)(exports.AccountSend2FAResponse),
|
|
152
|
+
...errors_1.AllErrorResponses
|
|
153
|
+
},
|
|
154
|
+
body: (0, utils_1.createSchemaModel)(exports.AccountSend2FACodeRequest)
|
|
155
|
+
};
|
|
156
|
+
exports.verify2FACodeSchema = {
|
|
157
|
+
tags: ['Account'],
|
|
158
|
+
security: (0, auth_schema_1.authSchema)([common_1.AuthType.Jwt]),
|
|
159
|
+
summary: 'Verify 2FA code for currently logged-in user',
|
|
160
|
+
description: 'Verify 2FA code for currently logged-in user',
|
|
161
|
+
response: {
|
|
162
|
+
200: (0, utils_1.createSchemaModel)(exports.AccountVerify2FAResponse),
|
|
163
|
+
...errors_1.AllErrorResponses
|
|
164
|
+
},
|
|
165
|
+
body: (0, utils_1.createSchemaModel)(exports.AccountVerify2FARequest)
|
|
166
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AuthUser } from '@appweaver/common';
|
|
2
|
+
export declare enum VerificationType {
|
|
3
|
+
Auto = "auto",
|
|
4
|
+
Manual = "manual"
|
|
5
|
+
}
|
|
6
|
+
export declare class AccountService {
|
|
7
|
+
/**
|
|
8
|
+
* Sends an email verification link to the specified user's email address.
|
|
9
|
+
*
|
|
10
|
+
* @param {AuthUser} authUser - The user object for whom the email verification is to be sent.
|
|
11
|
+
* @param {string} redirectToUrl - The URL to which the user will be redirected after verifying their email address.
|
|
12
|
+
* @param {VerificationType} [verificationType=VerificationType.Auto] - The type of verification process to use.
|
|
13
|
+
* Defaults to auto verification.
|
|
14
|
+
* @return {Promise<string>} A promise that resolves to a confirmation message upon successful email delivery.
|
|
15
|
+
* @throws {HttpError} Throws a 501 error if email verification is not supported.
|
|
16
|
+
* @throws {HttpError} Throws a 403 error if the user's email address is already verified.
|
|
17
|
+
* @throws {HttpError} Throws a 400 error if the provided redirect URL is invalid.
|
|
18
|
+
*/
|
|
19
|
+
sendEmailVerification(authUser: AuthUser, redirectToUrl: string, verificationType?: VerificationType): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Verifies the email address associated with a given token and returns a success message.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} token The one-time token used for email verification.
|
|
24
|
+
* @return {Promise<string>} A confirmation message indicating the email address has been verified.
|
|
25
|
+
* @throws {HttpError} If the authentication user does not exist, is disabled, or the email has already been verified.
|
|
26
|
+
*/
|
|
27
|
+
verifyEmailAddress(token: string): Promise<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Verify the email address associated with a one-time token and generates a redirection URL.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} token - The one-time token used to verify the email address.
|
|
32
|
+
* @return {Promise<{ status: 'ok' | 'error'; message: string; redirectUrl: string }>} An object containing the
|
|
33
|
+
* verification status, a message, and the constructed redirection URL.
|
|
34
|
+
*/
|
|
35
|
+
verifyEmailAddressRedirect(token: string): Promise<{
|
|
36
|
+
status: 'ok' | 'error';
|
|
37
|
+
message: string;
|
|
38
|
+
redirectUrl: string;
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* Sends a password reset email to the specified email address with a link for resetting the password.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} email - The email address of the user requesting a password reset.
|
|
44
|
+
* @param {string} redirectToUrl - The URL to be appended with a token for the password reset link.
|
|
45
|
+
* @return {Promise<string>} A promise that resolves to a confirmation message upon successful email dispatch.
|
|
46
|
+
* @throws {HttpError} If the email service is not configured, the user does not exist or is disabled, the user has no
|
|
47
|
+
* password set, or the redirect URL is invalid.
|
|
48
|
+
*/
|
|
49
|
+
sendResetPassword(email: string, redirectToUrl: string): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* Resets the password for a user using a one-time token.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} token - The one-time token used for password reset.
|
|
54
|
+
* @param {string} password - The new password to be set for the user.
|
|
55
|
+
* @return {Promise<string>} A message indicating the success of the password reset operation.
|
|
56
|
+
* @throws {HttpError} If the password does not meet the required complexity, if the token is invalid,
|
|
57
|
+
* if the user does not exist, if the user is disabled, or if the user does not have
|
|
58
|
+
* an existing password to reset.
|
|
59
|
+
*/
|
|
60
|
+
resetPassword(token: string, password: string): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Sends a two-factor authentication (2FA) code to the specified user via email for a given purpose.
|
|
63
|
+
*
|
|
64
|
+
* @param {AuthUser} authUser - The authenticated user to whom the 2FA code will be sent.
|
|
65
|
+
* @param {string} [purpose='authentication'] - The purpose for sending the 2FA code (e.g., authentication, account
|
|
66
|
+
* recovery).
|
|
67
|
+
* @return {Promise<{ challengeId: string; expiresIn: number }>} A promise that resolves to the unique challenge ID
|
|
68
|
+
* and expiration time associated with the 2FA code.
|
|
69
|
+
* @throws {HttpError} If the email service is not available, an error with a 501 status code is thrown.
|
|
70
|
+
*/
|
|
71
|
+
send2FACode(authUser: AuthUser, purpose?: string): Promise<{
|
|
72
|
+
challengeId: string;
|
|
73
|
+
expiresIn: number;
|
|
74
|
+
}>;
|
|
75
|
+
/**
|
|
76
|
+
* Verifies a two-factor authentication (2FA) code for a provided challenge ID.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} challengeId The unique identifier of the 2FA challenge.
|
|
79
|
+
* @param {string} code The 2FA code entered by the user.
|
|
80
|
+
* @return {Promise<{ token: string; expiresIn: number }>} A promise that resolves to a one-time token and expiration
|
|
81
|
+
* time if the 2FA verification is successful.
|
|
82
|
+
* @throws {Error} An error if verification fails or the associated user is disabled or does not exist.
|
|
83
|
+
*/
|
|
84
|
+
verify2FACode(challengeId: string, code: string): Promise<{
|
|
85
|
+
token: string;
|
|
86
|
+
expiresIn: number;
|
|
87
|
+
}>;
|
|
88
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccountService = exports.VerificationType = void 0;
|
|
4
|
+
const common_1 = require("@appweaver/common");
|
|
5
|
+
const auth_service_1 = require("../auth-service");
|
|
6
|
+
const helper_1 = require("../helper");
|
|
7
|
+
const context_1 = require("../../context");
|
|
8
|
+
const mailer_1 = require("../../mailer");
|
|
9
|
+
const errors_1 = require("../../errors");
|
|
10
|
+
var VerificationType;
|
|
11
|
+
(function (VerificationType) {
|
|
12
|
+
VerificationType["Auto"] = "auto";
|
|
13
|
+
VerificationType["Manual"] = "manual";
|
|
14
|
+
})(VerificationType || (exports.VerificationType = VerificationType = {}));
|
|
15
|
+
class AccountService {
|
|
16
|
+
/** @internal */
|
|
17
|
+
_authService = (0, context_1.inject)(auth_service_1.AuthService);
|
|
18
|
+
/** @internal */
|
|
19
|
+
_securityStore = (0, context_1.inject)(common_1.SecurityStore);
|
|
20
|
+
/** @internal */
|
|
21
|
+
_emailService = (0, context_1.inject)(mailer_1.EmailService, false);
|
|
22
|
+
/**
|
|
23
|
+
* Sends an email verification link to the specified user's email address.
|
|
24
|
+
*
|
|
25
|
+
* @param {AuthUser} authUser - The user object for whom the email verification is to be sent.
|
|
26
|
+
* @param {string} redirectToUrl - The URL to which the user will be redirected after verifying their email address.
|
|
27
|
+
* @param {VerificationType} [verificationType=VerificationType.Auto] - The type of verification process to use.
|
|
28
|
+
* Defaults to auto verification.
|
|
29
|
+
* @return {Promise<string>} A promise that resolves to a confirmation message upon successful email delivery.
|
|
30
|
+
* @throws {HttpError} Throws a 501 error if email verification is not supported.
|
|
31
|
+
* @throws {HttpError} Throws a 403 error if the user's email address is already verified.
|
|
32
|
+
* @throws {HttpError} Throws a 400 error if the provided redirect URL is invalid.
|
|
33
|
+
*/
|
|
34
|
+
async sendEmailVerification(authUser, redirectToUrl, verificationType = VerificationType.Auto) {
|
|
35
|
+
if (!this._emailService) {
|
|
36
|
+
throw new errors_1.HttpError('Email verification is not supported', 501);
|
|
37
|
+
}
|
|
38
|
+
if (authUser.verifiedEmail) {
|
|
39
|
+
throw new errors_1.HttpError('Email address is already verified', 403);
|
|
40
|
+
}
|
|
41
|
+
const result = (0, helper_1.validateRedirectUrl)(redirectToUrl);
|
|
42
|
+
if (!result.valid) {
|
|
43
|
+
throw new errors_1.HttpError(result.message, 400);
|
|
44
|
+
}
|
|
45
|
+
const token = await this._securityStore.generateOneTimeToken(common_1.AuthOTTPurpose.EmailVerification, { authUserId: authUser.id, redirectToUrl }, common_1.config.SECURITY_ACCOUNT_VERIFY_EMAIL_OTT_TTL);
|
|
46
|
+
const accountPath = common_1.config.SECURITY_ACCOUNT_ROUTE_PREFIX.replace(/\/$/, '');
|
|
47
|
+
const separator = redirectToUrl.includes('?') ? '&' : '?';
|
|
48
|
+
const verifyLink = verificationType === VerificationType.Auto
|
|
49
|
+
? `${common_1.config.APP_HOSTNAME}${accountPath}/verify-email-redirect?token=${token}}`
|
|
50
|
+
: `${redirectToUrl}${separator}token=${token}`;
|
|
51
|
+
await this._emailService.sendEmail({
|
|
52
|
+
to: authUser.email,
|
|
53
|
+
subject: 'Verify email address',
|
|
54
|
+
text: `Please verify your email address on following link: ${verifyLink}`
|
|
55
|
+
});
|
|
56
|
+
return 'Verification email sent';
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Verifies the email address associated with a given token and returns a success message.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} token The one-time token used for email verification.
|
|
62
|
+
* @return {Promise<string>} A confirmation message indicating the email address has been verified.
|
|
63
|
+
* @throws {HttpError} If the authentication user does not exist, is disabled, or the email has already been verified.
|
|
64
|
+
*/
|
|
65
|
+
async verifyEmailAddress(token) {
|
|
66
|
+
const { authUserId } = await this._securityStore.useOneTimeToken(token, common_1.AuthOTTPurpose.EmailVerification);
|
|
67
|
+
const authUser = await this._authService.findById(authUserId);
|
|
68
|
+
if (!authUser || !authUser.enabled) {
|
|
69
|
+
throw new errors_1.HttpError('Auth user does not exist or is disabled', 400);
|
|
70
|
+
}
|
|
71
|
+
if (authUser.verifiedEmail) {
|
|
72
|
+
throw new errors_1.HttpError('Email address is already verified', 403);
|
|
73
|
+
}
|
|
74
|
+
await this._authService.updateAuthUser(authUserId, { verifiedEmail: true });
|
|
75
|
+
return 'E-mail address verified';
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Verify the email address associated with a one-time token and generates a redirection URL.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} token - The one-time token used to verify the email address.
|
|
81
|
+
* @return {Promise<{ status: 'ok' | 'error'; message: string; redirectUrl: string }>} An object containing the
|
|
82
|
+
* verification status, a message, and the constructed redirection URL.
|
|
83
|
+
*/
|
|
84
|
+
async verifyEmailAddressRedirect(token) {
|
|
85
|
+
const { authUserId, redirectToUrl } = await this._securityStore.useOneTimeToken(token, common_1.AuthOTTPurpose.EmailVerification);
|
|
86
|
+
const separator = redirectToUrl.includes('?') ? '&' : '?';
|
|
87
|
+
const redirectUrl = `${redirectToUrl}${separator}`;
|
|
88
|
+
const authUser = await this._authService.findById(authUserId);
|
|
89
|
+
if (!authUser || !authUser.enabled) {
|
|
90
|
+
return {
|
|
91
|
+
redirectUrl,
|
|
92
|
+
status: 'error',
|
|
93
|
+
message: 'Auth user does not exist or is disabled'
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (authUser.verifiedEmail) {
|
|
97
|
+
return {
|
|
98
|
+
redirectUrl,
|
|
99
|
+
status: 'error',
|
|
100
|
+
message: 'Email address is already verified'
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
await this._authService.updateAuthUser(authUserId, { verifiedEmail: true });
|
|
104
|
+
return { redirectUrl, status: 'ok', message: 'E-mail address verified' };
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sends a password reset email to the specified email address with a link for resetting the password.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} email - The email address of the user requesting a password reset.
|
|
110
|
+
* @param {string} redirectToUrl - The URL to be appended with a token for the password reset link.
|
|
111
|
+
* @return {Promise<string>} A promise that resolves to a confirmation message upon successful email dispatch.
|
|
112
|
+
* @throws {HttpError} If the email service is not configured, the user does not exist or is disabled, the user has no
|
|
113
|
+
* password set, or the redirect URL is invalid.
|
|
114
|
+
*/
|
|
115
|
+
async sendResetPassword(email, redirectToUrl) {
|
|
116
|
+
if (!this._emailService) {
|
|
117
|
+
throw new errors_1.HttpError('Password reset is not supported', 501);
|
|
118
|
+
}
|
|
119
|
+
const authUser = await this._authService.findByUsername(email);
|
|
120
|
+
if (!authUser || !authUser.enabled) {
|
|
121
|
+
throw new errors_1.HttpError('Auth user does not exist or is disabled', 400);
|
|
122
|
+
}
|
|
123
|
+
if (!authUser.passwordHash) {
|
|
124
|
+
throw new errors_1.HttpError('Auth user does not have already set password', 403);
|
|
125
|
+
}
|
|
126
|
+
const result = (0, helper_1.validateRedirectUrl)(redirectToUrl);
|
|
127
|
+
if (!result.valid) {
|
|
128
|
+
throw new errors_1.HttpError(result.message, 400);
|
|
129
|
+
}
|
|
130
|
+
const token = await this._securityStore.generateOneTimeToken(common_1.AuthOTTPurpose.PasswordReset, { authUserId: authUser.id, authSource: common_1.AuthSource.Password }, common_1.config.SECURITY_ACCOUNT_RESET_PASSWORD_OTT_TTL);
|
|
131
|
+
const separator = redirectToUrl.includes('?') ? '&' : '?';
|
|
132
|
+
const changePasswordLink = `${redirectToUrl}${separator}token=${token}`;
|
|
133
|
+
await this._emailService.sendEmail({
|
|
134
|
+
to: authUser.email,
|
|
135
|
+
subject: 'Password reset request',
|
|
136
|
+
text: `Please change your password on following link: ${changePasswordLink}`
|
|
137
|
+
});
|
|
138
|
+
return 'Password reset email sent';
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Resets the password for a user using a one-time token.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} token - The one-time token used for password reset.
|
|
144
|
+
* @param {string} password - The new password to be set for the user.
|
|
145
|
+
* @return {Promise<string>} A message indicating the success of the password reset operation.
|
|
146
|
+
* @throws {HttpError} If the password does not meet the required complexity, if the token is invalid,
|
|
147
|
+
* if the user does not exist, if the user is disabled, or if the user does not have
|
|
148
|
+
* an existing password to reset.
|
|
149
|
+
*/
|
|
150
|
+
async resetPassword(token, password) {
|
|
151
|
+
const result = (0, helper_1.validatePasswordComplexity)(password);
|
|
152
|
+
if (!result.valid) {
|
|
153
|
+
throw new errors_1.HttpError(result.message, 400);
|
|
154
|
+
}
|
|
155
|
+
const { authUserId } = await this._securityStore.useOneTimeToken(token, common_1.AuthOTTPurpose.PasswordReset);
|
|
156
|
+
const authUser = await this._authService.findById(authUserId);
|
|
157
|
+
if (!authUser || !authUser.enabled) {
|
|
158
|
+
throw new errors_1.HttpError('Auth user does not exist or is disabled', 400);
|
|
159
|
+
}
|
|
160
|
+
if (!authUser.passwordHash) {
|
|
161
|
+
throw new errors_1.HttpError('Auth user does not have already set password', 403);
|
|
162
|
+
}
|
|
163
|
+
await this._authService.updateAuthUser(authUserId, {
|
|
164
|
+
logoutAt: new Date(),
|
|
165
|
+
passwordHash: await (0, helper_1.hashPassword)(password)
|
|
166
|
+
});
|
|
167
|
+
return 'Password reset successfully';
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Sends a two-factor authentication (2FA) code to the specified user via email for a given purpose.
|
|
171
|
+
*
|
|
172
|
+
* @param {AuthUser} authUser - The authenticated user to whom the 2FA code will be sent.
|
|
173
|
+
* @param {string} [purpose='authentication'] - The purpose for sending the 2FA code (e.g., authentication, account
|
|
174
|
+
* recovery).
|
|
175
|
+
* @return {Promise<{ challengeId: string; expiresIn: number }>} A promise that resolves to the unique challenge ID
|
|
176
|
+
* and expiration time associated with the 2FA code.
|
|
177
|
+
* @throws {HttpError} If the email service is not available, an error with a 501 status code is thrown.
|
|
178
|
+
*/
|
|
179
|
+
async send2FACode(authUser, purpose = 'authentication') {
|
|
180
|
+
if (!this._emailService) {
|
|
181
|
+
throw new errors_1.HttpError('2FA is not supported', 501);
|
|
182
|
+
}
|
|
183
|
+
const expiresIn = common_1.config.SECURITY_ACCOUNT_2FA_OTT_TTL;
|
|
184
|
+
const code = (0, common_1.randomString)(6, { numbers: true });
|
|
185
|
+
const challengeId = await this._securityStore.generateOneTimeToken(common_1.AuthOTTPurpose.TwoFAVerification, { authUserId: authUser.id, codeHash: (0, common_1.makeHash)(code), purpose }, expiresIn);
|
|
186
|
+
await this._emailService.sendEmail({
|
|
187
|
+
to: authUser.email,
|
|
188
|
+
subject: 'Verification code',
|
|
189
|
+
text: `Please enter the following code: ${code}`
|
|
190
|
+
});
|
|
191
|
+
return { challengeId, expiresIn };
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Verifies a two-factor authentication (2FA) code for a provided challenge ID.
|
|
195
|
+
*
|
|
196
|
+
* @param {string} challengeId The unique identifier of the 2FA challenge.
|
|
197
|
+
* @param {string} code The 2FA code entered by the user.
|
|
198
|
+
* @return {Promise<{ token: string; expiresIn: number }>} A promise that resolves to a one-time token and expiration
|
|
199
|
+
* time if the 2FA verification is successful.
|
|
200
|
+
* @throws {Error} An error if verification fails or the associated user is disabled or does not exist.
|
|
201
|
+
*/
|
|
202
|
+
async verify2FACode(challengeId, code) {
|
|
203
|
+
const data = await this._securityStore.useOneTimeToken(challengeId, common_1.AuthOTTPurpose.TwoFAVerification, (value) => {
|
|
204
|
+
if (value.codeHash !== (0, common_1.makeHash)(code)) {
|
|
205
|
+
return { valid: false, message: 'Invalid 2FA code provided' };
|
|
206
|
+
}
|
|
207
|
+
return { valid: true, message: 'OK' };
|
|
208
|
+
});
|
|
209
|
+
const authUser = await this._authService.findById(data.authUserId);
|
|
210
|
+
if (!authUser || !authUser.enabled) {
|
|
211
|
+
throw new errors_1.HttpError('Auth user does not exist or is disabled', 400);
|
|
212
|
+
}
|
|
213
|
+
const expiresIn = common_1.config.SECURITY_AUTH_OTT_TTL;
|
|
214
|
+
const token = await this._securityStore.generateOneTimeToken(data.purpose, { authUserId: authUser.id, authSource: common_1.AuthSource.Password }, expiresIn);
|
|
215
|
+
return { token, expiresIn };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
exports.AccountService = AccountService;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./account-routes"), exports);
|
|
18
|
+
__exportStar(require("./account-schema"), exports);
|
|
19
|
+
__exportStar(require("./account-service"), exports);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.apiKeyAuth = void 0;
|
|
7
|
+
exports.hasApiKey = hasApiKey;
|
|
8
|
+
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
9
|
+
const request_context_1 = require("@fastify/request-context");
|
|
10
|
+
const common_1 = require("@appweaver/common");
|
|
11
|
+
const context_1 = require("../../context");
|
|
12
|
+
const helper_1 = require("../helper");
|
|
13
|
+
const auth_service_1 = require("../auth-service");
|
|
14
|
+
const errors_1 = require("../../errors");
|
|
15
|
+
const cache_1 = require("../../cache");
|
|
16
|
+
exports.apiKeyAuth = (0, fastify_plugin_1.default)(async (server) => {
|
|
17
|
+
const authService = (0, context_1.inject)(auth_service_1.AuthService);
|
|
18
|
+
const cacheService = (0, context_1.inject)(cache_1.CacheService);
|
|
19
|
+
const db = (0, context_1.inject)(common_1.Database);
|
|
20
|
+
server.decorate('authenticateApiKey', async (request) => {
|
|
21
|
+
const key = request.headers[common_1.config.SECURITY_API_KEY_HEADER_NAME.toLowerCase()];
|
|
22
|
+
if (!key) {
|
|
23
|
+
throw new errors_1.HttpError(`Missing API key header: ${common_1.config.SECURITY_API_KEY_HEADER_NAME}`, 401);
|
|
24
|
+
}
|
|
25
|
+
const sanitizedApiKey = String(key).trim();
|
|
26
|
+
// Use configured delimiter to split an API key and separate ID from the
|
|
27
|
+
// rest of the key value
|
|
28
|
+
const apiKeyParts = sanitizedApiKey.split(common_1.config.SECURITY_API_KEY_DELIMITER);
|
|
29
|
+
const apiKeyId = parseInt(apiKeyParts.shift() ?? '', 10);
|
|
30
|
+
const apiKeyValue = apiKeyParts.join(common_1.config.SECURITY_API_KEY_DELIMITER);
|
|
31
|
+
const cacheKey = cacheService.buildCacheKey({
|
|
32
|
+
baseKey: `apikey:${apiKeyId}`,
|
|
33
|
+
modelName: 'ApiKey'
|
|
34
|
+
});
|
|
35
|
+
let apiKey = await cacheService.getCachedValue(cacheKey);
|
|
36
|
+
if (!apiKey) {
|
|
37
|
+
try {
|
|
38
|
+
apiKey = await db
|
|
39
|
+
.client()
|
|
40
|
+
.apiKey.findFirst({ where: { id: apiKeyId } });
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
throw new errors_1.HttpError(`Invalid API key format`, 401);
|
|
44
|
+
}
|
|
45
|
+
if (!apiKey ||
|
|
46
|
+
!apiKey.enabled ||
|
|
47
|
+
apiKey.keyHash !== (0, common_1.makeHash)(apiKeyValue)) {
|
|
48
|
+
throw new errors_1.HttpError(`Invalid API key`, 401);
|
|
49
|
+
}
|
|
50
|
+
await cacheService.addToCache(cacheKey, apiKey, common_1.config.SECURITY_CACHE_TTL);
|
|
51
|
+
}
|
|
52
|
+
if (apiKey.expiresAt && new Date(apiKey.expiresAt).getTime() < Date.now()) {
|
|
53
|
+
throw new errors_1.HttpError(`API key has expired`, 403);
|
|
54
|
+
}
|
|
55
|
+
const authModelField = (0, common_1.uncapitalize)((0, helper_1.resourceAuthModel)().name);
|
|
56
|
+
const authUser = await authService.findById(apiKey[`${authModelField}Id`]);
|
|
57
|
+
authService.authorize(authUser, request.url, request.routeOptions.config);
|
|
58
|
+
request_context_1.requestContext.set('apiKey', apiKey);
|
|
59
|
+
request_context_1.requestContext.set('authUser', authUser);
|
|
60
|
+
request_context_1.requestContext.set('authType', common_1.AuthType.ApiKey);
|
|
61
|
+
request_context_1.requestContext.set('authSource', common_1.AuthSource.ApiKey);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
function hasApiKey(request) {
|
|
65
|
+
return !!request.headers[common_1.config.SECURITY_API_KEY_HEADER_NAME.toLowerCase()];
|
|
66
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './api-key-auth';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api-key-auth"), exports);
|