@loomcore/api 0.2.50 → 0.2.52
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 +201 -201
- package/dist/__tests__/common-test.utils.d.ts +2 -0
- package/dist/__tests__/common-test.utils.js +7 -1
- package/dist/__tests__/mongo-db.test-database.js +46 -12
- package/dist/__tests__/test-express-app.js +10 -6
- package/dist/controllers/api.controller.d.ts +1 -0
- package/dist/controllers/api.controller.js +12 -10
- package/dist/controllers/auth.controller.js +6 -2
- package/dist/controllers/authorizations.controller.js +2 -2
- package/dist/controllers/features.controller.js +59 -7
- package/dist/controllers/organizations.controller.js +3 -4
- package/dist/controllers/query-api.controller.d.ts +1 -0
- package/dist/controllers/query-api.controller.js +7 -5
- package/dist/controllers/roles.controller.js +59 -7
- package/dist/controllers/user-roles.controller.js +59 -7
- package/dist/controllers/users.controller.d.ts +4 -1
- package/dist/controllers/users.controller.js +87 -11
- package/dist/services/{authorizations.service.d.ts → authorization.service.d.ts} +1 -1
- package/dist/services/{authorizations.service.js → authorization.service.js} +5 -7
- package/dist/services/feature.service.d.ts +6 -0
- package/dist/services/feature.service.js +7 -0
- package/dist/services/index.d.ts +4 -4
- package/dist/services/index.js +4 -4
- package/dist/services/role.service.d.ts +6 -0
- package/dist/services/role.service.js +7 -0
- package/dist/services/user-role.service.d.ts +6 -0
- package/dist/services/user-role.service.js +7 -0
- package/dist/services/user.service.d.ts +4 -7
- package/dist/services/user.service.js +15 -38
- package/dist/utils/auth/attempt-login.util.d.ts +2 -2
- package/dist/utils/auth/attempt-login.util.js +2 -2
- package/dist/utils/auth/authorize-method.util.d.ts +1 -1
- package/dist/utils/auth/index.d.ts +0 -6
- package/dist/utils/auth/index.js +0 -6
- package/dist/utils/auth/request-token-using-refresh-token.util.d.ts +2 -2
- package/dist/utils/auth/request-token-using-refresh-token.util.js +2 -2
- package/dist/utils/auth/reset-password.util.js +5 -3
- package/package.json +1 -1
- package/dist/services/features.service.d.ts +0 -7
- package/dist/services/features.service.js +0 -12
- package/dist/services/roles.service.d.ts +0 -7
- package/dist/services/roles.service.js +0 -12
- package/dist/services/user-roles.service.d.ts +0 -7
- package/dist/services/user-roles.service.js +0 -12
- package/dist/utils/auth/assert-admin-or-feature.util.d.ts +0 -2
- package/dist/utils/auth/assert-admin-or-feature.util.js +0 -8
- package/dist/utils/auth/assert-admin.util.d.ts +0 -2
- package/dist/utils/auth/assert-admin.util.js +0 -7
- package/dist/utils/auth/assert-owner-or-admin.util.d.ts +0 -2
- package/dist/utils/auth/assert-owner-or-admin.util.js +0 -10
- package/dist/utils/auth/assert-user-has-feature.util.d.ts +0 -2
- package/dist/utils/auth/assert-user-has-feature.util.js +0 -18
- package/dist/utils/auth/assert-user-or-admin.util.d.ts +0 -3
- package/dist/utils/auth/assert-user-or-admin.util.js +0 -10
- package/dist/utils/auth/change-password.util.d.ts +0 -6
- package/dist/utils/auth/change-password.util.js +0 -11
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { RequestHandler } from 'express';
|
|
2
|
-
export declare function authorizeMethod
|
|
2
|
+
export declare function authorizeMethod(instance: object, methodName: string): RequestHandler;
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
export * from './assert-admin.util.js';
|
|
2
|
-
export * from './assert-admin-or-feature.util.js';
|
|
3
|
-
export * from './assert-owner-or-admin.util.js';
|
|
4
|
-
export * from './assert-user-or-admin.util.js';
|
|
5
1
|
export * from './attempt-login.util.js';
|
|
6
2
|
export * from './auth-user-specs.util.js';
|
|
7
|
-
export * from './assert-user-has-feature.util.js';
|
|
8
|
-
export * from './change-password.util.js';
|
|
9
3
|
export * from './create-new-refresh-token.util.js';
|
|
10
4
|
export * from './create-new-tokens.util.js';
|
|
11
5
|
export * from './generate-device-id.util.js';
|
package/dist/utils/auth/index.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
export * from './assert-admin.util.js';
|
|
2
|
-
export * from './assert-admin-or-feature.util.js';
|
|
3
|
-
export * from './assert-owner-or-admin.util.js';
|
|
4
|
-
export * from './assert-user-or-admin.util.js';
|
|
5
1
|
export * from './attempt-login.util.js';
|
|
6
2
|
export * from './auth-user-specs.util.js';
|
|
7
|
-
export * from './assert-user-has-feature.util.js';
|
|
8
|
-
export * from './change-password.util.js';
|
|
9
3
|
export * from './create-new-refresh-token.util.js';
|
|
10
4
|
export * from './create-new-tokens.util.js';
|
|
11
5
|
export * from './generate-device-id.util.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ITokenResponse } from '@loomcore/common/models';
|
|
2
2
|
import type { IDatabase } from '../../databases/models/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { AuthorizationService } from '../../services/authorization.service.js';
|
|
4
4
|
import { UserService } from '../../services/user.service.js';
|
|
5
|
-
export declare function requestTokenUsingRefreshToken(database: IDatabase, refreshToken: string, deviceId: string, userService?: UserService, authorizationsService?:
|
|
5
|
+
export declare function requestTokenUsingRefreshToken(database: IDatabase, refreshToken: string, deviceId: string, userService?: UserService, authorizationsService?: AuthorizationService): Promise<ITokenResponse | null>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { getSystemUserContext, } from '@loomcore/common/models';
|
|
2
|
-
import {
|
|
2
|
+
import { AuthorizationService } from '../../services/authorization.service.js';
|
|
3
3
|
import { UserService } from '../../services/user.service.js';
|
|
4
4
|
import { createNewTokens } from './create-new-tokens.util.js';
|
|
5
5
|
import { getActiveRefreshToken } from './get-active-refresh-token.util.js';
|
|
6
6
|
import { BadRequestError } from '../../errors/index.js';
|
|
7
|
-
export async function requestTokenUsingRefreshToken(database, refreshToken, deviceId, userService = new UserService(database), authorizationsService = new
|
|
7
|
+
export async function requestTokenUsingRefreshToken(database, refreshToken, deviceId, userService = new UserService(database), authorizationsService = new AuthorizationService(database)) {
|
|
8
8
|
const systemUserContext = getSystemUserContext();
|
|
9
9
|
const activeRefreshToken = await getActiveRefreshToken(database, systemUserContext, refreshToken, deviceId);
|
|
10
10
|
if (!activeRefreshToken) {
|
|
@@ -3,7 +3,6 @@ import { entityUtils } from '@loomcore/common/utils';
|
|
|
3
3
|
import { BadRequestError, ServerError } from '../../errors/index.js';
|
|
4
4
|
import { PasswordResetTokenService } from '../../services/password-reset-token.service.js';
|
|
5
5
|
import { UserService } from '../../services/user.service.js';
|
|
6
|
-
import { changePassword } from './change-password.util.js';
|
|
7
6
|
export async function resetPassword(database, email, passwordResetToken, password, organization, userService = new UserService(database), userSpec = UserSpec) {
|
|
8
7
|
const validationErrors = entityUtils.validate(userSpec, { password: password }, true, passwordValidator);
|
|
9
8
|
entityUtils.handleValidationResult(validationErrors, 'AuthService.resetPassword');
|
|
@@ -26,9 +25,12 @@ export async function resetPassword(database, email, passwordResetToken, passwor
|
|
|
26
25
|
if (!user) {
|
|
27
26
|
throw new ServerError(`Unable to retrieve user for email: ${lowerCaseEmail}`);
|
|
28
27
|
}
|
|
29
|
-
const result = await changePassword(
|
|
28
|
+
const result = await userService.changePassword(systemUserContext, user._id, password);
|
|
30
29
|
console.log(`password changed using forgot-password for email: ${lowerCaseEmail}`);
|
|
31
30
|
await passwordResetTokenService.deleteById(systemUserContext, retrievedPasswordResetToken._id);
|
|
32
31
|
console.log(`passwordResetToken deleted for email: ${lowerCaseEmail}`);
|
|
33
|
-
return
|
|
32
|
+
return {
|
|
33
|
+
success: true,
|
|
34
|
+
count: 1,
|
|
35
|
+
};
|
|
34
36
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { IFeature, IUserContext } from '@loomcore/common/models';
|
|
2
|
-
import { IDatabase } from '../databases/index.js';
|
|
3
|
-
import { MultiTenantApiService } from './multi-tenant-api.service.js';
|
|
4
|
-
export declare class FeaturesService extends MultiTenantApiService<IFeature> {
|
|
5
|
-
constructor(database: IDatabase);
|
|
6
|
-
preProcessEntity(userContext: IUserContext, entity: Partial<IFeature>, isCreate: boolean, allowId?: boolean): Promise<Partial<IFeature>>;
|
|
7
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { FeatureModelSpec } from '@loomcore/common/models';
|
|
2
|
-
import { MultiTenantApiService } from './multi-tenant-api.service.js';
|
|
3
|
-
import { assertUserHasFeature } from '../utils/index.js';
|
|
4
|
-
export class FeaturesService extends MultiTenantApiService {
|
|
5
|
-
constructor(database) {
|
|
6
|
-
super(database, 'features', 'feature', FeatureModelSpec);
|
|
7
|
-
}
|
|
8
|
-
async preProcessEntity(userContext, entity, isCreate, allowId = true) {
|
|
9
|
-
assertUserHasFeature(userContext, ['admin', 'system', 'features']);
|
|
10
|
-
return super.preProcessEntity(userContext, entity, isCreate, allowId);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { IRole, IUserContext } from '@loomcore/common/models';
|
|
2
|
-
import { IDatabase } from '../databases/index.js';
|
|
3
|
-
import { MultiTenantApiService } from './multi-tenant-api.service.js';
|
|
4
|
-
export declare class RolesService extends MultiTenantApiService<IRole> {
|
|
5
|
-
constructor(database: IDatabase);
|
|
6
|
-
preProcessEntity(userContext: IUserContext, entity: Partial<IRole>, isCreate: boolean, allowId?: boolean): Promise<Partial<IRole>>;
|
|
7
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { RoleModelSpec } from '@loomcore/common/models';
|
|
2
|
-
import { MultiTenantApiService } from './multi-tenant-api.service.js';
|
|
3
|
-
import { assertUserHasFeature } from '../utils/index.js';
|
|
4
|
-
export class RolesService extends MultiTenantApiService {
|
|
5
|
-
constructor(database) {
|
|
6
|
-
super(database, 'roles', 'role', RoleModelSpec);
|
|
7
|
-
}
|
|
8
|
-
async preProcessEntity(userContext, entity, isCreate, allowId = true) {
|
|
9
|
-
assertUserHasFeature(userContext, ['admin', 'system', 'roles']);
|
|
10
|
-
return super.preProcessEntity(userContext, entity, isCreate, allowId);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { IUserContext, IUserRole } from '@loomcore/common/models';
|
|
2
|
-
import { IDatabase } from '../databases/index.js';
|
|
3
|
-
import { MultiTenantApiService } from './multi-tenant-api.service.js';
|
|
4
|
-
export declare class UserRolesService extends MultiTenantApiService<IUserRole> {
|
|
5
|
-
constructor(database: IDatabase);
|
|
6
|
-
preProcessEntity(userContext: IUserContext, entity: Partial<IUserRole>, isCreate: boolean, allowId?: boolean): Promise<Partial<IUserRole>>;
|
|
7
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { UserRoleModelSpec } from '@loomcore/common/models';
|
|
2
|
-
import { MultiTenantApiService } from './multi-tenant-api.service.js';
|
|
3
|
-
import { assertUserHasFeature } from '../utils/index.js';
|
|
4
|
-
export class UserRolesService extends MultiTenantApiService {
|
|
5
|
-
constructor(database) {
|
|
6
|
-
super(database, 'userRoles', 'userRole', UserRoleModelSpec);
|
|
7
|
-
}
|
|
8
|
-
async preProcessEntity(userContext, entity, isCreate, allowId = true) {
|
|
9
|
-
assertUserHasFeature(userContext, ['admin', 'system', 'user-roles']);
|
|
10
|
-
return super.preProcessEntity(userContext, entity, isCreate, allowId);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { isAdmin } from './is-admin.util.js';
|
|
2
|
-
import { assertUserHasFeature } from './assert-user-has-feature.util.js';
|
|
3
|
-
export function assertAdminOrFeature(userContext, features, requireAll = false) {
|
|
4
|
-
if (isAdmin(userContext)) {
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
7
|
-
assertUserHasFeature(userContext, features, requireAll);
|
|
8
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { UnauthorizedError } from '../../errors/index.js';
|
|
2
|
-
import { isAdmin } from './is-admin.util.js';
|
|
3
|
-
export function assertOwnerOrAdmin(userContext, auditable) {
|
|
4
|
-
if (isAdmin(userContext)) {
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
7
|
-
if (userContext.user._id !== auditable._createdBy) {
|
|
8
|
-
throw new UnauthorizedError();
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { UnauthorizedError } from '../../errors/index.js';
|
|
2
|
-
export function assertUserHasFeature(userContext, features, requireAll = false) {
|
|
3
|
-
if (features.length === 0) {
|
|
4
|
-
return;
|
|
5
|
-
}
|
|
6
|
-
const missingFeatures = features.filter(feature => !userContext.features.includes(feature));
|
|
7
|
-
if (requireAll) {
|
|
8
|
-
if (missingFeatures.length > 0) {
|
|
9
|
-
throw new UnauthorizedError(missingFeatures);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
if (missingFeatures.length === features.length) {
|
|
14
|
-
throw new UnauthorizedError(missingFeatures);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { UnauthorizedError } from '../../errors/index.js';
|
|
2
|
-
import { isAdmin } from './is-admin.util.js';
|
|
3
|
-
export function assertUserOrAdmin(userContext, userId) {
|
|
4
|
-
if (isAdmin(userContext)) {
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
7
|
-
if (userContext.user._id !== userId) {
|
|
8
|
-
throw new UnauthorizedError();
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { type IUserContext } from '@loomcore/common/models';
|
|
2
|
-
import type { IDatabase } from '../../databases/models/index.js';
|
|
3
|
-
import type { UpdateResult } from '../../databases/models/update-result.js';
|
|
4
|
-
import { UserService } from '../../services/user.service.js';
|
|
5
|
-
import { AppIdType } from '@loomcore/common/types';
|
|
6
|
-
export declare function changePassword(database: IDatabase, userContext: IUserContext, userId: AppIdType, password: string, userService?: UserService): Promise<UpdateResult>;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { UserService } from '../../services/user.service.js';
|
|
2
|
-
export async function changePassword(database, userContext, userId, password, userService = new UserService(database)) {
|
|
3
|
-
const updates = {
|
|
4
|
-
password: password,
|
|
5
|
-
};
|
|
6
|
-
await userService.partialUpdateById(userContext, userId, updates, true);
|
|
7
|
-
return {
|
|
8
|
-
success: true,
|
|
9
|
-
count: 1,
|
|
10
|
-
};
|
|
11
|
-
}
|