@vnodes/auth 0.0.15 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/README.md +3 -3
  2. package/bin/version.mjs +12 -0
  3. package/dist/index.d.ts +1 -26
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +3 -27
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/auth.d.ts +2 -0
  8. package/dist/lib/auth.d.ts.map +1 -0
  9. package/dist/lib/auth.js +5 -0
  10. package/dist/lib/auth.js.map +1 -0
  11. package/package.json +19 -30
  12. package/assets/favicon.png +0 -0
  13. package/dist/auth.controller.d.ts +0 -16
  14. package/dist/auth.controller.d.ts.map +0 -1
  15. package/dist/auth.controller.js +0 -90
  16. package/dist/auth.module.d.ts +0 -3
  17. package/dist/auth.module.d.ts.map +0 -1
  18. package/dist/auth.module.js +0 -38
  19. package/dist/client/auth-client.module.d.ts +0 -3
  20. package/dist/client/auth-client.module.d.ts.map +0 -1
  21. package/dist/client/auth-client.module.js +0 -21
  22. package/dist/context/context.d.ts +0 -12
  23. package/dist/context/context.d.ts.map +0 -1
  24. package/dist/context/context.js +0 -19
  25. package/dist/dto/access-token.dto.d.ts +0 -5
  26. package/dist/dto/access-token.dto.d.ts.map +0 -1
  27. package/dist/dto/access-token.dto.js +0 -12
  28. package/dist/dto/can-activate-response-dto.d.ts +0 -5
  29. package/dist/dto/can-activate-response-dto.d.ts.map +0 -1
  30. package/dist/dto/can-activate-response-dto.js +0 -12
  31. package/dist/dto/can-activate.dto.d.ts +0 -6
  32. package/dist/dto/can-activate.dto.d.ts.map +0 -1
  33. package/dist/dto/can-activate.dto.js +0 -17
  34. package/dist/dto/email-otp-event.dto.d.ts +0 -6
  35. package/dist/dto/email-otp-event.dto.d.ts.map +0 -1
  36. package/dist/dto/email-otp-event.dto.js +0 -17
  37. package/dist/dto/forgot-password.dto.d.ts +0 -4
  38. package/dist/dto/forgot-password.dto.d.ts.map +0 -1
  39. package/dist/dto/forgot-password.dto.js +0 -9
  40. package/dist/dto/login-with-otp.dto.d.ts +0 -5
  41. package/dist/dto/login-with-otp.dto.d.ts.map +0 -1
  42. package/dist/dto/login-with-otp.dto.js +0 -14
  43. package/dist/dto/login.dto.d.ts +0 -5
  44. package/dist/dto/login.dto.d.ts.map +0 -1
  45. package/dist/dto/login.dto.js +0 -14
  46. package/dist/dto/message.dto.d.ts +0 -5
  47. package/dist/dto/message.dto.d.ts.map +0 -1
  48. package/dist/dto/message.dto.js +0 -12
  49. package/dist/dto/otp-response-dto.d.ts +0 -5
  50. package/dist/dto/otp-response-dto.d.ts.map +0 -1
  51. package/dist/dto/otp-response-dto.js +0 -12
  52. package/dist/dto/update-password-event.dto.d.ts +0 -5
  53. package/dist/dto/update-password-event.dto.d.ts.map +0 -1
  54. package/dist/dto/update-password-event.dto.js +0 -14
  55. package/dist/dto/update-password.dto.d.ts +0 -4
  56. package/dist/dto/update-password.dto.d.ts.map +0 -1
  57. package/dist/dto/update-password.dto.js +0 -9
  58. package/dist/guards/auth-client.guard.d.ts +0 -14
  59. package/dist/guards/auth-client.guard.d.ts.map +0 -1
  60. package/dist/guards/auth-client.guard.js +0 -64
  61. package/dist/guards/auth.guard.d.ts +0 -16
  62. package/dist/guards/auth.guard.d.ts.map +0 -1
  63. package/dist/guards/auth.guard.js +0 -71
  64. package/dist/providers/auth-client-options.provider.d.ts +0 -5
  65. package/dist/providers/auth-client-options.provider.d.ts.map +0 -1
  66. package/dist/providers/auth-client-options.provider.js +0 -21
  67. package/dist/services/auth-listener.service.d.ts +0 -10
  68. package/dist/services/auth-listener.service.d.ts.map +0 -1
  69. package/dist/services/auth-listener.service.js +0 -32
  70. package/dist/services/auth-user.service.d.ts +0 -28
  71. package/dist/services/auth-user.service.d.ts.map +0 -1
  72. package/dist/services/auth-user.service.js +0 -94
  73. package/dist/services/auth.service.d.ts +0 -48
  74. package/dist/services/auth.service.d.ts.map +0 -1
  75. package/dist/services/auth.service.js +0 -84
  76. package/dist/types/auth-client-options.d.ts +0 -6
  77. package/dist/types/auth-client-options.d.ts.map +0 -1
  78. package/dist/types/auth-client-options.js +0 -7
  79. package/dist/types/auth-request.d.ts +0 -8
  80. package/dist/types/auth-request.d.ts.map +0 -1
  81. package/dist/types/auth-request.js +0 -1
  82. package/dist/types/jwt-payload.d.ts +0 -6
  83. package/dist/types/jwt-payload.d.ts.map +0 -1
  84. package/dist/types/jwt-payload.js +0 -5
  85. package/dist/types/user-manager.d.ts +0 -67
  86. package/dist/types/user-manager.d.ts.map +0 -1
  87. package/dist/types/user-manager.js +0 -118
  88. package/dist/types/user.d.ts +0 -10
  89. package/dist/types/user.d.ts.map +0 -1
  90. package/dist/types/user.js +0 -17
@@ -1,12 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Prop } from '@vnodes/property';
3
- export class MessageDto {
4
- message;
5
- constructor(data) {
6
- Object.assign(this, data);
7
- }
8
- }
9
- __decorate([
10
- Prop(),
11
- __metadata("design:type", String)
12
- ], MessageDto.prototype, "message", void 0);
@@ -1,5 +0,0 @@
1
- export declare class OtpResponseDto {
2
- otp: string;
3
- constructor(data: OtpResponseDto);
4
- }
5
- //# sourceMappingURL=otp-response-dto.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"otp-response-dto.d.ts","sourceRoot":"","sources":["../../src/dto/otp-response-dto.ts"],"names":[],"mappings":"AAEA,qBAAa,cAAc;IACf,GAAG,EAAE,MAAM,CAAC;gBACR,IAAI,EAAE,cAAc;CAGnC"}
@@ -1,12 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Prop } from '@vnodes/property';
3
- export class OtpResponseDto {
4
- otp;
5
- constructor(data) {
6
- Object.assign(this, data);
7
- }
8
- }
9
- __decorate([
10
- Prop(),
11
- __metadata("design:type", String)
12
- ], OtpResponseDto.prototype, "otp", void 0);
@@ -1,5 +0,0 @@
1
- export declare class UpdatePasswordEventDto {
2
- uuid: string;
3
- password: string;
4
- }
5
- //# sourceMappingURL=update-password-event.dto.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"update-password-event.dto.d.ts","sourceRoot":"","sources":["../../src/dto/update-password-event.dto.ts"],"names":[],"mappings":"AAEA,qBAAa,sBAAsB;IACL,IAAI,EAAE,MAAM,CAAC;IACO,QAAQ,EAAE,MAAM,CAAC;CAClE"}
@@ -1,14 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Prop } from '@vnodes/property';
3
- export class UpdatePasswordEventDto {
4
- uuid;
5
- password;
6
- }
7
- __decorate([
8
- Prop({ required: true }),
9
- __metadata("design:type", String)
10
- ], UpdatePasswordEventDto.prototype, "uuid", void 0);
11
- __decorate([
12
- Prop({ required: true, format: 'password' }),
13
- __metadata("design:type", String)
14
- ], UpdatePasswordEventDto.prototype, "password", void 0);
@@ -1,4 +0,0 @@
1
- export declare class UpdatePasswordDto {
2
- password: string;
3
- }
4
- //# sourceMappingURL=update-password.dto.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"update-password.dto.d.ts","sourceRoot":"","sources":["../../src/dto/update-password.dto.ts"],"names":[],"mappings":"AAEA,qBAAa,iBAAiB;IACoB,QAAQ,EAAE,MAAM,CAAC;CAClE"}
@@ -1,9 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Prop } from '@vnodes/property';
3
- export class UpdatePasswordDto {
4
- password;
5
- }
6
- __decorate([
7
- Prop({ required: true, format: 'password' }),
8
- __metadata("design:type", String)
9
- ], UpdatePasswordDto.prototype, "password", void 0);
@@ -1,14 +0,0 @@
1
- import { CanActivate, ExecutionContext } from '@nestjs/common';
2
- import { ConfigService } from '@nestjs/config';
3
- import { Reflector } from '@nestjs/core';
4
- import { AuthClientOptions } from '../types/auth-client-options.js';
5
- import { AuthRequest } from '../types/auth-request.js';
6
- export declare class AuthClientGuard implements CanActivate {
7
- protected readonly reflector: Reflector;
8
- protected readonly config: ConfigService;
9
- protected readonly serviceOptions: AuthClientOptions;
10
- constructor(reflector: Reflector, config: ConfigService, serviceOptions: AuthClientOptions);
11
- canActivate(context: ExecutionContext): Promise<boolean>;
12
- extractToken(request: AuthRequest): string;
13
- }
14
- //# sourceMappingURL=auth-client.guard.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth-client.guard.d.ts","sourceRoot":"","sources":["../../src/guards/auth-client.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAqC,MAAM,gBAAgB,CAAC;AAClG,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEvD,qBACa,eAAgB,YAAW,WAAW;IAE3C,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,SAAS;IACvC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,aAAa;IACb,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,iBAAiB;gBAF5D,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,aAAa,EACM,cAAc,EAAE,iBAAiB;IAG7E,WAAW,CAAC,OAAO,EAAE,gBAAgB;IAwC3C,YAAY,CAAC,OAAO,EAAE,WAAW;CAYpC"}
@@ -1,64 +0,0 @@
1
- import { __decorate, __metadata, __param } from "tslib";
2
- import { Injectable, UnauthorizedException } from '@nestjs/common';
3
- import { ConfigService } from '@nestjs/config';
4
- import { Reflector } from '@nestjs/core';
5
- import { getOperationName, getPermissions, getRoles, isPublic } from '@vnodes/metadata';
6
- import { CanActivateDto } from '../dto/can-activate.dto.js';
7
- import { InjectAutoClientOptions } from '../providers/auth-client-options.provider.js';
8
- import { AuthClientOptions } from '../types/auth-client-options.js';
9
- let AuthClientGuard = class AuthClientGuard {
10
- reflector;
11
- config;
12
- serviceOptions;
13
- constructor(reflector, config, serviceOptions) {
14
- this.reflector = reflector;
15
- this.config = config;
16
- this.serviceOptions = serviceOptions;
17
- }
18
- async canActivate(context) {
19
- const req = context.switchToHttp().getRequest();
20
- if (isPublic(this.reflector, context)) {
21
- return true;
22
- }
23
- const headers = new Headers();
24
- const resourceName = getOperationName(this.reflector, context);
25
- const operationName = getOperationName(this.reflector, context);
26
- const permit = `${this.serviceOptions.appId}.${resourceName}.${operationName}`;
27
- const customPermissions = getPermissions(this.reflector, context) ?? [];
28
- const customRoles = getRoles(this.reflector, context) ?? [];
29
- const token = this.extractToken(req);
30
- headers.set('authorization', token);
31
- const policy = new CanActivateDto({
32
- requiredRoles: [...customRoles],
33
- requiredPermissions: [permit, ...customPermissions],
34
- });
35
- const bodyAsString = JSON.stringify(policy);
36
- const res = await fetch(this.serviceOptions.authServiceUrl, {
37
- method: 'POST',
38
- headers,
39
- body: bodyAsString,
40
- });
41
- if (res.status > 200 && res.status < 300) {
42
- return true;
43
- }
44
- return false;
45
- }
46
- extractToken(request) {
47
- const rawToken = request.headers.authorization;
48
- if (!rawToken) {
49
- throw new UnauthorizedException('No token');
50
- }
51
- const [type, token] = rawToken.split(' ');
52
- if (type === 'Bearer' && token)
53
- return token;
54
- throw new UnauthorizedException('Invalid token ');
55
- }
56
- };
57
- AuthClientGuard = __decorate([
58
- Injectable(),
59
- __param(2, InjectAutoClientOptions()),
60
- __metadata("design:paramtypes", [Reflector,
61
- ConfigService,
62
- AuthClientOptions])
63
- ], AuthClientGuard);
64
- export { AuthClientGuard };
@@ -1,16 +0,0 @@
1
- import { CanActivate, ExecutionContext } from '@nestjs/common';
2
- import { ConfigService } from '@nestjs/config';
3
- import { Reflector } from '@nestjs/core';
4
- import { UserManager } from 'src/types/user-manager.js';
5
- import { AuthUserService } from '../services/auth-user.service.js';
6
- import { AuthRequest } from '../types/auth-request.js';
7
- export declare class AuthGuard implements CanActivate {
8
- protected readonly config: ConfigService;
9
- protected readonly reflector: Reflector;
10
- protected readonly authUserService: AuthUserService;
11
- constructor(config: ConfigService, reflector: Reflector, authUserService: AuthUserService);
12
- canActivate(context: ExecutionContext): Promise<boolean>;
13
- protected isAutorized(context: ExecutionContext, user: UserManager): boolean;
14
- extractToken(request: AuthRequest): string;
15
- }
16
- //# sourceMappingURL=auth.guard.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth.guard.d.ts","sourceRoot":"","sources":["../../src/guards/auth.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAqC,MAAM,gBAAgB,CAAC;AAClG,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AASzC,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEvD,qBACa,SAAU,YAAW,WAAW;IAErC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,aAAa;IACxC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,SAAS;IACvC,SAAS,CAAC,QAAQ,CAAC,eAAe,EAAE,eAAe;gBAFhC,MAAM,EAAE,aAAa,EACrB,SAAS,EAAE,SAAS,EACpB,eAAe,EAAE,eAAe;IAGjD,WAAW,CAAC,OAAO,EAAE,gBAAgB;IAgB3C,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW;IAgClE,YAAY,CAAC,OAAO,EAAE,WAAW;CAYpC"}
@@ -1,71 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable, UnauthorizedException } from '@nestjs/common';
3
- import { ConfigService } from '@nestjs/config';
4
- import { Reflector } from '@nestjs/core';
5
- import { getOperationName, getPermissions, getResourceName, getRoles, isByPassAuthorization, isPublic, } from '@vnodes/metadata';
6
- import { AuthUserService } from '../services/auth-user.service.js';
7
- let AuthGuard = class AuthGuard {
8
- config;
9
- reflector;
10
- authUserService;
11
- constructor(config, reflector, authUserService) {
12
- this.config = config;
13
- this.reflector = reflector;
14
- this.authUserService = authUserService;
15
- }
16
- async canActivate(context) {
17
- if (isPublic(this.reflector, context)) {
18
- return true;
19
- }
20
- const req = context.switchToHttp().getRequest();
21
- const token = this.extractToken(req);
22
- const user = await this.authUserService.findByToken(token);
23
- req.user = user.user;
24
- if (isByPassAuthorization(this.reflector, context)) {
25
- return true;
26
- }
27
- return this.isAutorized(context, user);
28
- }
29
- isAutorized(context, user) {
30
- const appId = this.config.getOrThrow('APP_ID');
31
- const resourceName = getResourceName(this.reflector, context);
32
- const operationName = getOperationName(this.reflector, context);
33
- const customPermits = getPermissions(this.reflector, context);
34
- const customRoles = getRoles(this.reflector, context);
35
- const permit = `${appId}.${resourceName}.${operationName}`;
36
- if (user.isAdmin()) {
37
- return true;
38
- }
39
- if (customPermits && customPermits.length > 0) {
40
- if (!user.hasAllPermissions(customPermits)) {
41
- return false;
42
- }
43
- }
44
- if (customRoles && customRoles.length > 0) {
45
- if (!user.hasSomeRoles(customRoles)) {
46
- return false;
47
- }
48
- }
49
- if (!user.hasAllPermissions([permit])) {
50
- return false;
51
- }
52
- return true;
53
- }
54
- extractToken(request) {
55
- const rawToken = request.headers.authorization;
56
- if (!rawToken) {
57
- throw new UnauthorizedException('No token');
58
- }
59
- const [type, token] = rawToken.split(' ');
60
- if (type === 'Bearer' && token)
61
- return token;
62
- throw new UnauthorizedException('Invalid token ');
63
- }
64
- };
65
- AuthGuard = __decorate([
66
- Injectable(),
67
- __metadata("design:paramtypes", [ConfigService,
68
- Reflector,
69
- AuthUserService])
70
- ], AuthGuard);
71
- export { AuthGuard };
@@ -1,5 +0,0 @@
1
- import { Provider } from '@nestjs/common';
2
- export declare function getAuthClientOptionsToken(): string;
3
- export declare function provideAuthClientOptions(): Provider;
4
- export declare const InjectAutoClientOptions: () => PropertyDecorator & ParameterDecorator;
5
- //# sourceMappingURL=auth-client-options.provider.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth-client-options.provider.d.ts","sourceRoot":"","sources":["../../src/providers/auth-client-options.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAKlD,wBAAgB,yBAAyB,WAExC;AAED,wBAAgB,wBAAwB,IAAI,QAAQ,CAanD;AAED,eAAO,MAAM,uBAAuB,8CAA4C,CAAC"}
@@ -1,21 +0,0 @@
1
- import { Inject } from '@nestjs/common';
2
- import { ConfigService } from '@nestjs/config';
3
- import { Env } from '@vnodes/metadata';
4
- export function getAuthClientOptionsToken() {
5
- return 'AUTH_CLIENT_OPTIONS_TOKEN';
6
- }
7
- export function provideAuthClientOptions() {
8
- return {
9
- inject: [ConfigService],
10
- provide: getAuthClientOptionsToken(),
11
- useFactory(config) {
12
- const appId = config.getOrThrow(Env.APP_ID);
13
- const authHost = config.getOrThrow(Env.AUTH_SERVICE_URL);
14
- return {
15
- appId,
16
- authServiceUrl: `${authHost}/api/auth/can-activate`,
17
- };
18
- },
19
- };
20
- }
21
- export const InjectAutoClientOptions = () => Inject(getAuthClientOptionsToken());
@@ -1,10 +0,0 @@
1
- import { EmailOtpEventDto } from '../dto/email-otp-event.dto.js';
2
- import { UpdatePasswordEventDto } from '../dto/update-password-event.dto.js';
3
- /**
4
- * Extend an override the methods requried
5
- */
6
- export declare class AuthListenerService {
7
- protected updatePassword(body: UpdatePasswordEventDto): Promise<void>;
8
- protected emailOtp(body: EmailOtpEventDto): Promise<void>;
9
- }
10
- //# sourceMappingURL=auth-listener.service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth-listener.service.d.ts","sourceRoot":"","sources":["../../src/services/auth-listener.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAE7E;;GAEG;AACH,qBACa,mBAAmB;cAEZ,cAAc,CAAC,IAAI,EAAE,sBAAsB;cAK3C,QAAQ,CAAC,IAAI,EAAE,gBAAgB;CAGlD"}
@@ -1,32 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable, NotImplementedException } from '@nestjs/common';
3
- import { OnEvent } from '@nestjs/event-emitter';
4
- import { EmailOtpEventDto } from '../dto/email-otp-event.dto.js';
5
- import { UpdatePasswordEventDto } from '../dto/update-password-event.dto.js';
6
- /**
7
- * Extend an override the methods requried
8
- */
9
- let AuthListenerService = class AuthListenerService {
10
- async updatePassword(body) {
11
- throw new NotImplementedException(`Password update for the user with uuid: ${body.uuid} is not implemented`);
12
- }
13
- async emailOtp(body) {
14
- throw new NotImplementedException(`Email otp is not implemented: ${body.username}`);
15
- }
16
- };
17
- __decorate([
18
- OnEvent('auth.user.update.password'),
19
- __metadata("design:type", Function),
20
- __metadata("design:paramtypes", [UpdatePasswordEventDto]),
21
- __metadata("design:returntype", Promise)
22
- ], AuthListenerService.prototype, "updatePassword", null);
23
- __decorate([
24
- OnEvent('auth.user.email.otp'),
25
- __metadata("design:type", Function),
26
- __metadata("design:paramtypes", [EmailOtpEventDto]),
27
- __metadata("design:returntype", Promise)
28
- ], AuthListenerService.prototype, "emailOtp", null);
29
- AuthListenerService = __decorate([
30
- Injectable()
31
- ], AuthListenerService);
32
- export { AuthListenerService };
@@ -1,28 +0,0 @@
1
- import { JwtService } from '@nestjs/jwt';
2
- import { User } from '../types/user.js';
3
- import { UserManager } from '../types/user-manager.js';
4
- export declare class AuthUserService {
5
- protected readonly jwt: JwtService;
6
- protected readonly usernameMap: Map<string, User>;
7
- protected readonly uuidUsernameMap: Map<string, string>;
8
- protected readonly tokenUsernameMap: Map<string, string>;
9
- protected readonly usernameOtpMap: Map<string, string>;
10
- constructor(jwt: JwtService);
11
- update(user: User): void;
12
- deleteByUsername(username: string): void;
13
- load(users: User[]): void;
14
- findByUsername(username: string): UserManager;
15
- /**
16
- * Find user by token and username or throw not found exception
17
- * @param token
18
- * @returns
19
- */
20
- findByToken(token: string): Promise<UserManager>;
21
- findByUuid(uuid: string): UserManager;
22
- deleteToken(token: string): boolean;
23
- updateToken(token: string, username: string): void;
24
- createOtp(username: string): string;
25
- compareOtp(username: string, otp: string): boolean;
26
- deleteOtp(username: string): void;
27
- }
28
- //# sourceMappingURL=auth-user.service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth-user.service.d.ts","sourceRoot":"","sources":["../../src/services/auth-user.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEvD,qBACa,eAAe;IAMZ,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU;IAL9C,SAAS,CAAC,QAAQ,CAAC,WAAW,oBAA2B;IACzD,SAAS,CAAC,QAAQ,CAAC,eAAe,sBAA6B;IAC/D,SAAS,CAAC,QAAQ,CAAC,gBAAgB,sBAA6B;IAChE,SAAS,CAAC,QAAQ,CAAC,cAAc,sBAA6B;gBAE/B,GAAG,EAAE,UAAU;IAE9C,MAAM,CAAC,IAAI,EAAE,IAAI;IAIjB,gBAAgB,CAAC,QAAQ,EAAE,MAAM;IAIjC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE;IAQlB,cAAc,CAAC,QAAQ,EAAE,MAAM;IAQ/B;;;;OAIG;IACG,WAAW,CAAC,KAAK,EAAE,MAAM;IAa/B,UAAU,CAAC,IAAI,EAAE,MAAM;IAQvB,WAAW,CAAC,KAAK,EAAE,MAAM;IAOzB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAI3C,SAAS,CAAC,QAAQ,EAAE,MAAM;IAM1B,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAYxC,SAAS,CAAC,QAAQ,EAAE,MAAM;CAG7B"}
@@ -1,94 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
3
- import { JwtService } from '@nestjs/jwt';
4
- import { otp } from '@vnodes/crypto';
5
- import { UserManager } from '../types/user-manager.js';
6
- let AuthUserService = class AuthUserService {
7
- jwt;
8
- usernameMap = new Map();
9
- uuidUsernameMap = new Map();
10
- tokenUsernameMap = new Map();
11
- usernameOtpMap = new Map();
12
- constructor(jwt) {
13
- this.jwt = jwt;
14
- }
15
- update(user) {
16
- this.usernameMap.set(user.username, user);
17
- }
18
- deleteByUsername(username) {
19
- this.usernameMap.delete(username);
20
- }
21
- load(users) {
22
- this.usernameMap.clear();
23
- for (const user of users) {
24
- this.usernameMap.set(user.username, user);
25
- this.uuidUsernameMap.set(user.uuid, user.username);
26
- }
27
- }
28
- findByUsername(username) {
29
- const foundUser = this.usernameMap.get(username);
30
- if (foundUser) {
31
- return new UserManager(foundUser, this.jwt);
32
- }
33
- throw new NotFoundException(`The user ${username} not found`);
34
- }
35
- /**
36
- * Find user by token and username or throw not found exception
37
- * @param token
38
- * @returns
39
- */
40
- async findByToken(token) {
41
- const username = this.tokenUsernameMap.get(token);
42
- if (username) {
43
- return this.findByUsername(username);
44
- }
45
- else {
46
- const paylaod = await this.jwt.verifyAsync(token);
47
- const userManager = this.findByUsername(paylaod.username);
48
- userManager.verifyVersion(paylaod.version);
49
- this.tokenUsernameMap.set(token, paylaod.username);
50
- return this.findByUsername(paylaod.username);
51
- }
52
- }
53
- findByUuid(uuid) {
54
- const username = this.uuidUsernameMap.get(uuid);
55
- if (username) {
56
- return this.findByUsername(username);
57
- }
58
- throw new UnauthorizedException(`User not found by uuid`);
59
- }
60
- deleteToken(token) {
61
- if (this.tokenUsernameMap.delete(token)) {
62
- return true;
63
- }
64
- throw new NotFoundException('Token not found');
65
- }
66
- updateToken(token, username) {
67
- this.tokenUsernameMap.set(token, username);
68
- }
69
- createOtp(username) {
70
- const otpValue = otp();
71
- this.usernameOtpMap.set(username, otpValue);
72
- return otpValue;
73
- }
74
- compareOtp(username, otp) {
75
- const foundOtp = this.usernameOtpMap.get(username);
76
- if (foundOtp) {
77
- if (foundOtp === otp) {
78
- return true;
79
- }
80
- throw new UnauthorizedException(`Wrong otp`);
81
- }
82
- else {
83
- throw new UnauthorizedException('Otp not found');
84
- }
85
- }
86
- deleteOtp(username) {
87
- this.usernameOtpMap.delete(username);
88
- }
89
- };
90
- AuthUserService = __decorate([
91
- Injectable(),
92
- __metadata("design:paramtypes", [JwtService])
93
- ], AuthUserService);
94
- export { AuthUserService };
@@ -1,48 +0,0 @@
1
- import { EventEmitter2 } from '@nestjs/event-emitter';
2
- import { AccessTokenDto } from '../dto/access-token.dto.js';
3
- import { CanActivateDto } from '../dto/can-activate.dto.js';
4
- import { CanActivateResponseDto } from '../dto/can-activate-response-dto.js';
5
- import { ForgotPasswordDto } from '../dto/forgot-password.dto.js';
6
- import { LoginDto } from '../dto/login.dto.js';
7
- import { LoginWithOtpDto } from '../dto/login-with-otp.dto.js';
8
- import { MessageDto } from '../dto/message.dto.js';
9
- import { UpdatePasswordDto } from '../dto/update-password.dto.js';
10
- import { AuthUserService } from './auth-user.service.js';
11
- export declare class AuthService {
12
- protected readonly authUserService: AuthUserService;
13
- protected readonly eventEmitter: EventEmitter2;
14
- constructor(authUserService: AuthUserService, eventEmitter: EventEmitter2);
15
- /**
16
- * Login with credentials (find user by username and compare the passed with hashed password)
17
- * @param body -- {@link LoginDto}
18
- * @returns -- {@link AccessTokenDto}
19
- */
20
- login(body: LoginDto): Promise<AccessTokenDto>;
21
- /**
22
- * Login with otp code (generated and sent to the user via email or sms)
23
- * @param body -- {@link LoginWithOtpDto}
24
- * @returns -- {@link AccessTokenDto}
25
- */
26
- loginWithOtp(body: LoginWithOtpDto): Promise<AccessTokenDto>;
27
- /**
28
- * Logout from the current sesison (delete the session token from token hash map)
29
- * @param token acesss token
30
- * @returns -- {@link MessageDto}
31
- */
32
- logout(token: string): MessageDto;
33
- /**
34
- * Create a otp code and emit "email.otp" event with payload of {@link OtpResponseDto}
35
- * @param body -- {@link ForgotPasswordDto}
36
- * @returns -- {@link MessageDto}
37
- */
38
- forgotPassword(body: ForgotPasswordDto): MessageDto;
39
- /**
40
- *
41
- * @param uuid
42
- * @param body
43
- */
44
- updatePassword(uuid: string, body: UpdatePasswordDto): Promise<MessageDto>;
45
- profile(uuid: string): import("../index.js").UserManager;
46
- canActivate(username: string, body: CanActivateDto): Promise<CanActivateResponseDto>;
47
- }
48
- //# sourceMappingURL=auth.service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth.service.d.ts","sourceRoot":"","sources":["../../src/services/auth.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,qBACa,WAAW;IAEhB,SAAS,CAAC,QAAQ,CAAC,eAAe,EAAE,eAAe;IACnD,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,aAAa;gBAD3B,eAAe,EAAE,eAAe,EAChC,YAAY,EAAE,aAAa;IAGlD;;;;OAIG;IACG,KAAK,CAAC,IAAI,EAAE,QAAQ;IAQ1B;;;;OAIG;IACG,YAAY,CAAC,IAAI,EAAE,eAAe;IAQxC;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM;IAKpB;;;;OAIG;IACH,cAAc,CAAC,IAAI,EAAE,iBAAiB;IAMtC;;;;OAIG;IACG,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB;IAK1D,OAAO,CAAC,IAAI,EAAE,MAAM;IAId,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc;CAO3D"}
@@ -1,84 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable } from '@nestjs/common';
3
- import { EventEmitter2 } from '@nestjs/event-emitter';
4
- import { hash } from '@vnodes/crypto';
5
- import { AccessTokenDto } from '../dto/access-token.dto.js';
6
- import { CanActivateResponseDto } from '../dto/can-activate-response-dto.js';
7
- import { MessageDto } from '../dto/message.dto.js';
8
- import { OtpResponseDto } from '../dto/otp-response-dto.js';
9
- import { AuthUserService } from './auth-user.service.js';
10
- let AuthService = class AuthService {
11
- authUserService;
12
- eventEmitter;
13
- constructor(authUserService, eventEmitter) {
14
- this.authUserService = authUserService;
15
- this.eventEmitter = eventEmitter;
16
- }
17
- /**
18
- * Login with credentials (find user by username and compare the passed with hashed password)
19
- * @param body -- {@link LoginDto}
20
- * @returns -- {@link AccessTokenDto}
21
- */
22
- async login(body) {
23
- const user = this.authUserService.findByUsername(body.username);
24
- await user.comparePassword(body.password);
25
- const token = await user.signToken();
26
- this.authUserService.updateToken(token, user.user.username);
27
- return new AccessTokenDto({ token });
28
- }
29
- /**
30
- * Login with otp code (generated and sent to the user via email or sms)
31
- * @param body -- {@link LoginWithOtpDto}
32
- * @returns -- {@link AccessTokenDto}
33
- */
34
- async loginWithOtp(body) {
35
- this.authUserService.compareOtp(body.username, body.otp);
36
- this.authUserService.deleteOtp(body.username);
37
- const user = this.authUserService.findByUsername(body.username);
38
- const token = await user.signToken();
39
- return new AccessTokenDto({ token });
40
- }
41
- /**
42
- * Logout from the current sesison (delete the session token from token hash map)
43
- * @param token acesss token
44
- * @returns -- {@link MessageDto}
45
- */
46
- logout(token) {
47
- this.authUserService.deleteToken(token);
48
- return new MessageDto({ message: 'Bye for now' });
49
- }
50
- /**
51
- * Create a otp code and emit "email.otp" event with payload of {@link OtpResponseDto}
52
- * @param body -- {@link ForgotPasswordDto}
53
- * @returns -- {@link MessageDto}
54
- */
55
- forgotPassword(body) {
56
- const otp = this.authUserService.createOtp(body.username);
57
- this.eventEmitter.emit('auth.user.email.otp', new OtpResponseDto({ otp }));
58
- return new MessageDto({ message: 'We sent the otp to your email' });
59
- }
60
- /**
61
- *
62
- * @param uuid
63
- * @param body
64
- */
65
- async updatePassword(uuid, body) {
66
- this.eventEmitter.emit('auth.user.update.password', { uuid, password: await hash(body.password) });
67
- return new MessageDto({ message: 'Update password request is sent' });
68
- }
69
- profile(uuid) {
70
- return this.authUserService.findByUuid(uuid);
71
- }
72
- async canActivate(username, body) {
73
- const found = this.authUserService.findByUsername(username);
74
- return new CanActivateResponseDto({
75
- canActivate: !!found.canActivate(body),
76
- });
77
- }
78
- };
79
- AuthService = __decorate([
80
- Injectable(),
81
- __metadata("design:paramtypes", [AuthUserService,
82
- EventEmitter2])
83
- ], AuthService);
84
- export { AuthService };
@@ -1,6 +0,0 @@
1
- export declare class AuthClientOptions {
2
- appId: string;
3
- authServiceUrl: string;
4
- constructor(data: AuthClientOptions);
5
- }
6
- //# sourceMappingURL=auth-client-options.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth-client-options.d.ts","sourceRoot":"","sources":["../../src/types/auth-client-options.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAiB;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;gBAEX,IAAI,EAAE,iBAAiB;CAGtC"}
@@ -1,7 +0,0 @@
1
- export class AuthClientOptions {
2
- appId;
3
- authServiceUrl;
4
- constructor(data) {
5
- Object.assign(this, data);
6
- }
7
- }
@@ -1,8 +0,0 @@
1
- import { User } from './user.js';
2
- export type HeaderNames = 'authorization';
3
- export type AuthRequest = {
4
- user: User;
5
- headers: Record<HeaderNames, string>;
6
- accessToken: string;
7
- };
8
- //# sourceMappingURL=auth-request.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth-request.d.ts","sourceRoot":"","sources":["../../src/types/auth-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,MAAM,WAAW,GAAG,eAAe,CAAC;AAE1C,MAAM,MAAM,WAAW,GAAG;IACtB,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACrC,WAAW,EAAE,MAAM,CAAC;CACvB,CAAC"}
@@ -1 +0,0 @@
1
- export {};