@breadstone/archipel-platform-authentication 0.0.1

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 (128) hide show
  1. package/README.md +47 -0
  2. package/package.json +67 -0
  3. package/src/AuthModule.d.ts +57 -0
  4. package/src/AuthModule.js +158 -0
  5. package/src/contracts/AuthSubjectPort.d.ts +33 -0
  6. package/src/contracts/AuthSubjectPort.js +15 -0
  7. package/src/contracts/IAuthSubject.d.ts +40 -0
  8. package/src/contracts/IAuthSubject.js +3 -0
  9. package/src/contracts/IMfaSubject.d.ts +48 -0
  10. package/src/contracts/IMfaSubject.js +3 -0
  11. package/src/contracts/IMfaSubjectUpdate.d.ts +16 -0
  12. package/src/contracts/IMfaSubjectUpdate.js +3 -0
  13. package/src/contracts/ISessionRecord.d.ts +22 -0
  14. package/src/contracts/ISessionRecord.js +3 -0
  15. package/src/contracts/ISocialProfile.d.ts +23 -0
  16. package/src/contracts/ISocialProfile.js +3 -0
  17. package/src/contracts/IStoreSessionArgs.d.ts +14 -0
  18. package/src/contracts/IStoreSessionArgs.js +4 -0
  19. package/src/contracts/IVerifiableSubject.d.ts +28 -0
  20. package/src/contracts/IVerifiableSubject.js +3 -0
  21. package/src/contracts/MfaSubjectPort.d.ts +26 -0
  22. package/src/contracts/MfaSubjectPort.js +15 -0
  23. package/src/contracts/SessionPersistencePort.d.ts +58 -0
  24. package/src/contracts/SessionPersistencePort.js +15 -0
  25. package/src/contracts/SocialAuthPort.d.ts +19 -0
  26. package/src/contracts/SocialAuthPort.js +15 -0
  27. package/src/contracts/TokenEnricherPort.d.ts +17 -0
  28. package/src/contracts/TokenEnricherPort.js +15 -0
  29. package/src/contracts/VerificationSubjectPort.d.ts +40 -0
  30. package/src/contracts/VerificationSubjectPort.js +15 -0
  31. package/src/contracts/index.d.ts +13 -0
  32. package/src/contracts/index.js +16 -0
  33. package/src/decorators/RolesDecorator.d.ts +12 -0
  34. package/src/decorators/RolesDecorator.js +23 -0
  35. package/src/decorators/TokenDecorator.d.ts +6 -0
  36. package/src/decorators/TokenDecorator.js +20 -0
  37. package/src/decorators/UserDecorator.d.ts +6 -0
  38. package/src/decorators/UserDecorator.js +16 -0
  39. package/src/decorators/index.d.ts +3 -0
  40. package/src/decorators/index.js +7 -0
  41. package/src/env.d.ts +57 -0
  42. package/src/env.js +179 -0
  43. package/src/guards/AnonymousAuthGuard.d.ts +9 -0
  44. package/src/guards/AnonymousAuthGuard.js +20 -0
  45. package/src/guards/GithubAuthGuard.d.ts +9 -0
  46. package/src/guards/GithubAuthGuard.js +20 -0
  47. package/src/guards/JwtAuthGuard.d.ts +9 -0
  48. package/src/guards/JwtAuthGuard.js +20 -0
  49. package/src/guards/LocalAuthGuard.d.ts +9 -0
  50. package/src/guards/LocalAuthGuard.js +20 -0
  51. package/src/guards/RolesGuard.d.ts +16 -0
  52. package/src/guards/RolesGuard.js +43 -0
  53. package/src/guards/SocialAuthGuard.d.ts +9 -0
  54. package/src/guards/SocialAuthGuard.js +35 -0
  55. package/src/guards/_RefreshTokenAuthGuard.d.ts +0 -0
  56. package/src/guards/_RefreshTokenAuthGuard.js +2 -0
  57. package/src/guards/_TwoFactorAuthGuard.d.ts +0 -0
  58. package/src/guards/_TwoFactorAuthGuard.js +2 -0
  59. package/src/guards/index.d.ts +6 -0
  60. package/src/guards/index.js +12 -0
  61. package/src/index.d.ts +9 -0
  62. package/src/index.js +13 -0
  63. package/src/mappers/SessionMappingProfile.d.ts +42 -0
  64. package/src/mappers/SessionMappingProfile.js +68 -0
  65. package/src/middlewares/LastActiveMiddleware.d.ts +29 -0
  66. package/src/middlewares/LastActiveMiddleware.js +55 -0
  67. package/src/middlewares/index.d.ts +1 -0
  68. package/src/middlewares/index.js +5 -0
  69. package/src/models/IMfaChallengePayload.d.ts +31 -0
  70. package/src/models/IMfaChallengePayload.js +3 -0
  71. package/src/models/IMfaConfirmation.d.ts +11 -0
  72. package/src/models/IMfaConfirmation.js +3 -0
  73. package/src/models/IMfaSetup.d.ts +23 -0
  74. package/src/models/IMfaSetup.js +3 -0
  75. package/src/models/ISignInResult.d.ts +31 -0
  76. package/src/models/ISignInResult.js +3 -0
  77. package/src/models/JwtPayloadBase.d.ts +44 -0
  78. package/src/models/JwtPayloadBase.js +55 -0
  79. package/src/models/index.d.ts +6 -0
  80. package/src/models/index.js +10 -0
  81. package/src/models/responses/SessionResponse.d.ts +20 -0
  82. package/src/models/responses/SessionResponse.js +164 -0
  83. package/src/models/responses/index.d.ts +1 -0
  84. package/src/models/responses/index.js +5 -0
  85. package/src/serializer/SessionSerializer.d.ts +5 -0
  86. package/src/serializer/SessionSerializer.js +23 -0
  87. package/src/services/AuthTokenService.d.ts +30 -0
  88. package/src/services/AuthTokenService.js +58 -0
  89. package/src/services/ChallengeService.d.ts +41 -0
  90. package/src/services/ChallengeService.js +89 -0
  91. package/src/services/MfaService.d.ts +60 -0
  92. package/src/services/MfaService.js +251 -0
  93. package/src/services/SessionService.d.ts +66 -0
  94. package/src/services/SessionService.js +108 -0
  95. package/src/services/VerificationService.d.ts +56 -0
  96. package/src/services/VerificationService.js +124 -0
  97. package/src/services/index.d.ts +5 -0
  98. package/src/services/index.js +9 -0
  99. package/src/services/mfa/IMfaChannel.d.ts +40 -0
  100. package/src/services/mfa/IMfaChannel.js +4 -0
  101. package/src/services/mfa/MfaEncryptionService.d.ts +23 -0
  102. package/src/services/mfa/MfaEncryptionService.js +59 -0
  103. package/src/services/mfa/MfaState.d.ts +38 -0
  104. package/src/services/mfa/MfaState.js +23 -0
  105. package/src/services/mfa/MfaTokens.d.ts +4 -0
  106. package/src/services/mfa/MfaTokens.js +8 -0
  107. package/src/services/mfa/channels/EmailOtpMfaChannel.d.ts +21 -0
  108. package/src/services/mfa/channels/EmailOtpMfaChannel.js +152 -0
  109. package/src/services/mfa/channels/PushMfaChannel.d.ts +25 -0
  110. package/src/services/mfa/channels/PushMfaChannel.js +150 -0
  111. package/src/services/mfa/channels/SmsMfaChannel.d.ts +21 -0
  112. package/src/services/mfa/channels/SmsMfaChannel.js +158 -0
  113. package/src/services/mfa/channels/TotpMfaChannel.d.ts +19 -0
  114. package/src/services/mfa/channels/TotpMfaChannel.js +100 -0
  115. package/src/strategies/AnonymousStrategy.d.ts +32 -0
  116. package/src/strategies/AnonymousStrategy.js +53 -0
  117. package/src/strategies/AppleStrategy.d.ts +38 -0
  118. package/src/strategies/AppleStrategy.js +79 -0
  119. package/src/strategies/GithubStrategy.d.ts +24 -0
  120. package/src/strategies/GithubStrategy.js +65 -0
  121. package/src/strategies/GoogleStrategy.d.ts +34 -0
  122. package/src/strategies/GoogleStrategy.js +74 -0
  123. package/src/strategies/JwtStrategy.d.ts +31 -0
  124. package/src/strategies/JwtStrategy.js +55 -0
  125. package/src/strategies/LocalStrategy.d.ts +31 -0
  126. package/src/strategies/LocalStrategy.js +56 -0
  127. package/src/strategies/MicrosoftStrategy.d.ts +35 -0
  128. package/src/strategies/MicrosoftStrategy.js +75 -0
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ // #region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.GoogleStrategy = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const archipel_platform_core_1 = require("@breadstone/archipel-platform-core");
7
+ const common_1 = require("@nestjs/common");
8
+ const passport_1 = require("@nestjs/passport");
9
+ const passport_google_oauth20_1 = require("passport-google-oauth20");
10
+ const SocialAuthPort_1 = require("../contracts/SocialAuthPort");
11
+ // #endregion
12
+ /**
13
+ * The `GoogleStrategy` integrates Google OAuth authentication.
14
+ *
15
+ * @public
16
+ */
17
+ let GoogleStrategy = class GoogleStrategy extends (0, passport_1.PassportStrategy)(passport_google_oauth20_1.Strategy, 'google') {
18
+ // #endregion
19
+ // #region Ctor
20
+ /**
21
+ * Initializes a new instance of the `GoogleStrategy` class.
22
+ *
23
+ * @public
24
+ * @param socialAuth Social auth port instance.
25
+ * @param configService Configuration service instance.
26
+ */
27
+ constructor(socialAuth, configService) {
28
+ super({
29
+ clientID: configService.get('AUTH_GOOGLE_CLIENT_ID'),
30
+ clientSecret: configService.get('AUTH_GOOGLE_CLIENT_SECRET'),
31
+ callbackURL: configService.tryGet('AUTH_GOOGLE_CALLBACK_URL', '/auth/google/callback'),
32
+ scope: ['email', 'profile'],
33
+ state: true,
34
+ });
35
+ this._socialAuth = socialAuth;
36
+ }
37
+ // #endregion
38
+ // #region Methods
39
+ /**
40
+ * Validates the Google authentication payload.
41
+ *
42
+ * @public
43
+ * @param accessToken OAuth access token.
44
+ * @param refreshToken OAuth refresh token.
45
+ * @param profile Google profile data.
46
+ * @returns The authenticated user.
47
+ */
48
+ async validate(accessToken, refreshToken, profile) {
49
+ void accessToken;
50
+ void refreshToken;
51
+ const email = profile.emails?.[0]?.value;
52
+ if (!email) {
53
+ throw new common_1.UnauthorizedException('No email provided from Google');
54
+ }
55
+ const displayName = profile.displayName ?? profile.name?.givenName ?? profile.name?.familyName;
56
+ if (!displayName) {
57
+ throw new common_1.UnauthorizedException('No display name provided from Google');
58
+ }
59
+ const userName = profile.username ?? email.split('@')[0];
60
+ const user = await this._socialAuth.findOrCreateByProfile({
61
+ provider: 'google',
62
+ email: email,
63
+ userName: userName,
64
+ displayName: displayName,
65
+ });
66
+ return user;
67
+ }
68
+ };
69
+ exports.GoogleStrategy = GoogleStrategy;
70
+ exports.GoogleStrategy = GoogleStrategy = tslib_1.__decorate([
71
+ (0, common_1.Injectable)(),
72
+ tslib_1.__metadata("design:paramtypes", [SocialAuthPort_1.SocialAuthPort, archipel_platform_core_1.ConfigService])
73
+ ], GoogleStrategy);
74
+ //# sourceMappingURL=GoogleStrategy.js.map
@@ -0,0 +1,31 @@
1
+ import { ConfigService } from '@breadstone/archipel-platform-core';
2
+ import { Strategy } from 'passport-jwt';
3
+ import { AuthSubjectPort } from '../contracts/AuthSubjectPort';
4
+ import type { IAuthSubject } from '../contracts/IAuthSubject';
5
+ import { JwtPayloadBase } from '../models/JwtPayloadBase';
6
+ declare const JwtStrategy_base: new (...args: [opt: import("passport-jwt").StrategyOptionsWithRequest] | [opt: import("passport-jwt").StrategyOptionsWithoutRequest]) => Strategy & {
7
+ validate(...args: any[]): unknown;
8
+ };
9
+ /**
10
+ * JWT strategy for handling token validation.
11
+ */
12
+ export declare class JwtStrategy extends JwtStrategy_base {
13
+ private readonly _authSubject;
14
+ /**
15
+ * Constructs a new instance of the `JwtStrategy` class.
16
+ *
17
+ * @public
18
+ * @param authSubject Auth subject port instance.
19
+ * @param configService Configuration service instance.
20
+ */
21
+ constructor(authSubject: AuthSubjectPort, configService: ConfigService);
22
+ /**
23
+ * Validates the authentication credentials.
24
+ *
25
+ * @public
26
+ * @param payload The decoded JWT payload
27
+ * @returns The authenticated user and this is also later available in the request.user object
28
+ */
29
+ validate(payload: JwtPayloadBase): Promise<IAuthSubject>;
30
+ }
31
+ export {};
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ // #region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.JwtStrategy = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const archipel_platform_core_1 = require("@breadstone/archipel-platform-core");
7
+ const common_1 = require("@nestjs/common");
8
+ const passport_1 = require("@nestjs/passport");
9
+ const passport_jwt_1 = require("passport-jwt");
10
+ const AuthSubjectPort_1 = require("../contracts/AuthSubjectPort");
11
+ // #endregion
12
+ /**
13
+ * JWT strategy for handling token validation.
14
+ */
15
+ let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(passport_jwt_1.Strategy) {
16
+ // #endregion
17
+ // #region Ctor
18
+ /**
19
+ * Constructs a new instance of the `JwtStrategy` class.
20
+ *
21
+ * @public
22
+ * @param authSubject Auth subject port instance.
23
+ * @param configService Configuration service instance.
24
+ */
25
+ constructor(authSubject, configService) {
26
+ super({
27
+ jwtFromRequest: passport_jwt_1.ExtractJwt.fromAuthHeaderAsBearerToken(),
28
+ ignoreExpiration: false, // Optional: Allow or disallow expired tokens
29
+ secretOrKey: configService.get('AUTH_JWT_SECRET'), // The secret used for signing tokens
30
+ });
31
+ this._authSubject = authSubject;
32
+ }
33
+ // #endregion
34
+ // #region Methods
35
+ /**
36
+ * Validates the authentication credentials.
37
+ *
38
+ * @public
39
+ * @param payload The decoded JWT payload
40
+ * @returns The authenticated user and this is also later available in the request.user object
41
+ */
42
+ async validate(payload) {
43
+ const user = await this._authSubject.findById(payload.id);
44
+ if (!user) {
45
+ throw new common_1.NotFoundException('User not found');
46
+ }
47
+ return user;
48
+ }
49
+ };
50
+ exports.JwtStrategy = JwtStrategy;
51
+ exports.JwtStrategy = JwtStrategy = tslib_1.__decorate([
52
+ (0, common_1.Injectable)(),
53
+ tslib_1.__metadata("design:paramtypes", [AuthSubjectPort_1.AuthSubjectPort, archipel_platform_core_1.ConfigService])
54
+ ], JwtStrategy);
55
+ //# sourceMappingURL=JwtStrategy.js.map
@@ -0,0 +1,31 @@
1
+ import { Strategy } from 'passport-local';
2
+ import { AuthSubjectPort } from '../contracts/AuthSubjectPort';
3
+ import type { IAuthSubject } from '../contracts/IAuthSubject';
4
+ declare const LocalStrategy_base: new (...args: [] | [options: import("passport-local").IStrategyOptionsWithRequest] | [options: import("passport-local").IStrategyOptions]) => Strategy & {
5
+ validate(...args: any[]): unknown;
6
+ };
7
+ /**
8
+ * Local strategy for user name and password login.
9
+ *
10
+ * @public
11
+ */
12
+ export declare class LocalStrategy extends LocalStrategy_base {
13
+ private readonly _authSubject;
14
+ /**
15
+ * Constructs a new instance of the `LocalStrategy` class.
16
+ *
17
+ * @public
18
+ * @param authSubject Auth subject port instance.
19
+ */
20
+ constructor(authSubject: AuthSubjectPort);
21
+ /**
22
+ * Validates the authentication credentials.
23
+ *
24
+ * @public
25
+ * @param userName The user name
26
+ * @param password The password
27
+ * @returns The authenticated user and this is also later available in the request.user object
28
+ */
29
+ validate(userName: string, password: string): Promise<IAuthSubject>;
30
+ }
31
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ // #region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.LocalStrategy = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@nestjs/common");
7
+ const passport_1 = require("@nestjs/passport");
8
+ const passport_local_1 = require("passport-local");
9
+ const AuthSubjectPort_1 = require("../contracts/AuthSubjectPort");
10
+ // #endregion
11
+ /**
12
+ * Local strategy for user name and password login.
13
+ *
14
+ * @public
15
+ */
16
+ let LocalStrategy = class LocalStrategy extends (0, passport_1.PassportStrategy)(passport_local_1.Strategy) {
17
+ // #endregion
18
+ // #region Ctor
19
+ /**
20
+ * Constructs a new instance of the `LocalStrategy` class.
21
+ *
22
+ * @public
23
+ * @param authSubject Auth subject port instance.
24
+ */
25
+ constructor(authSubject) {
26
+ super();
27
+ this._authSubject = authSubject;
28
+ }
29
+ // #endregion
30
+ // #region Methods
31
+ /**
32
+ * Validates the authentication credentials.
33
+ *
34
+ * @public
35
+ * @param userName The user name
36
+ * @param password The password
37
+ * @returns The authenticated user and this is also later available in the request.user object
38
+ */
39
+ async validate(userName, password) {
40
+ const user = await this._authSubject.findByLogin(userName);
41
+ if (!user) {
42
+ throw new common_1.NotFoundException('User not found');
43
+ }
44
+ // TODO Check if we need bcrypt here...
45
+ if (user.password !== password) {
46
+ throw new common_1.UnauthorizedException('Invalid credentials');
47
+ }
48
+ return user;
49
+ }
50
+ };
51
+ exports.LocalStrategy = LocalStrategy;
52
+ exports.LocalStrategy = LocalStrategy = tslib_1.__decorate([
53
+ (0, common_1.Injectable)(),
54
+ tslib_1.__metadata("design:paramtypes", [AuthSubjectPort_1.AuthSubjectPort])
55
+ ], LocalStrategy);
56
+ //# sourceMappingURL=LocalStrategy.js.map
@@ -0,0 +1,35 @@
1
+ import { ConfigService } from '@breadstone/archipel-platform-core';
2
+ import type { Profile as PassportProfile } from 'passport';
3
+ import { Strategy } from 'passport-microsoft';
4
+ import type { IAuthSubject } from '../contracts/IAuthSubject';
5
+ import { SocialAuthPort } from '../contracts/SocialAuthPort';
6
+ declare const MicrosoftStrategy_base: new (...args: [options: import("passport-microsoft").MicrosoftStrategyOptionsWithRequest] | [options: import("passport-microsoft").MicrosoftStrategyOptions]) => Strategy & {
7
+ validate(...args: any[]): unknown;
8
+ };
9
+ /**
10
+ * The `MicrosoftStrategy` integrates Microsoft OAuth authentication.
11
+ *
12
+ * @public
13
+ */
14
+ export declare class MicrosoftStrategy extends MicrosoftStrategy_base {
15
+ private readonly _socialAuth;
16
+ /**
17
+ * Initializes a new instance of the `MicrosoftStrategy` class.
18
+ *
19
+ * @public
20
+ * @param socialAuth Social auth port instance.
21
+ * @param configService Configuration service instance.
22
+ */
23
+ constructor(socialAuth: SocialAuthPort, configService: ConfigService);
24
+ /**
25
+ * Validates the Microsoft authentication payload.
26
+ *
27
+ * @public
28
+ * @param accessToken OAuth access token.
29
+ * @param refreshToken OAuth refresh token.
30
+ * @param profile Microsoft profile data.
31
+ * @returns The authenticated user.
32
+ */
33
+ validate(accessToken: string, refreshToken: string, profile: PassportProfile): Promise<IAuthSubject>;
34
+ }
35
+ export {};
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ // #region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.MicrosoftStrategy = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const archipel_platform_core_1 = require("@breadstone/archipel-platform-core");
7
+ const common_1 = require("@nestjs/common");
8
+ const passport_1 = require("@nestjs/passport");
9
+ const passport_microsoft_1 = require("passport-microsoft");
10
+ const SocialAuthPort_1 = require("../contracts/SocialAuthPort");
11
+ // #endregion
12
+ /**
13
+ * The `MicrosoftStrategy` integrates Microsoft OAuth authentication.
14
+ *
15
+ * @public
16
+ */
17
+ let MicrosoftStrategy = class MicrosoftStrategy extends (0, passport_1.PassportStrategy)(passport_microsoft_1.Strategy, 'microsoft') {
18
+ // #endregion
19
+ // #region Ctor
20
+ /**
21
+ * Initializes a new instance of the `MicrosoftStrategy` class.
22
+ *
23
+ * @public
24
+ * @param socialAuth Social auth port instance.
25
+ * @param configService Configuration service instance.
26
+ */
27
+ constructor(socialAuth, configService) {
28
+ super({
29
+ clientID: configService.get('AUTH_MICROSOFT_CLIENT_ID'),
30
+ clientSecret: configService.get('AUTH_MICROSOFT_CLIENT_SECRET'),
31
+ callbackURL: configService.tryGet('AUTH_MICROSOFT_CALLBACK_URL', '/auth/microsoft/callback'),
32
+ tenant: configService.tryGet('AUTH_MICROSOFT_TENANT', 'common'),
33
+ scope: ['user.read'],
34
+ state: true,
35
+ });
36
+ this._socialAuth = socialAuth;
37
+ }
38
+ // #endregion
39
+ // #region Methods
40
+ /**
41
+ * Validates the Microsoft authentication payload.
42
+ *
43
+ * @public
44
+ * @param accessToken OAuth access token.
45
+ * @param refreshToken OAuth refresh token.
46
+ * @param profile Microsoft profile data.
47
+ * @returns The authenticated user.
48
+ */
49
+ async validate(accessToken, refreshToken, profile) {
50
+ void accessToken;
51
+ void refreshToken;
52
+ const email = profile.emails?.[0]?.value;
53
+ if (!email) {
54
+ throw new common_1.UnauthorizedException('No email provided from Microsoft');
55
+ }
56
+ const displayName = profile.displayName ?? profile.name?.givenName ?? profile.name?.familyName;
57
+ if (!displayName) {
58
+ throw new common_1.UnauthorizedException('No display name provided from Microsoft');
59
+ }
60
+ const userName = profile.username ?? email.split('@')[0];
61
+ const user = await this._socialAuth.findOrCreateByProfile({
62
+ provider: 'microsoft',
63
+ email: email,
64
+ userName: userName,
65
+ displayName: displayName,
66
+ });
67
+ return user;
68
+ }
69
+ };
70
+ exports.MicrosoftStrategy = MicrosoftStrategy;
71
+ exports.MicrosoftStrategy = MicrosoftStrategy = tslib_1.__decorate([
72
+ (0, common_1.Injectable)(),
73
+ tslib_1.__metadata("design:paramtypes", [SocialAuthPort_1.SocialAuthPort, archipel_platform_core_1.ConfigService])
74
+ ], MicrosoftStrategy);
75
+ //# sourceMappingURL=MicrosoftStrategy.js.map