@vnodes/auth 0.0.5 → 0.0.11

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 (67) hide show
  1. package/dist/auth.controller.d.ts +5 -0
  2. package/dist/auth.controller.d.ts.map +1 -1
  3. package/dist/auth.controller.js +41 -3
  4. package/dist/auth.module.d.ts.map +1 -1
  5. package/dist/auth.module.js +8 -4
  6. package/dist/client/auth-client.module.d.ts +3 -0
  7. package/dist/client/auth-client.module.d.ts.map +1 -0
  8. package/dist/client/auth-client.module.js +21 -0
  9. package/dist/context/context.d.ts +3 -1
  10. package/dist/context/context.d.ts.map +1 -1
  11. package/dist/context/context.js +6 -0
  12. package/dist/dto/can-activate-response-dto.d.ts +5 -0
  13. package/dist/dto/can-activate-response-dto.d.ts.map +1 -0
  14. package/dist/dto/can-activate-response-dto.js +12 -0
  15. package/dist/dto/can-activate.dto.d.ts +6 -0
  16. package/dist/dto/can-activate.dto.d.ts.map +1 -0
  17. package/dist/dto/can-activate.dto.js +17 -0
  18. package/dist/dto/email-otp-event.dto.d.ts +6 -0
  19. package/dist/dto/email-otp-event.dto.d.ts.map +1 -0
  20. package/dist/dto/email-otp-event.dto.js +17 -0
  21. package/dist/dto/forgot-password.dto.d.ts.map +1 -1
  22. package/dist/dto/forgot-password.dto.js +1 -1
  23. package/dist/dto/login-with-otp.dto.d.ts.map +1 -1
  24. package/dist/dto/login-with-otp.dto.js +2 -2
  25. package/dist/dto/login.dto.d.ts.map +1 -1
  26. package/dist/dto/login.dto.js +2 -2
  27. package/dist/dto/update-password-event.dto.d.ts +5 -0
  28. package/dist/dto/update-password-event.dto.d.ts.map +1 -0
  29. package/dist/dto/update-password-event.dto.js +14 -0
  30. package/dist/guards/auth-client.guard.d.ts +14 -0
  31. package/dist/guards/auth-client.guard.d.ts.map +1 -0
  32. package/dist/guards/auth-client.guard.js +64 -0
  33. package/dist/guards/auth.guard.d.ts +7 -3
  34. package/dist/guards/auth.guard.d.ts.map +1 -1
  35. package/dist/guards/auth.guard.js +32 -19
  36. package/dist/index.d.ts +13 -2
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +13 -2
  39. package/dist/providers/auth-client-options.provider.d.ts +5 -0
  40. package/dist/providers/auth-client-options.provider.d.ts.map +1 -0
  41. package/dist/providers/auth-client-options.provider.js +21 -0
  42. package/dist/services/auth-listener.service.d.ts +10 -0
  43. package/dist/services/auth-listener.service.d.ts.map +1 -0
  44. package/dist/services/auth-listener.service.js +32 -0
  45. package/dist/services/{user.service.d.ts → auth-user.service.d.ts} +12 -4
  46. package/dist/services/auth-user.service.d.ts.map +1 -0
  47. package/dist/services/{user.service.js → auth-user.service.js} +29 -10
  48. package/dist/services/auth.service.d.ts +14 -3
  49. package/dist/services/auth.service.d.ts.map +1 -1
  50. package/dist/services/auth.service.js +33 -13
  51. package/dist/types/auth-client-options.d.ts +6 -0
  52. package/dist/types/auth-client-options.d.ts.map +1 -0
  53. package/dist/types/auth-client-options.js +7 -0
  54. package/dist/types/auth-request.d.ts +1 -1
  55. package/dist/types/auth-request.d.ts.map +1 -1
  56. package/dist/types/jwt-payload.d.ts +6 -0
  57. package/dist/types/jwt-payload.d.ts.map +1 -0
  58. package/dist/types/jwt-payload.js +5 -0
  59. package/dist/{services → types}/user-manager.d.ts +12 -16
  60. package/dist/types/user-manager.d.ts.map +1 -0
  61. package/dist/{services → types}/user-manager.js +25 -29
  62. package/dist/types/user.d.ts +10 -0
  63. package/dist/types/user.d.ts.map +1 -0
  64. package/dist/types/user.js +17 -0
  65. package/package.json +4 -4
  66. package/dist/services/user-manager.d.ts.map +0 -1
  67. package/dist/services/user.service.d.ts.map +0 -1
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,21 @@
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());
@@ -0,0 +1,10 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,32 @@
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,8 +1,10 @@
1
1
  import { JwtService } from '@nestjs/jwt';
2
- import { User, UserManager } from './user-manager.js';
3
- export declare class UserService {
2
+ import { User } from '../types/user.js';
3
+ import { UserManager } from '../types/user-manager.js';
4
+ export declare class AuthUserService {
4
5
  protected readonly jwt: JwtService;
5
6
  protected readonly usernameMap: Map<string, User>;
7
+ protected readonly uuidUsernameMap: Map<string, string>;
6
8
  protected readonly tokenUsernameMap: Map<string, string>;
7
9
  protected readonly usernameOtpMap: Map<string, string>;
8
10
  constructor(jwt: JwtService);
@@ -10,11 +12,17 @@ export declare class UserService {
10
12
  deleteByUsername(username: string): void;
11
13
  load(users: User[]): void;
12
14
  findByUsername(username: string): UserManager;
13
- findByToken(token: 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;
14
22
  deleteToken(token: string): boolean;
15
23
  updateToken(token: string, username: string): void;
16
24
  createOtp(username: string): string;
17
25
  compareOtp(username: string, otp: string): boolean;
18
26
  deleteOtp(username: string): void;
19
27
  }
20
- //# sourceMappingURL=user.service.d.ts.map
28
+ //# sourceMappingURL=auth-user.service.d.ts.map
@@ -0,0 +1 @@
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,11 +1,12 @@
1
- import { __decorate, __metadata, __param } from "tslib";
2
- import { Inject, Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
3
3
  import { JwtService } from '@nestjs/jwt';
4
4
  import { otp } from '@vnodes/crypto';
5
- import { UserManager } from './user-manager.js';
6
- let UserService = class UserService {
5
+ import { UserManager } from '../types/user-manager.js';
6
+ let AuthUserService = class AuthUserService {
7
7
  jwt;
8
8
  usernameMap = new Map();
9
+ uuidUsernameMap = new Map();
9
10
  tokenUsernameMap = new Map();
10
11
  usernameOtpMap = new Map();
11
12
  constructor(jwt) {
@@ -21,6 +22,7 @@ let UserService = class UserService {
21
22
  this.usernameMap.clear();
22
23
  for (const user of users) {
23
24
  this.usernameMap.set(user.username, user);
25
+ this.uuidUsernameMap.set(user.uuid, user.username);
24
26
  }
25
27
  }
26
28
  findByUsername(username) {
@@ -30,12 +32,30 @@ let UserService = class UserService {
30
32
  }
31
33
  throw new NotFoundException(`The user ${username} not found`);
32
34
  }
33
- findByToken(token) {
35
+ /**
36
+ * Find user by token and username or throw not found exception
37
+ * @param token
38
+ * @returns
39
+ */
40
+ async findByToken(token) {
34
41
  const username = this.tokenUsernameMap.get(token);
35
42
  if (username) {
36
43
  return this.findByUsername(username);
37
44
  }
38
- throw new UnauthorizedException(`User not found by token`);
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`);
39
59
  }
40
60
  deleteToken(token) {
41
61
  if (this.tokenUsernameMap.delete(token)) {
@@ -67,9 +87,8 @@ let UserService = class UserService {
67
87
  this.usernameOtpMap.delete(username);
68
88
  }
69
89
  };
70
- UserService = __decorate([
90
+ AuthUserService = __decorate([
71
91
  Injectable(),
72
- __param(0, Inject(JwtService)),
73
92
  __metadata("design:paramtypes", [JwtService])
74
- ], UserService);
75
- export { UserService };
93
+ ], AuthUserService);
94
+ export { AuthUserService };
@@ -1,14 +1,17 @@
1
1
  import { EventEmitter2 } from '@nestjs/event-emitter';
2
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';
3
5
  import { ForgotPasswordDto } from '../dto/forgot-password.dto.js';
4
6
  import { LoginDto } from '../dto/login.dto.js';
5
7
  import { LoginWithOtpDto } from '../dto/login-with-otp.dto.js';
6
8
  import { MessageDto } from '../dto/message.dto.js';
7
- import { UserService } from './user.service.js';
9
+ import { UpdatePasswordDto } from '../dto/update-password.dto.js';
10
+ import { AuthUserService } from './auth-user.service.js';
8
11
  export declare class AuthService {
9
- protected readonly userService: UserService;
12
+ protected readonly authUserService: AuthUserService;
10
13
  protected readonly eventEmitter: EventEmitter2;
11
- constructor(userService: UserService, eventEmitter: EventEmitter2);
14
+ constructor(authUserService: AuthUserService, eventEmitter: EventEmitter2);
12
15
  /**
13
16
  * Login with credentials (find user by username and compare the passed with hashed password)
14
17
  * @param body -- {@link LoginDto}
@@ -33,5 +36,13 @@ export declare class AuthService {
33
36
  * @returns -- {@link MessageDto}
34
37
  */
35
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>;
36
47
  }
37
48
  //# sourceMappingURL=auth.service.d.ts.map
@@ -1 +1 @@
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;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,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,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,qBACa,WAAW;IAEhB,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,WAAW;IAC3C,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,aAAa;gBAD3B,WAAW,EAAE,WAAW,EACxB,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;CAKzC"}
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,15 +1,17 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
2
  import { Injectable } from '@nestjs/common';
3
3
  import { EventEmitter2 } from '@nestjs/event-emitter';
4
+ import { hash } from '@vnodes/crypto';
4
5
  import { AccessTokenDto } from '../dto/access-token.dto.js';
6
+ import { CanActivateResponseDto } from '../dto/can-activate-response-dto.js';
5
7
  import { MessageDto } from '../dto/message.dto.js';
6
8
  import { OtpResponseDto } from '../dto/otp-response-dto.js';
7
- import { UserService } from './user.service.js';
9
+ import { AuthUserService } from './auth-user.service.js';
8
10
  let AuthService = class AuthService {
9
- userService;
11
+ authUserService;
10
12
  eventEmitter;
11
- constructor(userService, eventEmitter) {
12
- this.userService = userService;
13
+ constructor(authUserService, eventEmitter) {
14
+ this.authUserService = authUserService;
13
15
  this.eventEmitter = eventEmitter;
14
16
  }
15
17
  /**
@@ -18,10 +20,10 @@ let AuthService = class AuthService {
18
20
  * @returns -- {@link AccessTokenDto}
19
21
  */
20
22
  async login(body) {
21
- const user = this.userService.findByUsername(body.username);
23
+ const user = this.authUserService.findByUsername(body.username);
22
24
  await user.comparePassword(body.password);
23
25
  const token = await user.signToken();
24
- this.userService.updateToken(token, user.user.username);
26
+ this.authUserService.updateToken(token, user.user.username);
25
27
  return new AccessTokenDto({ token });
26
28
  }
27
29
  /**
@@ -30,9 +32,9 @@ let AuthService = class AuthService {
30
32
  * @returns -- {@link AccessTokenDto}
31
33
  */
32
34
  async loginWithOtp(body) {
33
- this.userService.compareOtp(body.username, body.otp);
34
- this.userService.deleteOtp(body.username);
35
- const user = this.userService.findByUsername(body.username);
35
+ this.authUserService.compareOtp(body.username, body.otp);
36
+ this.authUserService.deleteOtp(body.username);
37
+ const user = this.authUserService.findByUsername(body.username);
36
38
  const token = await user.signToken();
37
39
  return new AccessTokenDto({ token });
38
40
  }
@@ -42,7 +44,7 @@ let AuthService = class AuthService {
42
44
  * @returns -- {@link MessageDto}
43
45
  */
44
46
  logout(token) {
45
- this.userService.deleteToken(token);
47
+ this.authUserService.deleteToken(token);
46
48
  return new MessageDto({ message: 'Bye for now' });
47
49
  }
48
50
  /**
@@ -51,14 +53,32 @@ let AuthService = class AuthService {
51
53
  * @returns -- {@link MessageDto}
52
54
  */
53
55
  forgotPassword(body) {
54
- const otp = this.userService.createOtp(body.username);
55
- this.eventEmitter.emit('email.otp', new OtpResponseDto({ otp }));
56
+ const otp = this.authUserService.createOtp(body.username);
57
+ this.eventEmitter.emit('auth.user.email.otp', new OtpResponseDto({ otp }));
56
58
  return new MessageDto({ message: 'We sent the otp to your email' });
57
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
+ }
58
78
  };
59
79
  AuthService = __decorate([
60
80
  Injectable(),
61
- __metadata("design:paramtypes", [UserService,
81
+ __metadata("design:paramtypes", [AuthUserService,
62
82
  EventEmitter2])
63
83
  ], AuthService);
64
84
  export { AuthService };
@@ -0,0 +1,6 @@
1
+ export declare class AuthClientOptions {
2
+ appId: string;
3
+ authServiceUrl: string;
4
+ constructor(data: AuthClientOptions);
5
+ }
6
+ //# sourceMappingURL=auth-client-options.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,7 @@
1
+ export class AuthClientOptions {
2
+ appId;
3
+ authServiceUrl;
4
+ constructor(data) {
5
+ Object.assign(this, data);
6
+ }
7
+ }
@@ -1,4 +1,4 @@
1
- import { User } from '../services/user-manager.js';
1
+ import { User } from './user.js';
2
2
  export type HeaderNames = 'authorization';
3
3
  export type AuthRequest = {
4
4
  user: User;
@@ -1 +1 @@
1
- {"version":3,"file":"auth-request.d.ts","sourceRoot":"","sources":["../../src/types/auth-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AACnD,MAAM,MAAM,WAAW,GAAG,eAAe,CAAC;AAC1C,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
+ {"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"}
@@ -0,0 +1,6 @@
1
+ export declare class JwtPayload {
2
+ sub: string;
3
+ username: string;
4
+ version: string;
5
+ }
6
+ //# sourceMappingURL=jwt-payload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-payload.d.ts","sourceRoot":"","sources":["../../src/types/jwt-payload.ts"],"names":[],"mappings":"AAAA,qBAAa,UAAU;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACnB"}
@@ -0,0 +1,5 @@
1
+ export class JwtPayload {
2
+ sub;
3
+ username;
4
+ version;
5
+ }
@@ -1,18 +1,7 @@
1
1
  import { JwtService } from '@nestjs/jwt';
2
- export declare class JwtPayload {
3
- sub: number;
4
- username: string;
5
- version: string;
6
- }
7
- export declare class User {
8
- id: number;
9
- version: string;
10
- username: string;
11
- password: string;
12
- permissions?: string[];
13
- roles?: string[];
14
- constructor(user: User);
15
- }
2
+ import { CanActivateDto } from 'src/dto/can-activate.dto.js';
3
+ import { JwtPayload } from './jwt-payload.js';
4
+ import { User } from './user.js';
16
5
  export declare class UserManager {
17
6
  protected readonly userData: User;
18
7
  protected readonly jwt: JwtService;
@@ -40,11 +29,12 @@ export declare class UserManager {
40
29
  /**
41
30
  * Check the user has all {@link requiredPermissions} or throw {@link ForbiddenException}
42
31
  */
43
- hasPermissions(requiredPermissions: string[]): boolean;
32
+ hasAllPermissions(requiredPermissions: string[]): boolean;
44
33
  /**
45
34
  * Check the user has one of the {@link requiredRoles} or throw {@link ForbiddenException}
46
35
  */
47
- hasRoles(requiredRoles: string[]): boolean;
36
+ hasSomeRoles(requiredRoles: string[]): boolean;
37
+ canActivate(body: CanActivateDto): boolean | undefined;
48
38
  /**
49
39
  * Create the jwt payload object {@link JwtPayload}
50
40
  * @returns -- {@link JwtPayload}
@@ -67,5 +57,11 @@ export declare class UserManager {
67
57
  * @returns string or throw {@link UnauthorizedException} that indicated invalid or old versioned token
68
58
  */
69
59
  verifyToken(token: string): Promise<JwtPayload>;
60
+ /**
61
+ * Check the jwt version matches with the provided token's version or thow Invalid Jwt version error
62
+ * @param version
63
+ * @returns
64
+ */
65
+ verifyVersion(version: string): boolean;
70
66
  }
71
67
  //# sourceMappingURL=user-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-manager.d.ts","sourceRoot":"","sources":["../../src/types/user-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,qBAAa,WAAW;IAEhB,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,IAAI;IACjC,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU;gBADf,QAAQ,EAAE,IAAI,EACd,GAAG,EAAE,UAAU;IAGtC;;OAEG;IACH,IAAI,IAAI,IAAI,IAAI,CAEf;IAED;;OAEG;IACH,IAAI,WAAW,gBAEd;IAED;;OAEG;IACH,IAAI,KAAK,gBAER;IAED;;OAEG;IACH,IAAI,OAAO,WAEV;IAED;;OAEG;IACH,OAAO;IAIP;;OAEG;IACH,iBAAiB,CAAC,mBAAmB,EAAE,MAAM,EAAE;IAO/C;;OAEG;IACH,YAAY,CAAC,aAAa,EAAE,MAAM,EAAE;IAOpC,WAAW,CAAC,IAAI,EAAE,cAAc;IAYhC;;;OAGG;IACH,YAAY,IAAI,UAAU;IAQ1B;;;;OAIG;IACG,eAAe,CAAC,QAAQ,EAAE,MAAM;IAOtC;;;OAGG;IACG,SAAS;IAKf;;;;OAIG;IACG,WAAW,CAAC,KAAK,EAAE,MAAM;IAM/B;;;;OAIG;IACH,aAAa,CAAC,OAAO,EAAE,MAAM;CAMhC"}
@@ -1,27 +1,6 @@
1
1
  import { ForbiddenException, UnauthorizedException } from '@nestjs/common';
2
2
  import { compare } from '@vnodes/crypto';
3
- export class JwtPayload {
4
- sub;
5
- username;
6
- version;
7
- }
8
- export class User {
9
- id;
10
- version;
11
- username;
12
- password;
13
- permissions;
14
- roles;
15
- constructor(user) {
16
- Object.assign(this, user);
17
- if (user.permissions && user.permissions?.length > 0) {
18
- this.permissions = user.permissions;
19
- }
20
- if (user.roles && user.roles?.length > 0) {
21
- this.roles = user.roles;
22
- }
23
- }
24
- }
3
+ import { User } from './user.js';
25
4
  export class UserManager {
26
5
  userData;
27
6
  jwt;
@@ -57,12 +36,12 @@ export class UserManager {
57
36
  * Check the user has the "admin" role
58
37
  */
59
38
  isAdmin() {
60
- return this.roles.has('admin');
39
+ return this.roles.has('Admin');
61
40
  }
62
41
  /**
63
42
  * Check the user has all {@link requiredPermissions} or throw {@link ForbiddenException}
64
43
  */
65
- hasPermissions(requiredPermissions) {
44
+ hasAllPermissions(requiredPermissions) {
66
45
  if (requiredPermissions.every((permission) => this.permisisons.has(permission))) {
67
46
  return true;
68
47
  }
@@ -71,19 +50,27 @@ export class UserManager {
71
50
  /**
72
51
  * Check the user has one of the {@link requiredRoles} or throw {@link ForbiddenException}
73
52
  */
74
- hasRoles(requiredRoles) {
53
+ hasSomeRoles(requiredRoles) {
75
54
  if (requiredRoles.some((role) => this.roles.has(role))) {
76
55
  return true;
77
56
  }
78
57
  throw new ForbiddenException('Insufficient role');
79
58
  }
59
+ canActivate(body) {
60
+ const { requiredPermissions, requiredRoles } = body;
61
+ if (this.isAdmin()) {
62
+ return true;
63
+ }
64
+ return ((requiredRoles && this.hasSomeRoles(requiredRoles)) ||
65
+ (requiredPermissions && this.hasAllPermissions(requiredPermissions)));
66
+ }
80
67
  /**
81
68
  * Create the jwt payload object {@link JwtPayload}
82
69
  * @returns -- {@link JwtPayload}
83
70
  */
84
71
  toJwtPayload() {
85
72
  return {
86
- sub: this.userData.id,
73
+ sub: this.userData.uuid,
87
74
  username: this.userData.username,
88
75
  version: this.userData.version,
89
76
  };
@@ -114,9 +101,18 @@ export class UserManager {
114
101
  */
115
102
  async verifyToken(token) {
116
103
  const jwtPayload = await this.jwt.verifyAsync(token);
117
- if (jwtPayload.version === this.version) {
118
- return jwtPayload;
104
+ this.verifyVersion(jwtPayload.version);
105
+ return jwtPayload;
106
+ }
107
+ /**
108
+ * Check the jwt version matches with the provided token's version or thow Invalid Jwt version error
109
+ * @param version
110
+ * @returns
111
+ */
112
+ verifyVersion(version) {
113
+ if (this.version !== version) {
114
+ throw new UnauthorizedException(`Invalid jwt version`);
119
115
  }
120
- throw new UnauthorizedException(`Invalid jwt version`);
116
+ return true;
121
117
  }
122
118
  }
@@ -0,0 +1,10 @@
1
+ export declare class User {
2
+ uuid: string;
3
+ version: string;
4
+ username: string;
5
+ password: string;
6
+ permissions?: string[];
7
+ roles?: string[];
8
+ constructor(user: User);
9
+ }
10
+ //# sourceMappingURL=user.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../src/types/user.ts"],"names":[],"mappings":"AAAA,qBAAa,IAAI;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;gBAEL,IAAI,EAAE,IAAI;CAWzB"}
@@ -0,0 +1,17 @@
1
+ export class User {
2
+ uuid;
3
+ version;
4
+ username;
5
+ password;
6
+ permissions;
7
+ roles;
8
+ constructor(user) {
9
+ Object.assign(this, user);
10
+ if (user.permissions && user.permissions?.length > 0) {
11
+ this.permissions = user.permissions;
12
+ }
13
+ if (user.roles && user.roles?.length > 0) {
14
+ this.roles = user.roles;
15
+ }
16
+ }
17
+ }
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "url": "https://cash.app/$puqlib"
20
20
  }
21
21
  ],
22
- "version": "0.0.5",
22
+ "version": "0.0.11",
23
23
  "type": "module",
24
24
  "main": "./dist/index.js",
25
25
  "module": "./dist/index.js",
@@ -61,8 +61,8 @@
61
61
  "@nestjs/jwt": "^11.0.2",
62
62
  "@nestjs/swagger": "^11.2.6",
63
63
  "@nestjs/throttler": "^6.5.0",
64
- "@vnodes/crypto": "0.0.5",
65
- "@vnodes/metadata": "0.0.5",
66
- "@vnodes/property": "0.0.5"
64
+ "@vnodes/crypto": "0.0.11",
65
+ "@vnodes/metadata": "0.0.11",
66
+ "@vnodes/property": "0.0.11"
67
67
  }
68
68
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"user-manager.d.ts","sourceRoot":"","sources":["../../src/services/user-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,qBAAa,UAAU;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,IAAI;IACb,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;gBAEL,IAAI,EAAE,IAAI;CAWzB;AAED,qBAAa,WAAW;IAEhB,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,IAAI;IACjC,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU;gBADf,QAAQ,EAAE,IAAI,EACd,GAAG,EAAE,UAAU;IAGtC;;OAEG;IACH,IAAI,IAAI,IAAI,IAAI,CAEf;IAED;;OAEG;IACH,IAAI,WAAW,gBAEd;IAED;;OAEG;IACH,IAAI,KAAK,gBAER;IAED;;OAEG;IACH,IAAI,OAAO,WAEV;IAED;;OAEG;IACH,OAAO;IAIP;;OAEG;IACH,cAAc,CAAC,mBAAmB,EAAE,MAAM,EAAE;IAO5C;;OAEG;IACH,QAAQ,CAAC,aAAa,EAAE,MAAM,EAAE;IAOhC;;;OAGG;IACH,YAAY,IAAI,UAAU;IAQ1B;;;;OAIG;IACG,eAAe,CAAC,QAAQ,EAAE,MAAM;IAOtC;;;OAGG;IACG,SAAS;IAKf;;;;OAIG;IACG,WAAW,CAAC,KAAK,EAAE,MAAM;CASlC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"user.service.d.ts","sourceRoot":"","sources":["../../src/services/user.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEtD,qBACa,WAAW;IAKY,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU;IAJlE,SAAS,CAAC,QAAQ,CAAC,WAAW,oBAA2B;IACzD,SAAS,CAAC,QAAQ,CAAC,gBAAgB,sBAA6B;IAChE,SAAS,CAAC,QAAQ,CAAC,cAAc,sBAA6B;gBAEX,GAAG,EAAE,UAAU;IAElE,MAAM,CAAC,IAAI,EAAE,IAAI;IAIjB,gBAAgB,CAAC,QAAQ,EAAE,MAAM;IAIjC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE;IAOlB,cAAc,CAAC,QAAQ,EAAE,MAAM;IAQ/B,WAAW,CAAC,KAAK,EAAE,MAAM;IASzB,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"}