najm-auth 1.1.41 → 1.1.42
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/dist/index.d.ts +52 -1
- package/dist/index.js +427 -349
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -208,7 +208,16 @@ var auth = {
|
|
|
208
208
|
passwordChanged: "Password changed successfully",
|
|
209
209
|
passwordResetSent: "If that email exists, a reset link has been sent",
|
|
210
210
|
passwordReset: "Password has been reset successfully",
|
|
211
|
+
accountInviteSent: "Invitation sent successfully",
|
|
211
212
|
tokenRefreshed: "Token refreshed successfully"
|
|
213
|
+
},
|
|
214
|
+
emails: {
|
|
215
|
+
passwordReset: {
|
|
216
|
+
subject: "Reset your password"
|
|
217
|
+
},
|
|
218
|
+
accountInvite: {
|
|
219
|
+
subject: "You've been invited — set up your account"
|
|
220
|
+
}
|
|
212
221
|
}
|
|
213
222
|
};
|
|
214
223
|
var users = {
|
|
@@ -301,8 +310,17 @@ declare const AUTH_LOCALES: {
|
|
|
301
310
|
passwordChanged: string;
|
|
302
311
|
passwordResetSent: string;
|
|
303
312
|
passwordReset: string;
|
|
313
|
+
accountInviteSent: string;
|
|
304
314
|
tokenRefreshed: string;
|
|
305
315
|
};
|
|
316
|
+
emails: {
|
|
317
|
+
passwordReset: {
|
|
318
|
+
subject: string;
|
|
319
|
+
};
|
|
320
|
+
accountInvite: {
|
|
321
|
+
subject: string;
|
|
322
|
+
};
|
|
323
|
+
};
|
|
306
324
|
};
|
|
307
325
|
users: {
|
|
308
326
|
errors: {
|
|
@@ -920,6 +938,12 @@ declare class TokenService {
|
|
|
920
938
|
* without throwing, because logout can still fall back to revoke-all.
|
|
921
939
|
*/
|
|
922
940
|
private resolveRefreshCookieFamily;
|
|
941
|
+
/**
|
|
942
|
+
* Generate a one-time set-password token (used by both password reset and
|
|
943
|
+
* account invites). Stores the jti in cache so the token can only be used
|
|
944
|
+
* once, with a TTL matching the JWT expiry.
|
|
945
|
+
*/
|
|
946
|
+
private generateSetPasswordToken;
|
|
923
947
|
/**
|
|
924
948
|
* Generate secure password reset token
|
|
925
949
|
* Returns both the plain token (to send via email) and userId for identification
|
|
@@ -928,6 +952,15 @@ declare class TokenService {
|
|
|
928
952
|
token: string;
|
|
929
953
|
userId: string;
|
|
930
954
|
}>;
|
|
955
|
+
/**
|
|
956
|
+
* Generate secure account-invite token.
|
|
957
|
+
* Longer expiry (3d) than reset because an invited user may not check
|
|
958
|
+
* their email immediately. Consumed via the same reset-password endpoint.
|
|
959
|
+
*/
|
|
960
|
+
generateInviteToken(userId: string): Promise<{
|
|
961
|
+
token: string;
|
|
962
|
+
userId: string;
|
|
963
|
+
}>;
|
|
931
964
|
/**
|
|
932
965
|
* Verify password reset token
|
|
933
966
|
* Returns userId if valid, throws error if expired/invalid
|
|
@@ -962,6 +995,12 @@ declare const updateUserDto: z.ZodObject<{
|
|
|
962
995
|
inactive: "inactive";
|
|
963
996
|
}>>>;
|
|
964
997
|
}, z.core.$strip>;
|
|
998
|
+
declare const inviteUserDto: z.ZodObject<{
|
|
999
|
+
name: z.ZodOptional<z.ZodString>;
|
|
1000
|
+
email: z.ZodString;
|
|
1001
|
+
roleId: z.ZodOptional<z.ZodString>;
|
|
1002
|
+
image: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
1003
|
+
}, z.core.$strip>;
|
|
965
1004
|
declare const userIdParam: z.ZodObject<{
|
|
966
1005
|
id: z.ZodString;
|
|
967
1006
|
}, z.core.$strip>;
|
|
@@ -999,6 +1038,7 @@ declare const userListQuery: z.ZodObject<{
|
|
|
999
1038
|
}, z.core.$strip>;
|
|
1000
1039
|
type CreateUserDto = z.infer<typeof createUserDto>;
|
|
1001
1040
|
type UpdateUserDto = z.infer<typeof updateUserDto>;
|
|
1041
|
+
type InviteUserDto = z.infer<typeof inviteUserDto>;
|
|
1002
1042
|
type UserIdParam = z.infer<typeof userIdParam>;
|
|
1003
1043
|
type LoginDto = z.infer<typeof loginDto>;
|
|
1004
1044
|
type ChangePasswordDto = z.infer<typeof changePasswordDto>;
|
|
@@ -1028,6 +1068,16 @@ declare class AuthService {
|
|
|
1028
1068
|
private getDummyHash;
|
|
1029
1069
|
warmupPasswordHash(): Promise<void>;
|
|
1030
1070
|
registerUser(body: CreateUserDto): Promise<SanitizedUser>;
|
|
1071
|
+
/**
|
|
1072
|
+
* Admin-initiated account creation. The user is created with a random,
|
|
1073
|
+
* unusable password (the schema requires one) and then emailed a one-time
|
|
1074
|
+
* link to set their own. They can't log in until they do, since they never
|
|
1075
|
+
* learn the random password.
|
|
1076
|
+
*
|
|
1077
|
+
* Email is best-effort: a send failure logs a warning but never rolls back
|
|
1078
|
+
* account creation (and with the console provider, nothing is actually sent).
|
|
1079
|
+
*/
|
|
1080
|
+
inviteUser(body: InviteUserDto): Promise<SanitizedUser>;
|
|
1031
1081
|
loginUser(body: LoginDto): Promise<TokenPair & {
|
|
1032
1082
|
user: SanitizedUser;
|
|
1033
1083
|
}>;
|
|
@@ -1077,6 +1127,7 @@ declare class AuthController {
|
|
|
1077
1127
|
loginUser(body: LoginDto): Promise<TokenPair & {
|
|
1078
1128
|
user: SanitizedUser;
|
|
1079
1129
|
}>;
|
|
1130
|
+
inviteUser(body: InviteUserDto): Promise<SanitizedUser>;
|
|
1080
1131
|
refreshTokens(): Promise<TokenPair>;
|
|
1081
1132
|
logoutUser(userId: string, authorization?: string): Promise<{
|
|
1082
1133
|
data: any;
|
|
@@ -2049,4 +2100,4 @@ declare const authSeed: (config: AuthSeedConfig) => Record<string, SeedEntry>;
|
|
|
2049
2100
|
*/
|
|
2050
2101
|
declare function seedAuthData(config: SeedAuthDataConfig): Promise<SeedAuthDataResult>;
|
|
2051
2102
|
|
|
2052
|
-
export { AUTH_CONFIG, en as AUTH_EN, AUTH_LOCALES, AUTH_MODULE, AUTH_PERMISSIONS, AUTH_ROLE, AUTH_SCHEMA, AUTH_SUPPORTED_LANGUAGES, AUTH_USER, type AssignPermissionDto, type AssignRoleDto, type AssignRoleParams, type AuthConfig, AuthController, AuthGuard, type AuthPluginConfig, AuthQueries, AuthResolver, type AuthSchema, type AuthSeedConfig, AuthService, type AuthUser, Can, CanCreate, CanDelete, CanList, CanRead, CanUpdate, type ChainableGuard, type ChangePasswordDto, type CheckPermissionDto, type ConfiguredOwnership, type ConfirmResetPasswordDto, CookieManager, type CreatePermissionDto, type CreateRoleDto, type CreateTokenDto, type CreateUserDto, type DefineRolesOptions, type EmailParam, EncryptionService, type JwtConfig, type JwtPayload, type LanguageParam, type LoginDto, NewPermission, NewRoleEntity, NewUser, Owned, type OwnedMethods, type OwnershipConfig, type OwnershipProvider, type OwnershipRule, OwnershipToken, type OwnershipTokenOptions, Permission, PermissionController, PermissionGuard, type PermissionIdParam, PermissionRepository, PermissionService, PermissionValidator, Policy, ROLES, ROLE_GROUPS, type RefreshTokenDto, type ResetPasswordDto, type ResourceAccessor, type ResourceGuards, type ResourceGuardsOptions, type RevokeTokenDto, Role, RoleController, RoleEntity, RoleGuard, type RoleIdParam, type RoleInput, RolePermission, RoleRepository, RoleService, type RoleType, RoleValidator, type RunAsUser, type SanitizedUser, ScopeContext, type ScopeResult, type SeedAuthDataConfig, type SeedAuthDataResult, type SeedUserConfig, type SessionCookieData, TOKEN_STATUS, TOKEN_TYPE, type TokenIdParam, type TokenPair, TokenRepository, TokenService, USER_STATUS, type UpdatePermissionDto, type UpdateRoleDto, type UpdateTokenDto, type UpdateUserDto, User, UserController, type UserIdInParam, type UserIdParam, type UserListQuery, UserRepository, UserService, UserValidator, type UserWithPermissions, type VerifyTokenDto, assignPermissionDto, assignRoleDto, assignRoleParams, auth$1 as auth, authSeed, avatarsPath, calculateAge, calculateYearsOfExperience, changePasswordDto, checkPermissionDto, clean, configureOwnership, confirmResetPasswordDto, createPermissionDto, createRoleDto, createTokenDto, createUserDto, defineRoles, emailParam, formatDate, getAuthLocale, getAvatarFile, isAdmin, isAdministrator, isAuth, isEmpty, isFile, isPath, join, languageParam, loginDto, own, parseSchema, permissionIdParam, pickProps, refreshTokenDto, resetPasswordDto, revokeTokenDto, roleIdParam, runAsUser, seedAuthData, tokenIdParam, updatePermissionDto, updateRoleDto, updateTokenDto, updateUserDto, userIdInParam, userIdParam, userListQuery, verifyTokenDto, where };
|
|
2103
|
+
export { AUTH_CONFIG, en as AUTH_EN, AUTH_LOCALES, AUTH_MODULE, AUTH_PERMISSIONS, AUTH_ROLE, AUTH_SCHEMA, AUTH_SUPPORTED_LANGUAGES, AUTH_USER, type AssignPermissionDto, type AssignRoleDto, type AssignRoleParams, type AuthConfig, AuthController, AuthGuard, type AuthPluginConfig, AuthQueries, AuthResolver, type AuthSchema, type AuthSeedConfig, AuthService, type AuthUser, Can, CanCreate, CanDelete, CanList, CanRead, CanUpdate, type ChainableGuard, type ChangePasswordDto, type CheckPermissionDto, type ConfiguredOwnership, type ConfirmResetPasswordDto, CookieManager, type CreatePermissionDto, type CreateRoleDto, type CreateTokenDto, type CreateUserDto, type DefineRolesOptions, type EmailParam, EncryptionService, type InviteUserDto, type JwtConfig, type JwtPayload, type LanguageParam, type LoginDto, NewPermission, NewRoleEntity, NewUser, Owned, type OwnedMethods, type OwnershipConfig, type OwnershipProvider, type OwnershipRule, OwnershipToken, type OwnershipTokenOptions, Permission, PermissionController, PermissionGuard, type PermissionIdParam, PermissionRepository, PermissionService, PermissionValidator, Policy, ROLES, ROLE_GROUPS, type RefreshTokenDto, type ResetPasswordDto, type ResourceAccessor, type ResourceGuards, type ResourceGuardsOptions, type RevokeTokenDto, Role, RoleController, RoleEntity, RoleGuard, type RoleIdParam, type RoleInput, RolePermission, RoleRepository, RoleService, type RoleType, RoleValidator, type RunAsUser, type SanitizedUser, ScopeContext, type ScopeResult, type SeedAuthDataConfig, type SeedAuthDataResult, type SeedUserConfig, type SessionCookieData, TOKEN_STATUS, TOKEN_TYPE, type TokenIdParam, type TokenPair, TokenRepository, TokenService, USER_STATUS, type UpdatePermissionDto, type UpdateRoleDto, type UpdateTokenDto, type UpdateUserDto, User, UserController, type UserIdInParam, type UserIdParam, type UserListQuery, UserRepository, UserService, UserValidator, type UserWithPermissions, type VerifyTokenDto, assignPermissionDto, assignRoleDto, assignRoleParams, auth$1 as auth, authSeed, avatarsPath, calculateAge, calculateYearsOfExperience, changePasswordDto, checkPermissionDto, clean, configureOwnership, confirmResetPasswordDto, createPermissionDto, createRoleDto, createTokenDto, createUserDto, defineRoles, emailParam, formatDate, getAuthLocale, getAvatarFile, inviteUserDto, isAdmin, isAdministrator, isAuth, isEmpty, isFile, isPath, join, languageParam, loginDto, own, parseSchema, permissionIdParam, pickProps, refreshTokenDto, resetPasswordDto, revokeTokenDto, roleIdParam, runAsUser, seedAuthData, tokenIdParam, updatePermissionDto, updateRoleDto, updateTokenDto, updateUserDto, userIdInParam, userIdParam, userListQuery, verifyTokenDto, where };
|
package/dist/index.js
CHANGED
|
@@ -373,15 +373,16 @@ CookieManager = __decorate2([
|
|
|
373
373
|
], CookieManager);
|
|
374
374
|
|
|
375
375
|
// src/auth/AuthController.ts
|
|
376
|
-
import { Controller } from "najm-core";
|
|
377
|
-
import { Get, Post, ResMsg } from "najm-core";
|
|
378
|
-
import { Body, User as
|
|
376
|
+
import { Controller as Controller2 } from "najm-core";
|
|
377
|
+
import { Get as Get2, Post as Post2, ResMsg as ResMsg2 } from "najm-core";
|
|
378
|
+
import { Body as Body2, User as User3, Headers } from "najm-core";
|
|
379
379
|
|
|
380
380
|
// src/auth/AuthService.ts
|
|
381
381
|
import { Injectable as Injectable7, Inject as Inject8 } from "najm-core";
|
|
382
382
|
import { Err as Err6, Log } from "najm-core";
|
|
383
383
|
import { I18n as I18n5, I18nService as I18nService2 } from "najm-i18n";
|
|
384
|
-
import { EmailService, passwordResetTemplate } from "najm-email";
|
|
384
|
+
import { EmailService, passwordResetTemplate, accountInviteTemplate } from "najm-email";
|
|
385
|
+
import { nanoid as nanoid5 } from "nanoid";
|
|
385
386
|
|
|
386
387
|
// src/users/UserService.ts
|
|
387
388
|
import { Injectable as Injectable5, Inject as Inject5 } from "najm-core";
|
|
@@ -1861,23 +1862,39 @@ var TokenService = class TokenService2 {
|
|
|
1861
1862
|
}
|
|
1862
1863
|
// ============ PASSWORD RESET TOKENS ============
|
|
1863
1864
|
/**
|
|
1864
|
-
* Generate
|
|
1865
|
-
*
|
|
1865
|
+
* Generate a one-time set-password token (used by both password reset and
|
|
1866
|
+
* account invites). Stores the jti in cache so the token can only be used
|
|
1867
|
+
* once, with a TTL matching the JWT expiry.
|
|
1866
1868
|
*/
|
|
1867
|
-
async
|
|
1869
|
+
async generateSetPasswordToken(userId, type, expiresIn) {
|
|
1868
1870
|
const jti = nanoid4(16);
|
|
1869
|
-
const
|
|
1871
|
+
const data = {
|
|
1870
1872
|
userId,
|
|
1871
|
-
type
|
|
1873
|
+
type,
|
|
1872
1874
|
jti,
|
|
1873
1875
|
timestamp: Date.now()
|
|
1874
1876
|
};
|
|
1875
|
-
const token = jwt.sign(
|
|
1876
|
-
expiresIn
|
|
1877
|
+
const token = jwt.sign(data, this.config.jwt.refreshSecret, {
|
|
1878
|
+
expiresIn
|
|
1877
1879
|
});
|
|
1878
|
-
await this.cache.set(`${this.resetTokenPrefix}${userId}`, jti,
|
|
1880
|
+
await this.cache.set(`${this.resetTokenPrefix}${userId}`, jti, timestring2(expiresIn, "ms"));
|
|
1879
1881
|
return { token, userId };
|
|
1880
1882
|
}
|
|
1883
|
+
/**
|
|
1884
|
+
* Generate secure password reset token
|
|
1885
|
+
* Returns both the plain token (to send via email) and userId for identification
|
|
1886
|
+
*/
|
|
1887
|
+
async generateResetToken(userId) {
|
|
1888
|
+
return this.generateSetPasswordToken(userId, "reset", "1h");
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* Generate secure account-invite token.
|
|
1892
|
+
* Longer expiry (3d) than reset because an invited user may not check
|
|
1893
|
+
* their email immediately. Consumed via the same reset-password endpoint.
|
|
1894
|
+
*/
|
|
1895
|
+
async generateInviteToken(userId) {
|
|
1896
|
+
return this.generateSetPasswordToken(userId, "invite", "3d");
|
|
1897
|
+
}
|
|
1881
1898
|
/**
|
|
1882
1899
|
* Verify password reset token
|
|
1883
1900
|
* Returns userId if valid, throws error if expired/invalid
|
|
@@ -1889,7 +1906,7 @@ var TokenService = class TokenService2 {
|
|
|
1889
1906
|
} catch {
|
|
1890
1907
|
Err5(this.t("errors.resetTokenExpired"));
|
|
1891
1908
|
}
|
|
1892
|
-
if (decoded.type !== "reset" || !decoded.jti) {
|
|
1909
|
+
if (decoded.type !== "reset" && decoded.type !== "invite" || !decoded.jti) {
|
|
1893
1910
|
Err5(this.t("errors.invalidResetToken"));
|
|
1894
1911
|
}
|
|
1895
1912
|
const key = `${this.resetTokenPrefix}${decoded.userId}`;
|
|
@@ -1978,6 +1995,38 @@ var AuthService = class AuthService2 {
|
|
|
1978
1995
|
async registerUser(body) {
|
|
1979
1996
|
return await this.userService.create(body);
|
|
1980
1997
|
}
|
|
1998
|
+
/**
|
|
1999
|
+
* Admin-initiated account creation. The user is created with a random,
|
|
2000
|
+
* unusable password (the schema requires one) and then emailed a one-time
|
|
2001
|
+
* link to set their own. They can't log in until they do, since they never
|
|
2002
|
+
* learn the random password.
|
|
2003
|
+
*
|
|
2004
|
+
* Email is best-effort: a send failure logs a warning but never rolls back
|
|
2005
|
+
* account creation (and with the console provider, nothing is actually sent).
|
|
2006
|
+
*/
|
|
2007
|
+
async inviteUser(body) {
|
|
2008
|
+
const randomPassword = `${nanoid5(24)}Aa1!`;
|
|
2009
|
+
const user = await this.userService.create({
|
|
2010
|
+
name: body.name,
|
|
2011
|
+
email: body.email,
|
|
2012
|
+
roleId: body.roleId,
|
|
2013
|
+
image: body.image,
|
|
2014
|
+
password: randomPassword,
|
|
2015
|
+
status: "active",
|
|
2016
|
+
emailVerified: false
|
|
2017
|
+
});
|
|
2018
|
+
const { token } = await this.tokenService.generateInviteToken(user.id);
|
|
2019
|
+
const inviteLink = `${this.config.frontendUrl}/reset-password?token=${token}`;
|
|
2020
|
+
try {
|
|
2021
|
+
await this.emailService.sendHtml(body.email, this.t("emails.accountInvite.subject"), accountInviteTemplate({
|
|
2022
|
+
inviteLink,
|
|
2023
|
+
userName: user.name || body.email
|
|
2024
|
+
}));
|
|
2025
|
+
} catch (error) {
|
|
2026
|
+
this.logger.warn("Account invite email failed", { email: body.email, error });
|
|
2027
|
+
}
|
|
2028
|
+
return user;
|
|
2029
|
+
}
|
|
1981
2030
|
async loginUser(body) {
|
|
1982
2031
|
const { email: email2, password } = body;
|
|
1983
2032
|
const user = await this.userService.findByEmail(email2);
|
|
@@ -2191,78 +2240,309 @@ AuthGuard = __decorate12([
|
|
|
2191
2240
|
], AuthGuard);
|
|
2192
2241
|
var isAuth = createGuard(AuthGuard);
|
|
2193
2242
|
|
|
2194
|
-
// src/
|
|
2243
|
+
// src/roles/index.ts
|
|
2244
|
+
var roles_exports = {};
|
|
2245
|
+
__export(roles_exports, {
|
|
2246
|
+
ROLES: () => ROLES,
|
|
2247
|
+
ROLE_GROUPS: () => ROLE_GROUPS,
|
|
2248
|
+
Role: () => Role,
|
|
2249
|
+
RoleController: () => RoleController,
|
|
2250
|
+
RoleGuard: () => RoleGuard,
|
|
2251
|
+
RoleRepository: () => RoleRepository,
|
|
2252
|
+
RoleService: () => RoleService,
|
|
2253
|
+
RoleValidator: () => RoleValidator,
|
|
2254
|
+
assignRoleDto: () => assignRoleDto,
|
|
2255
|
+
createRoleDto: () => createRoleDto,
|
|
2256
|
+
defineRoles: () => defineRoles,
|
|
2257
|
+
isAdmin: () => isAdmin,
|
|
2258
|
+
isAdministrator: () => isAdministrator,
|
|
2259
|
+
roleIdParam: () => roleIdParam,
|
|
2260
|
+
updateRoleDto: () => updateRoleDto
|
|
2261
|
+
});
|
|
2262
|
+
|
|
2263
|
+
// src/roles/defineRoles.ts
|
|
2264
|
+
import { composeGuards as composeGuards2, createGuard as createGuard3 } from "najm-guard";
|
|
2265
|
+
|
|
2266
|
+
// src/roles/RoleGuards.ts
|
|
2267
|
+
import { Service as Service3 } from "najm-core";
|
|
2268
|
+
import { GuardParams, User as User2 } from "najm-core";
|
|
2269
|
+
import { composeGuards, createGuard as createGuard2 } from "najm-guard";
|
|
2270
|
+
var __decorate13 = function(decorators, target, key, desc) {
|
|
2271
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2272
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2273
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2274
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2275
|
+
};
|
|
2276
|
+
var __metadata13 = function(k, v) {
|
|
2277
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
2278
|
+
};
|
|
2279
|
+
var __param4 = function(paramIndex, decorator) {
|
|
2280
|
+
return function(target, key) {
|
|
2281
|
+
decorator(target, key, paramIndex);
|
|
2282
|
+
};
|
|
2283
|
+
};
|
|
2284
|
+
var RoleGuard = class RoleGuard2 {
|
|
2285
|
+
static {
|
|
2286
|
+
__name(this, "RoleGuard");
|
|
2287
|
+
}
|
|
2288
|
+
canActivate(allowedRoles, userRole) {
|
|
2289
|
+
if (!userRole)
|
|
2290
|
+
return false;
|
|
2291
|
+
const requiredRoles = Array.isArray(allowedRoles) ? allowedRoles : [allowedRoles];
|
|
2292
|
+
const hasRole = requiredRoles.some((r) => r.toLowerCase() === userRole.toLowerCase());
|
|
2293
|
+
if (hasRole) {
|
|
2294
|
+
return { role: userRole };
|
|
2295
|
+
}
|
|
2296
|
+
return false;
|
|
2297
|
+
}
|
|
2298
|
+
};
|
|
2299
|
+
__decorate13([
|
|
2300
|
+
__param4(0, GuardParams()),
|
|
2301
|
+
__param4(1, User2("role")),
|
|
2302
|
+
__metadata13("design:type", Function),
|
|
2303
|
+
__metadata13("design:paramtypes", [Object, String]),
|
|
2304
|
+
__metadata13("design:returntype", void 0)
|
|
2305
|
+
], RoleGuard.prototype, "canActivate", null);
|
|
2306
|
+
RoleGuard = __decorate13([
|
|
2307
|
+
Service3()
|
|
2308
|
+
], RoleGuard);
|
|
2309
|
+
var Role = createGuard2(RoleGuard);
|
|
2310
|
+
var isAdmin = composeGuards(isAuth(), Role(ROLES.ADMIN));
|
|
2311
|
+
var isAdministrator = composeGuards(isAuth(), Role(ROLE_GROUPS.ADMINISTRATORS));
|
|
2312
|
+
|
|
2313
|
+
// src/roles/defineRoles.ts
|
|
2314
|
+
var Role2 = createGuard3(RoleGuard);
|
|
2315
|
+
function defineRoles(roles, options) {
|
|
2316
|
+
const ROLES2 = roles;
|
|
2317
|
+
const superRoleKeys = options?.superRoles ?? [];
|
|
2318
|
+
function resolveRoleValues(keys) {
|
|
2319
|
+
return Array.from(new Set([...keys, ...superRoleKeys].map((key) => roles[key])));
|
|
2320
|
+
}
|
|
2321
|
+
__name(resolveRoleValues, "resolveRoleValues");
|
|
2322
|
+
const guards2 = {};
|
|
2323
|
+
for (const [key, value] of Object.entries(roles)) {
|
|
2324
|
+
const name = `is${key.charAt(0).toUpperCase()}${key.slice(1).toLowerCase()}`;
|
|
2325
|
+
const allowedValues = resolveRoleValues([key]);
|
|
2326
|
+
guards2[name] = composeGuards2(isAuth(), Role2(allowedValues.length === 1 ? value : allowedValues));
|
|
2327
|
+
}
|
|
2328
|
+
function createGroupGuard(keys) {
|
|
2329
|
+
const values = resolveRoleValues(keys);
|
|
2330
|
+
return composeGuards2(isAuth(), Role2(values));
|
|
2331
|
+
}
|
|
2332
|
+
__name(createGroupGuard, "createGroupGuard");
|
|
2333
|
+
function hasRole(userRole, ...keys) {
|
|
2334
|
+
if (!userRole)
|
|
2335
|
+
return false;
|
|
2336
|
+
const normalized = userRole.toLowerCase();
|
|
2337
|
+
return resolveRoleValues(keys).some((role) => role === normalized);
|
|
2338
|
+
}
|
|
2339
|
+
__name(hasRole, "hasRole");
|
|
2340
|
+
function isInGroup(userRole, keys) {
|
|
2341
|
+
return hasRole(userRole, ...keys);
|
|
2342
|
+
}
|
|
2343
|
+
__name(isInGroup, "isInGroup");
|
|
2344
|
+
return { ROLES: ROLES2, createGroupGuard, hasRole, isInGroup, ...guards2 };
|
|
2345
|
+
}
|
|
2346
|
+
__name(defineRoles, "defineRoles");
|
|
2347
|
+
|
|
2348
|
+
// src/roles/RoleController.ts
|
|
2349
|
+
import { Controller } from "najm-core";
|
|
2350
|
+
import { Get, Post, Put, Delete, ResMsg } from "najm-core";
|
|
2351
|
+
import { Params, Body } from "najm-core";
|
|
2195
2352
|
import { Validate } from "najm-validation";
|
|
2353
|
+
|
|
2354
|
+
// src/roles/RoleDto.ts
|
|
2355
|
+
import { z } from "zod";
|
|
2356
|
+
var nameField = z.string().min(2, "Name must be at least 2 characters").max(50, "Name too long");
|
|
2357
|
+
var descriptionField = z.string().max(255, "Description too long").optional();
|
|
2358
|
+
var createRoleDto = z.object({
|
|
2359
|
+
name: nameField,
|
|
2360
|
+
description: descriptionField
|
|
2361
|
+
});
|
|
2362
|
+
var updateRoleDto = createRoleDto.partial();
|
|
2363
|
+
var roleIdParam = z.object({
|
|
2364
|
+
id: z.string().length(5, "Role ID must be 5 characters")
|
|
2365
|
+
});
|
|
2366
|
+
var assignRoleDto = z.object({
|
|
2367
|
+
userId: z.string().length(8, "User ID must be 8 characters"),
|
|
2368
|
+
roleId: z.string().length(5, "Role ID must be 5 characters")
|
|
2369
|
+
});
|
|
2370
|
+
|
|
2371
|
+
// src/roles/RoleController.ts
|
|
2372
|
+
var __decorate14 = function(decorators, target, key, desc) {
|
|
2373
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2374
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2375
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2376
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2377
|
+
};
|
|
2378
|
+
var __metadata14 = function(k, v) {
|
|
2379
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
2380
|
+
};
|
|
2381
|
+
var __param5 = function(paramIndex, decorator) {
|
|
2382
|
+
return function(target, key) {
|
|
2383
|
+
decorator(target, key, paramIndex);
|
|
2384
|
+
};
|
|
2385
|
+
};
|
|
2386
|
+
var _a8;
|
|
2387
|
+
var RoleController = class RoleController2 {
|
|
2388
|
+
static {
|
|
2389
|
+
__name(this, "RoleController");
|
|
2390
|
+
}
|
|
2391
|
+
roleService;
|
|
2392
|
+
constructor(roleService) {
|
|
2393
|
+
this.roleService = roleService;
|
|
2394
|
+
}
|
|
2395
|
+
async getRoles() {
|
|
2396
|
+
return this.roleService.getAll();
|
|
2397
|
+
}
|
|
2398
|
+
async getRole(params) {
|
|
2399
|
+
return this.roleService.getById(params.id);
|
|
2400
|
+
}
|
|
2401
|
+
async createRole(body) {
|
|
2402
|
+
return this.roleService.create(body);
|
|
2403
|
+
}
|
|
2404
|
+
async updateRole(params, body) {
|
|
2405
|
+
return this.roleService.update(params.id, body);
|
|
2406
|
+
}
|
|
2407
|
+
async deleteRole(params) {
|
|
2408
|
+
return this.roleService.delete(params.id);
|
|
2409
|
+
}
|
|
2410
|
+
};
|
|
2411
|
+
__decorate14([
|
|
2412
|
+
Get(),
|
|
2413
|
+
isAdmin(),
|
|
2414
|
+
ResMsg("roles.success.retrieved"),
|
|
2415
|
+
__metadata14("design:type", Function),
|
|
2416
|
+
__metadata14("design:paramtypes", []),
|
|
2417
|
+
__metadata14("design:returntype", Promise)
|
|
2418
|
+
], RoleController.prototype, "getRoles", null);
|
|
2419
|
+
__decorate14([
|
|
2420
|
+
Get("/:id"),
|
|
2421
|
+
isAdmin(),
|
|
2422
|
+
Validate({ params: roleIdParam }),
|
|
2423
|
+
ResMsg("roles.success.retrieved"),
|
|
2424
|
+
__param5(0, Params()),
|
|
2425
|
+
__metadata14("design:type", Function),
|
|
2426
|
+
__metadata14("design:paramtypes", [Object]),
|
|
2427
|
+
__metadata14("design:returntype", Promise)
|
|
2428
|
+
], RoleController.prototype, "getRole", null);
|
|
2429
|
+
__decorate14([
|
|
2430
|
+
Post(),
|
|
2431
|
+
isAdmin(),
|
|
2432
|
+
Validate(createRoleDto),
|
|
2433
|
+
ResMsg("roles.success.created"),
|
|
2434
|
+
__param5(0, Body()),
|
|
2435
|
+
__metadata14("design:type", Function),
|
|
2436
|
+
__metadata14("design:paramtypes", [Object]),
|
|
2437
|
+
__metadata14("design:returntype", Promise)
|
|
2438
|
+
], RoleController.prototype, "createRole", null);
|
|
2439
|
+
__decorate14([
|
|
2440
|
+
Put("/:id"),
|
|
2441
|
+
isAdmin(),
|
|
2442
|
+
Validate({
|
|
2443
|
+
params: roleIdParam,
|
|
2444
|
+
body: updateRoleDto
|
|
2445
|
+
}),
|
|
2446
|
+
ResMsg("roles.success.updated"),
|
|
2447
|
+
__param5(0, Params()),
|
|
2448
|
+
__param5(1, Body()),
|
|
2449
|
+
__metadata14("design:type", Function),
|
|
2450
|
+
__metadata14("design:paramtypes", [Object, Object]),
|
|
2451
|
+
__metadata14("design:returntype", Promise)
|
|
2452
|
+
], RoleController.prototype, "updateRole", null);
|
|
2453
|
+
__decorate14([
|
|
2454
|
+
Delete("/:id"),
|
|
2455
|
+
isAdmin(),
|
|
2456
|
+
Validate({ params: roleIdParam }),
|
|
2457
|
+
ResMsg("roles.success.deleted"),
|
|
2458
|
+
__param5(0, Params()),
|
|
2459
|
+
__metadata14("design:type", Function),
|
|
2460
|
+
__metadata14("design:paramtypes", [Object]),
|
|
2461
|
+
__metadata14("design:returntype", Promise)
|
|
2462
|
+
], RoleController.prototype, "deleteRole", null);
|
|
2463
|
+
RoleController = __decorate14([
|
|
2464
|
+
Controller("/roles"),
|
|
2465
|
+
__metadata14("design:paramtypes", [typeof (_a8 = typeof RoleService !== "undefined" && RoleService) === "function" ? _a8 : Object])
|
|
2466
|
+
], RoleController);
|
|
2467
|
+
|
|
2468
|
+
// src/auth/AuthController.ts
|
|
2469
|
+
import { Validate as Validate2 } from "najm-validation";
|
|
2196
2470
|
import { RateLimit } from "najm-rate";
|
|
2197
2471
|
import { createHash as createHash2 } from "crypto";
|
|
2198
2472
|
|
|
2199
2473
|
// src/users/UserDto.ts
|
|
2200
|
-
import { z } from "zod";
|
|
2201
|
-
var emailField =
|
|
2202
|
-
var passwordField =
|
|
2203
|
-
var optionalDateField =
|
|
2204
|
-
var createUserDto =
|
|
2205
|
-
name:
|
|
2474
|
+
import { z as z2 } from "zod";
|
|
2475
|
+
var emailField = z2.string().email("Invalid email format");
|
|
2476
|
+
var passwordField = z2.string().min(8, "Password must be at least 8 characters");
|
|
2477
|
+
var optionalDateField = z2.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Date must be in YYYY-MM-DD format").nullable().optional();
|
|
2478
|
+
var createUserDto = z2.object({
|
|
2479
|
+
name: z2.string().max(100).optional(),
|
|
2206
2480
|
email: emailField,
|
|
2207
2481
|
password: passwordField,
|
|
2208
|
-
roleId:
|
|
2209
|
-
image:
|
|
2210
|
-
emailVerified:
|
|
2211
|
-
status:
|
|
2482
|
+
roleId: z2.string().min(1).optional(),
|
|
2483
|
+
image: z2.string().nullish(),
|
|
2484
|
+
emailVerified: z2.boolean().default(false),
|
|
2485
|
+
status: z2.enum(["active", "inactive", "pending"]).optional()
|
|
2212
2486
|
});
|
|
2213
2487
|
var updateUserDto = createUserDto.partial();
|
|
2214
|
-
var
|
|
2215
|
-
|
|
2488
|
+
var inviteUserDto = z2.object({
|
|
2489
|
+
name: z2.string().max(100).optional(),
|
|
2490
|
+
email: emailField,
|
|
2491
|
+
roleId: z2.string().min(1).optional(),
|
|
2492
|
+
image: z2.string().nullish()
|
|
2216
2493
|
});
|
|
2217
|
-
var
|
|
2494
|
+
var userIdParam = z2.object({
|
|
2495
|
+
id: z2.string().min(1, "User ID is required")
|
|
2496
|
+
});
|
|
2497
|
+
var loginDto = z2.object({
|
|
2218
2498
|
email: emailField,
|
|
2219
2499
|
password: passwordField
|
|
2220
2500
|
});
|
|
2221
|
-
var changePasswordDto =
|
|
2501
|
+
var changePasswordDto = z2.object({
|
|
2222
2502
|
currentPassword: passwordField,
|
|
2223
2503
|
newPassword: passwordField
|
|
2224
2504
|
});
|
|
2225
|
-
var resetPasswordDto =
|
|
2505
|
+
var resetPasswordDto = z2.object({
|
|
2226
2506
|
email: emailField
|
|
2227
2507
|
});
|
|
2228
|
-
var confirmResetPasswordDto =
|
|
2229
|
-
token:
|
|
2508
|
+
var confirmResetPasswordDto = z2.object({
|
|
2509
|
+
token: z2.string().min(10, "Invalid reset token"),
|
|
2230
2510
|
newPassword: passwordField
|
|
2231
2511
|
});
|
|
2232
|
-
var languageParam =
|
|
2233
|
-
language:
|
|
2512
|
+
var languageParam = z2.object({
|
|
2513
|
+
language: z2.string().min(2)
|
|
2234
2514
|
});
|
|
2235
|
-
var emailParam =
|
|
2515
|
+
var emailParam = z2.object({
|
|
2236
2516
|
email: emailField
|
|
2237
2517
|
});
|
|
2238
|
-
var userIdInParam =
|
|
2239
|
-
userId:
|
|
2518
|
+
var userIdInParam = z2.object({
|
|
2519
|
+
userId: z2.string().min(1, "User ID is required")
|
|
2240
2520
|
});
|
|
2241
|
-
var assignRoleParams =
|
|
2242
|
-
userId:
|
|
2243
|
-
roleId:
|
|
2521
|
+
var assignRoleParams = z2.object({
|
|
2522
|
+
userId: z2.string().min(1, "User ID is required"),
|
|
2523
|
+
roleId: z2.string().min(1)
|
|
2244
2524
|
});
|
|
2245
|
-
var userListQuery =
|
|
2246
|
-
limit:
|
|
2247
|
-
offset:
|
|
2525
|
+
var userListQuery = z2.object({
|
|
2526
|
+
limit: z2.coerce.number().int().min(1).max(100).default(50),
|
|
2527
|
+
offset: z2.coerce.number().int().min(0).default(0)
|
|
2248
2528
|
});
|
|
2249
2529
|
|
|
2250
2530
|
// src/auth/AuthController.ts
|
|
2251
|
-
var
|
|
2531
|
+
var __decorate15 = function(decorators, target, key, desc) {
|
|
2252
2532
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2253
2533
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2254
2534
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2255
2535
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2256
2536
|
};
|
|
2257
|
-
var
|
|
2537
|
+
var __metadata15 = function(k, v) {
|
|
2258
2538
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
2259
2539
|
};
|
|
2260
|
-
var
|
|
2540
|
+
var __param6 = function(paramIndex, decorator) {
|
|
2261
2541
|
return function(target, key) {
|
|
2262
2542
|
decorator(target, key, paramIndex);
|
|
2263
2543
|
};
|
|
2264
2544
|
};
|
|
2265
|
-
var
|
|
2545
|
+
var _a9;
|
|
2266
2546
|
var hashKeyPart = /* @__PURE__ */ __name((value) => createHash2("sha256").update(value).digest("base64url").slice(0, 32), "hashKeyPart");
|
|
2267
2547
|
var cookieFingerprint = /* @__PURE__ */ __name(() => (ctx) => {
|
|
2268
2548
|
const ip = ctx.req.header("x-forwarded-for")?.split(",")[0]?.trim() ?? ctx.req.header("x-real-ip") ?? "unknown";
|
|
@@ -2297,6 +2577,9 @@ var AuthController = class AuthController2 {
|
|
|
2297
2577
|
async loginUser(body) {
|
|
2298
2578
|
return this.authService.loginUser(body);
|
|
2299
2579
|
}
|
|
2580
|
+
async inviteUser(body) {
|
|
2581
|
+
return this.authService.inviteUser(body);
|
|
2582
|
+
}
|
|
2300
2583
|
async refreshTokens() {
|
|
2301
2584
|
return this.authService.refreshTokens();
|
|
2302
2585
|
}
|
|
@@ -2316,102 +2599,113 @@ var AuthController = class AuthController2 {
|
|
|
2316
2599
|
return this.authService.resetPassword(body.token, body.newPassword);
|
|
2317
2600
|
}
|
|
2318
2601
|
};
|
|
2319
|
-
|
|
2320
|
-
|
|
2602
|
+
__decorate15([
|
|
2603
|
+
Post2("/register"),
|
|
2321
2604
|
RateLimit({ limit: 5, window: "15m", key: ipAndEmail }),
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2605
|
+
Validate2(createUserDto),
|
|
2606
|
+
ResMsg2("auth.success.register"),
|
|
2607
|
+
__param6(0, Body2()),
|
|
2608
|
+
__metadata15("design:type", Function),
|
|
2609
|
+
__metadata15("design:paramtypes", [Object]),
|
|
2610
|
+
__metadata15("design:returntype", Promise)
|
|
2328
2611
|
], AuthController.prototype, "registerUser", null);
|
|
2329
|
-
|
|
2330
|
-
|
|
2612
|
+
__decorate15([
|
|
2613
|
+
Post2("/login"),
|
|
2331
2614
|
RateLimit({ limit: 5, window: "15m", key: ipAndEmail, message: "Too many login attempts. Please try again later." }),
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2615
|
+
Validate2(loginDto),
|
|
2616
|
+
ResMsg2("auth.success.login"),
|
|
2617
|
+
__param6(0, Body2()),
|
|
2618
|
+
__metadata15("design:type", Function),
|
|
2619
|
+
__metadata15("design:paramtypes", [Object]),
|
|
2620
|
+
__metadata15("design:returntype", Promise)
|
|
2338
2621
|
], AuthController.prototype, "loginUser", null);
|
|
2339
|
-
|
|
2340
|
-
|
|
2622
|
+
__decorate15([
|
|
2623
|
+
Post2("/invite"),
|
|
2624
|
+
isAdmin(),
|
|
2625
|
+
RateLimit({ limit: 20, window: "15m", key: "user" }),
|
|
2626
|
+
Validate2(inviteUserDto),
|
|
2627
|
+
ResMsg2("auth.success.accountInviteSent"),
|
|
2628
|
+
__param6(0, Body2()),
|
|
2629
|
+
__metadata15("design:type", Function),
|
|
2630
|
+
__metadata15("design:paramtypes", [Object]),
|
|
2631
|
+
__metadata15("design:returntype", Promise)
|
|
2632
|
+
], AuthController.prototype, "inviteUser", null);
|
|
2633
|
+
__decorate15([
|
|
2634
|
+
Post2("/refresh"),
|
|
2341
2635
|
RateLimit({ limit: 15, window: "15m", key: cookieFingerprint() }),
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2636
|
+
ResMsg2("auth.success.tokenRefreshed"),
|
|
2637
|
+
__metadata15("design:type", Function),
|
|
2638
|
+
__metadata15("design:paramtypes", []),
|
|
2639
|
+
__metadata15("design:returntype", Promise)
|
|
2346
2640
|
], AuthController.prototype, "refreshTokens", null);
|
|
2347
|
-
|
|
2348
|
-
|
|
2641
|
+
__decorate15([
|
|
2642
|
+
Post2("/logout"),
|
|
2349
2643
|
isAuth(),
|
|
2350
2644
|
RateLimit({ limit: 10, window: "15m", key: "user" }),
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2645
|
+
__param6(0, User3("id")),
|
|
2646
|
+
__param6(1, Headers("authorization")),
|
|
2647
|
+
__metadata15("design:type", Function),
|
|
2648
|
+
__metadata15("design:paramtypes", [String, String]),
|
|
2649
|
+
__metadata15("design:returntype", Promise)
|
|
2356
2650
|
], AuthController.prototype, "logoutUser", null);
|
|
2357
|
-
|
|
2358
|
-
|
|
2651
|
+
__decorate15([
|
|
2652
|
+
Post2("/change-password"),
|
|
2359
2653
|
isAuth(),
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2654
|
+
Validate2(changePasswordDto),
|
|
2655
|
+
ResMsg2("auth.success.passwordChanged"),
|
|
2656
|
+
__param6(0, User3("id")),
|
|
2657
|
+
__param6(1, Body2()),
|
|
2658
|
+
__metadata15("design:type", Function),
|
|
2659
|
+
__metadata15("design:paramtypes", [String, Object]),
|
|
2660
|
+
__metadata15("design:returntype", Promise)
|
|
2367
2661
|
], AuthController.prototype, "changePassword", null);
|
|
2368
|
-
|
|
2369
|
-
|
|
2662
|
+
__decorate15([
|
|
2663
|
+
Get2("/me"),
|
|
2370
2664
|
RateLimit({ limit: 30, window: "1m", key: cookieFingerprint() }),
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2665
|
+
ResMsg2("auth.users.success.retrieved"),
|
|
2666
|
+
__param6(0, Headers("authorization")),
|
|
2667
|
+
__metadata15("design:type", Function),
|
|
2668
|
+
__metadata15("design:paramtypes", [String]),
|
|
2669
|
+
__metadata15("design:returntype", Promise)
|
|
2376
2670
|
], AuthController.prototype, "userProfile", null);
|
|
2377
|
-
|
|
2378
|
-
|
|
2671
|
+
__decorate15([
|
|
2672
|
+
Post2("/forgot-password"),
|
|
2379
2673
|
RateLimit({ limit: 3, window: "15m", key: ipAndEmail, message: "Too many password reset requests. Please try again later." }),
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2674
|
+
Validate2(resetPasswordDto),
|
|
2675
|
+
ResMsg2("auth.success.passwordResetSent"),
|
|
2676
|
+
__param6(0, Body2()),
|
|
2677
|
+
__metadata15("design:type", Function),
|
|
2678
|
+
__metadata15("design:paramtypes", [Object]),
|
|
2679
|
+
__metadata15("design:returntype", Promise)
|
|
2386
2680
|
], AuthController.prototype, "forgotPassword", null);
|
|
2387
|
-
|
|
2388
|
-
|
|
2681
|
+
__decorate15([
|
|
2682
|
+
Post2("/reset-password"),
|
|
2389
2683
|
RateLimit({ limit: 5, window: "15m", key: "ip", message: "Too many password reset attempts. Please try again later." }),
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2684
|
+
Validate2(confirmResetPasswordDto),
|
|
2685
|
+
ResMsg2("auth.success.passwordReset"),
|
|
2686
|
+
__param6(0, Body2()),
|
|
2687
|
+
__metadata15("design:type", Function),
|
|
2688
|
+
__metadata15("design:paramtypes", [Object]),
|
|
2689
|
+
__metadata15("design:returntype", Promise)
|
|
2396
2690
|
], AuthController.prototype, "resetPassword", null);
|
|
2397
|
-
AuthController =
|
|
2398
|
-
|
|
2399
|
-
|
|
2691
|
+
AuthController = __decorate15([
|
|
2692
|
+
Controller2("/auth"),
|
|
2693
|
+
__metadata15("design:paramtypes", [typeof (_a9 = typeof AuthService !== "undefined" && AuthService) === "function" ? _a9 : Object])
|
|
2400
2694
|
], AuthController);
|
|
2401
2695
|
|
|
2402
2696
|
// src/auth/AuthResolver.ts
|
|
2403
|
-
import { APP, Container, DI, Inject as Inject9, LOGGER, Meta, Service as
|
|
2697
|
+
import { APP, Container, DI, Inject as Inject9, LOGGER, Meta, Service as Service4 } from "najm-core";
|
|
2404
2698
|
import { USER, ROLE, PERMISSIONS } from "najm-guard";
|
|
2405
|
-
var
|
|
2699
|
+
var __decorate16 = function(decorators, target, key, desc) {
|
|
2406
2700
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2407
2701
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2408
2702
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2409
2703
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2410
2704
|
};
|
|
2411
|
-
var
|
|
2705
|
+
var __metadata16 = function(k, v) {
|
|
2412
2706
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
2413
2707
|
};
|
|
2414
|
-
var
|
|
2708
|
+
var _a10;
|
|
2415
2709
|
var AuthResolver = class AuthResolver2 {
|
|
2416
2710
|
static {
|
|
2417
2711
|
__name(this, "AuthResolver");
|
|
@@ -2509,20 +2803,20 @@ var AuthResolver = class AuthResolver2 {
|
|
|
2509
2803
|
await authService.warmupPasswordHash();
|
|
2510
2804
|
}
|
|
2511
2805
|
};
|
|
2512
|
-
|
|
2806
|
+
__decorate16([
|
|
2513
2807
|
DI(),
|
|
2514
|
-
|
|
2808
|
+
__metadata16("design:type", typeof (_a10 = typeof Container !== "undefined" && Container) === "function" ? _a10 : Object)
|
|
2515
2809
|
], AuthResolver.prototype, "container", void 0);
|
|
2516
|
-
|
|
2810
|
+
__decorate16([
|
|
2517
2811
|
Inject9(APP),
|
|
2518
|
-
|
|
2812
|
+
__metadata16("design:type", Object)
|
|
2519
2813
|
], AuthResolver.prototype, "app", void 0);
|
|
2520
|
-
|
|
2814
|
+
__decorate16([
|
|
2521
2815
|
Inject9(LOGGER),
|
|
2522
|
-
|
|
2816
|
+
__metadata16("design:type", Object)
|
|
2523
2817
|
], AuthResolver.prototype, "log", void 0);
|
|
2524
|
-
AuthResolver =
|
|
2525
|
-
|
|
2818
|
+
AuthResolver = __decorate16([
|
|
2819
|
+
Service4(),
|
|
2526
2820
|
Meta({ layer: "plugin", order: 30 })
|
|
2527
2821
|
], AuthResolver);
|
|
2528
2822
|
|
|
@@ -2571,6 +2865,7 @@ __export(users_exports, {
|
|
|
2571
2865
|
confirmResetPasswordDto: () => confirmResetPasswordDto,
|
|
2572
2866
|
createUserDto: () => createUserDto,
|
|
2573
2867
|
emailParam: () => emailParam,
|
|
2868
|
+
inviteUserDto: () => inviteUserDto,
|
|
2574
2869
|
languageParam: () => languageParam,
|
|
2575
2870
|
loginDto: () => loginDto,
|
|
2576
2871
|
resetPasswordDto: () => resetPasswordDto,
|
|
@@ -2584,233 +2879,6 @@ __export(users_exports, {
|
|
|
2584
2879
|
import { Controller as Controller3 } from "najm-core";
|
|
2585
2880
|
import { Get as Get3, Post as Post3, Put as Put2, Delete as Delete2, ResMsg as ResMsg3 } from "najm-core";
|
|
2586
2881
|
import { Params as Params2, Body as Body3, Query } from "najm-core";
|
|
2587
|
-
|
|
2588
|
-
// src/roles/index.ts
|
|
2589
|
-
var roles_exports = {};
|
|
2590
|
-
__export(roles_exports, {
|
|
2591
|
-
ROLES: () => ROLES,
|
|
2592
|
-
ROLE_GROUPS: () => ROLE_GROUPS,
|
|
2593
|
-
Role: () => Role,
|
|
2594
|
-
RoleController: () => RoleController,
|
|
2595
|
-
RoleGuard: () => RoleGuard,
|
|
2596
|
-
RoleRepository: () => RoleRepository,
|
|
2597
|
-
RoleService: () => RoleService,
|
|
2598
|
-
RoleValidator: () => RoleValidator,
|
|
2599
|
-
assignRoleDto: () => assignRoleDto,
|
|
2600
|
-
createRoleDto: () => createRoleDto,
|
|
2601
|
-
defineRoles: () => defineRoles,
|
|
2602
|
-
isAdmin: () => isAdmin,
|
|
2603
|
-
isAdministrator: () => isAdministrator,
|
|
2604
|
-
roleIdParam: () => roleIdParam,
|
|
2605
|
-
updateRoleDto: () => updateRoleDto
|
|
2606
|
-
});
|
|
2607
|
-
|
|
2608
|
-
// src/roles/defineRoles.ts
|
|
2609
|
-
import { composeGuards as composeGuards2, createGuard as createGuard3 } from "najm-guard";
|
|
2610
|
-
|
|
2611
|
-
// src/roles/RoleGuards.ts
|
|
2612
|
-
import { Service as Service4 } from "najm-core";
|
|
2613
|
-
import { GuardParams, User as User3 } from "najm-core";
|
|
2614
|
-
import { composeGuards, createGuard as createGuard2 } from "najm-guard";
|
|
2615
|
-
var __decorate15 = function(decorators, target, key, desc) {
|
|
2616
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2617
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2618
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2619
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2620
|
-
};
|
|
2621
|
-
var __metadata15 = function(k, v) {
|
|
2622
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
2623
|
-
};
|
|
2624
|
-
var __param5 = function(paramIndex, decorator) {
|
|
2625
|
-
return function(target, key) {
|
|
2626
|
-
decorator(target, key, paramIndex);
|
|
2627
|
-
};
|
|
2628
|
-
};
|
|
2629
|
-
var RoleGuard = class RoleGuard2 {
|
|
2630
|
-
static {
|
|
2631
|
-
__name(this, "RoleGuard");
|
|
2632
|
-
}
|
|
2633
|
-
canActivate(allowedRoles, userRole) {
|
|
2634
|
-
if (!userRole)
|
|
2635
|
-
return false;
|
|
2636
|
-
const requiredRoles = Array.isArray(allowedRoles) ? allowedRoles : [allowedRoles];
|
|
2637
|
-
const hasRole = requiredRoles.some((r) => r.toLowerCase() === userRole.toLowerCase());
|
|
2638
|
-
if (hasRole) {
|
|
2639
|
-
return { role: userRole };
|
|
2640
|
-
}
|
|
2641
|
-
return false;
|
|
2642
|
-
}
|
|
2643
|
-
};
|
|
2644
|
-
__decorate15([
|
|
2645
|
-
__param5(0, GuardParams()),
|
|
2646
|
-
__param5(1, User3("role")),
|
|
2647
|
-
__metadata15("design:type", Function),
|
|
2648
|
-
__metadata15("design:paramtypes", [Object, String]),
|
|
2649
|
-
__metadata15("design:returntype", void 0)
|
|
2650
|
-
], RoleGuard.prototype, "canActivate", null);
|
|
2651
|
-
RoleGuard = __decorate15([
|
|
2652
|
-
Service4()
|
|
2653
|
-
], RoleGuard);
|
|
2654
|
-
var Role = createGuard2(RoleGuard);
|
|
2655
|
-
var isAdmin = composeGuards(isAuth(), Role(ROLES.ADMIN));
|
|
2656
|
-
var isAdministrator = composeGuards(isAuth(), Role(ROLE_GROUPS.ADMINISTRATORS));
|
|
2657
|
-
|
|
2658
|
-
// src/roles/defineRoles.ts
|
|
2659
|
-
var Role2 = createGuard3(RoleGuard);
|
|
2660
|
-
function defineRoles(roles, options) {
|
|
2661
|
-
const ROLES2 = roles;
|
|
2662
|
-
const superRoleKeys = options?.superRoles ?? [];
|
|
2663
|
-
function resolveRoleValues(keys) {
|
|
2664
|
-
return Array.from(new Set([...keys, ...superRoleKeys].map((key) => roles[key])));
|
|
2665
|
-
}
|
|
2666
|
-
__name(resolveRoleValues, "resolveRoleValues");
|
|
2667
|
-
const guards2 = {};
|
|
2668
|
-
for (const [key, value] of Object.entries(roles)) {
|
|
2669
|
-
const name = `is${key.charAt(0).toUpperCase()}${key.slice(1).toLowerCase()}`;
|
|
2670
|
-
const allowedValues = resolveRoleValues([key]);
|
|
2671
|
-
guards2[name] = composeGuards2(isAuth(), Role2(allowedValues.length === 1 ? value : allowedValues));
|
|
2672
|
-
}
|
|
2673
|
-
function createGroupGuard(keys) {
|
|
2674
|
-
const values = resolveRoleValues(keys);
|
|
2675
|
-
return composeGuards2(isAuth(), Role2(values));
|
|
2676
|
-
}
|
|
2677
|
-
__name(createGroupGuard, "createGroupGuard");
|
|
2678
|
-
function hasRole(userRole, ...keys) {
|
|
2679
|
-
if (!userRole)
|
|
2680
|
-
return false;
|
|
2681
|
-
const normalized = userRole.toLowerCase();
|
|
2682
|
-
return resolveRoleValues(keys).some((role) => role === normalized);
|
|
2683
|
-
}
|
|
2684
|
-
__name(hasRole, "hasRole");
|
|
2685
|
-
function isInGroup(userRole, keys) {
|
|
2686
|
-
return hasRole(userRole, ...keys);
|
|
2687
|
-
}
|
|
2688
|
-
__name(isInGroup, "isInGroup");
|
|
2689
|
-
return { ROLES: ROLES2, createGroupGuard, hasRole, isInGroup, ...guards2 };
|
|
2690
|
-
}
|
|
2691
|
-
__name(defineRoles, "defineRoles");
|
|
2692
|
-
|
|
2693
|
-
// src/roles/RoleController.ts
|
|
2694
|
-
import { Controller as Controller2 } from "najm-core";
|
|
2695
|
-
import { Get as Get2, Post as Post2, Put, Delete, ResMsg as ResMsg2 } from "najm-core";
|
|
2696
|
-
import { Params, Body as Body2 } from "najm-core";
|
|
2697
|
-
import { Validate as Validate2 } from "najm-validation";
|
|
2698
|
-
|
|
2699
|
-
// src/roles/RoleDto.ts
|
|
2700
|
-
import { z as z2 } from "zod";
|
|
2701
|
-
var nameField = z2.string().min(2, "Name must be at least 2 characters").max(50, "Name too long");
|
|
2702
|
-
var descriptionField = z2.string().max(255, "Description too long").optional();
|
|
2703
|
-
var createRoleDto = z2.object({
|
|
2704
|
-
name: nameField,
|
|
2705
|
-
description: descriptionField
|
|
2706
|
-
});
|
|
2707
|
-
var updateRoleDto = createRoleDto.partial();
|
|
2708
|
-
var roleIdParam = z2.object({
|
|
2709
|
-
id: z2.string().length(5, "Role ID must be 5 characters")
|
|
2710
|
-
});
|
|
2711
|
-
var assignRoleDto = z2.object({
|
|
2712
|
-
userId: z2.string().length(8, "User ID must be 8 characters"),
|
|
2713
|
-
roleId: z2.string().length(5, "Role ID must be 5 characters")
|
|
2714
|
-
});
|
|
2715
|
-
|
|
2716
|
-
// src/roles/RoleController.ts
|
|
2717
|
-
var __decorate16 = function(decorators, target, key, desc) {
|
|
2718
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2719
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2720
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2721
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2722
|
-
};
|
|
2723
|
-
var __metadata16 = function(k, v) {
|
|
2724
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
2725
|
-
};
|
|
2726
|
-
var __param6 = function(paramIndex, decorator) {
|
|
2727
|
-
return function(target, key) {
|
|
2728
|
-
decorator(target, key, paramIndex);
|
|
2729
|
-
};
|
|
2730
|
-
};
|
|
2731
|
-
var _a10;
|
|
2732
|
-
var RoleController = class RoleController2 {
|
|
2733
|
-
static {
|
|
2734
|
-
__name(this, "RoleController");
|
|
2735
|
-
}
|
|
2736
|
-
roleService;
|
|
2737
|
-
constructor(roleService) {
|
|
2738
|
-
this.roleService = roleService;
|
|
2739
|
-
}
|
|
2740
|
-
async getRoles() {
|
|
2741
|
-
return this.roleService.getAll();
|
|
2742
|
-
}
|
|
2743
|
-
async getRole(params) {
|
|
2744
|
-
return this.roleService.getById(params.id);
|
|
2745
|
-
}
|
|
2746
|
-
async createRole(body) {
|
|
2747
|
-
return this.roleService.create(body);
|
|
2748
|
-
}
|
|
2749
|
-
async updateRole(params, body) {
|
|
2750
|
-
return this.roleService.update(params.id, body);
|
|
2751
|
-
}
|
|
2752
|
-
async deleteRole(params) {
|
|
2753
|
-
return this.roleService.delete(params.id);
|
|
2754
|
-
}
|
|
2755
|
-
};
|
|
2756
|
-
__decorate16([
|
|
2757
|
-
Get2(),
|
|
2758
|
-
isAdmin(),
|
|
2759
|
-
ResMsg2("roles.success.retrieved"),
|
|
2760
|
-
__metadata16("design:type", Function),
|
|
2761
|
-
__metadata16("design:paramtypes", []),
|
|
2762
|
-
__metadata16("design:returntype", Promise)
|
|
2763
|
-
], RoleController.prototype, "getRoles", null);
|
|
2764
|
-
__decorate16([
|
|
2765
|
-
Get2("/:id"),
|
|
2766
|
-
isAdmin(),
|
|
2767
|
-
Validate2({ params: roleIdParam }),
|
|
2768
|
-
ResMsg2("roles.success.retrieved"),
|
|
2769
|
-
__param6(0, Params()),
|
|
2770
|
-
__metadata16("design:type", Function),
|
|
2771
|
-
__metadata16("design:paramtypes", [Object]),
|
|
2772
|
-
__metadata16("design:returntype", Promise)
|
|
2773
|
-
], RoleController.prototype, "getRole", null);
|
|
2774
|
-
__decorate16([
|
|
2775
|
-
Post2(),
|
|
2776
|
-
isAdmin(),
|
|
2777
|
-
Validate2(createRoleDto),
|
|
2778
|
-
ResMsg2("roles.success.created"),
|
|
2779
|
-
__param6(0, Body2()),
|
|
2780
|
-
__metadata16("design:type", Function),
|
|
2781
|
-
__metadata16("design:paramtypes", [Object]),
|
|
2782
|
-
__metadata16("design:returntype", Promise)
|
|
2783
|
-
], RoleController.prototype, "createRole", null);
|
|
2784
|
-
__decorate16([
|
|
2785
|
-
Put("/:id"),
|
|
2786
|
-
isAdmin(),
|
|
2787
|
-
Validate2({
|
|
2788
|
-
params: roleIdParam,
|
|
2789
|
-
body: updateRoleDto
|
|
2790
|
-
}),
|
|
2791
|
-
ResMsg2("roles.success.updated"),
|
|
2792
|
-
__param6(0, Params()),
|
|
2793
|
-
__param6(1, Body2()),
|
|
2794
|
-
__metadata16("design:type", Function),
|
|
2795
|
-
__metadata16("design:paramtypes", [Object, Object]),
|
|
2796
|
-
__metadata16("design:returntype", Promise)
|
|
2797
|
-
], RoleController.prototype, "updateRole", null);
|
|
2798
|
-
__decorate16([
|
|
2799
|
-
Delete("/:id"),
|
|
2800
|
-
isAdmin(),
|
|
2801
|
-
Validate2({ params: roleIdParam }),
|
|
2802
|
-
ResMsg2("roles.success.deleted"),
|
|
2803
|
-
__param6(0, Params()),
|
|
2804
|
-
__metadata16("design:type", Function),
|
|
2805
|
-
__metadata16("design:paramtypes", [Object]),
|
|
2806
|
-
__metadata16("design:returntype", Promise)
|
|
2807
|
-
], RoleController.prototype, "deleteRole", null);
|
|
2808
|
-
RoleController = __decorate16([
|
|
2809
|
-
Controller2("/roles"),
|
|
2810
|
-
__metadata16("design:paramtypes", [typeof (_a10 = typeof RoleService !== "undefined" && RoleService) === "function" ? _a10 : Object])
|
|
2811
|
-
], RoleController);
|
|
2812
|
-
|
|
2813
|
-
// src/users/UserController.ts
|
|
2814
2882
|
import { Validate as Validate3 } from "najm-validation";
|
|
2815
2883
|
var __decorate17 = function(decorators, target, key, desc) {
|
|
2816
2884
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -4294,7 +4362,16 @@ var en_default = {
|
|
|
4294
4362
|
passwordChanged: "Password changed successfully",
|
|
4295
4363
|
passwordResetSent: "If that email exists, a reset link has been sent",
|
|
4296
4364
|
passwordReset: "Password has been reset successfully",
|
|
4365
|
+
accountInviteSent: "Invitation sent successfully",
|
|
4297
4366
|
tokenRefreshed: "Token refreshed successfully"
|
|
4367
|
+
},
|
|
4368
|
+
emails: {
|
|
4369
|
+
passwordReset: {
|
|
4370
|
+
subject: "Reset your password"
|
|
4371
|
+
},
|
|
4372
|
+
accountInvite: {
|
|
4373
|
+
subject: "You've been invited \u2014 set up your account"
|
|
4374
|
+
}
|
|
4298
4375
|
}
|
|
4299
4376
|
},
|
|
4300
4377
|
users: {
|
|
@@ -4619,6 +4696,7 @@ export {
|
|
|
4619
4696
|
formatDate,
|
|
4620
4697
|
getAuthLocale,
|
|
4621
4698
|
getAvatarFile,
|
|
4699
|
+
inviteUserDto,
|
|
4622
4700
|
isAdmin,
|
|
4623
4701
|
isAdministrator,
|
|
4624
4702
|
isAuth,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "najm-auth",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.42",
|
|
4
4
|
"description": "Authentication and authorization library for najm framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -74,12 +74,12 @@
|
|
|
74
74
|
"dependencies": {
|
|
75
75
|
"bcryptjs": "^3.0.2",
|
|
76
76
|
"najm-cookies": "^1.1.14",
|
|
77
|
-
"najm-core": "^1.2.
|
|
77
|
+
"najm-core": "^1.2.14",
|
|
78
78
|
"najm-database": "^1.1.16",
|
|
79
79
|
"najm-guard": "^1.1.14",
|
|
80
80
|
"najm-i18n": "^1.1.14",
|
|
81
81
|
"najm-cache": "^1.2.11",
|
|
82
|
-
"najm-email": "^1.1.
|
|
82
|
+
"najm-email": "^1.1.15",
|
|
83
83
|
"najm-rate": "^1.1.14",
|
|
84
84
|
"najm-validation": "^1.1.15",
|
|
85
85
|
"jsonwebtoken": "^9.0.3",
|