@maestro-js/password 1.0.0-alpha.1 → 1.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4,38 +4,61 @@ import { Hash } from '@maestro-js/hash';
4
4
 
5
5
  declare function create<Id, Credentials extends {
6
6
  password: string;
7
- }>(config: Password.Config<Id, Credentials>): Password.Service<Id, Credentials>;
8
- declare const Password: {
9
- create: typeof create;
7
+ }>(config: Password.Internal.Config<Id, Credentials>): Password.Internal.Service<Id, Credentials>;
8
+ declare function provider<Key extends Password.Provider.Key>(key: Key): Password.PasswordService<Key>;
9
+ /**
10
+ * Password verification and reset with pluggable credential lookups and secure token-based resets.
11
+ * Call methods directly on the default instance, or use `Password.Provider.create()` for named instances.
12
+ */
13
+ declare const PasswordBase: {
14
+ Provider: {
15
+ create: typeof create;
16
+ register: (name: Password.Provider.Key, item: Password.AnyPasswordService) => void;
17
+ };
18
+ provider: typeof provider;
10
19
  errors: {
11
20
  ThrottledError: CustomErrors.CustomErrorConstructor<undefined, {}, "tooManyRequests">;
12
21
  InvalidTokenError: CustomErrors.CustomErrorConstructor<undefined, {}, "unprocessableContent">;
13
22
  ExpiredTokenError: CustomErrors.CustomErrorConstructor<undefined, {}, "unprocessableContent">;
14
23
  };
15
24
  };
25
+ type KeysWithFallback = keyof Password.Provider.Keys extends never ? {
26
+ default: {
27
+ Id: unknown;
28
+ Credentials: {
29
+ password: string;
30
+ } & Record<string, unknown>;
31
+ };
32
+ } : Password.Provider.Keys;
33
+ declare const Password: Password.PasswordService<'default'> & typeof PasswordBase;
16
34
  declare namespace Password {
17
- interface Service<Id, Credentials extends {
18
- password: string;
19
- }> {
20
- verifyCredentials(credentials: Credentials): Promise<Password.Authenticatable<Id> | null>;
21
- createPasswordResetToken: (authenticatableId: Id) => Promise<string>;
22
- resetPassword: (options: {
23
- authenticatableId: Id;
24
- token: string;
25
- newPassword: string;
26
- }) => Promise<void>;
27
- }
28
- interface Config<Id, Credentials extends {
29
- password: string;
30
- }> {
31
- getAuthenticatableByCredentials(credentials: Omit<Credentials, 'password'>): Promise<(Password.Authenticatable<Id> & {
32
- passwordHash: string;
33
- }) | null>;
34
- updatePasswordHash(id: Id, passwordHash: string): Promise<void>;
35
- updateCredentialsInvalidBefore(id: Id, date: Iso.Instant): Promise<void>;
36
- /** Used for password hashing and verification. `Hash.drivers.bcrypt()` is recommended. */
37
- hash: Hash.HashService;
38
- passwordResetOptions?: Password.ResetOptions<Id>;
35
+ type PasswordService<ProviderKey extends Password.Provider.Key = 'default'> = Internal.Service<KeysWithFallback[ProviderKey]['Id'], KeysWithFallback[ProviderKey]['Credentials']>;
36
+ type AnyPasswordService = Internal.Service<any, any>;
37
+ type PasswordServiceConfig<ProviderKey extends Password.Provider.Key = 'default'> = Internal.Config<KeysWithFallback[ProviderKey]['Id'], KeysWithFallback[ProviderKey]['Credentials']>;
38
+ namespace Internal {
39
+ interface Service<Id, Credentials extends {
40
+ password: string;
41
+ }> {
42
+ verifyCredentials(credentials: Credentials): Promise<Password.Authenticatable<Id> | null>;
43
+ createPasswordResetToken: (authenticatableId: Id) => Promise<string>;
44
+ resetPassword: (options: {
45
+ authenticatableId: Id;
46
+ token: string;
47
+ newPassword: string;
48
+ }) => Promise<void>;
49
+ }
50
+ interface Config<Id, Credentials extends {
51
+ password: string;
52
+ }> {
53
+ getAuthenticatableByCredentials(credentials: Omit<Credentials, 'password'>): Promise<(Password.Authenticatable<Id> & {
54
+ passwordHash: string;
55
+ }) | null>;
56
+ updatePasswordHash(id: Id, passwordHash: string): Promise<void>;
57
+ updateCredentialsInvalidBefore(id: Id, date: Iso.Instant): Promise<void>;
58
+ /** Used for password hashing and verification. `Hash.drivers.bcrypt()` is recommended. */
59
+ hash: Hash.HashService;
60
+ passwordResetOptions?: Password.ResetOptions<Id>;
61
+ }
39
62
  }
40
63
  interface Authenticatable<Id> {
41
64
  id: Id;
@@ -58,6 +81,11 @@ declare namespace Password {
58
81
  /** Minimum seconds between createToken calls for the same authenticatable. Default: 60 */
59
82
  throttleSeconds?: number;
60
83
  }
84
+ namespace Provider {
85
+ type Key = keyof KeysWithFallback;
86
+ interface Keys {
87
+ }
88
+ }
61
89
  }
62
90
 
63
91
  export { Password };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  import crypto from "crypto";
3
3
  import { CustomErrors } from "@maestro-js/custom-errors";
4
+ import { ServiceRegistry } from "@maestro-js/service-registry";
4
5
  import { instantFns } from "iso-fns2";
5
6
  function create(config) {
6
7
  const hashService = config.hash;
@@ -85,6 +86,21 @@ function create(config) {
85
86
  resetPassword
86
87
  };
87
88
  }
89
+ var registry = ServiceRegistry.createRegistry(
90
+ ServiceRegistry.proxyFunctionsForObject
91
+ );
92
+ var Provider = {
93
+ create,
94
+ register: registry.register
95
+ };
96
+ function provider(key) {
97
+ const service = registry.resolve(key);
98
+ return {
99
+ verifyCredentials: service.verifyCredentials,
100
+ createPasswordResetToken: service.createPasswordResetToken,
101
+ resetPassword: service.resetPassword
102
+ };
103
+ }
88
104
  var PasswordResetThrottledError = CustomErrors.create({
89
105
  name: "PasswordResetThrottledError",
90
106
  message: "Password reset requests are being throttled. Please try again later.",
@@ -100,14 +116,19 @@ var PasswordResetExpiredTokenError = CustomErrors.create({
100
116
  message: "The password reset token has expired.",
101
117
  httpStatusCode: "unprocessableContent"
102
118
  });
103
- var Password = {
104
- create,
119
+ var PasswordBase = {
120
+ Provider,
121
+ provider,
105
122
  errors: {
106
123
  ThrottledError: PasswordResetThrottledError,
107
124
  InvalidTokenError: PasswordResetInvalidTokenError,
108
125
  ExpiredTokenError: PasswordResetExpiredTokenError
109
126
  }
110
127
  };
128
+ var Password = {
129
+ ...PasswordBase,
130
+ ...provider("default")
131
+ };
111
132
  function decodePasswordResetToken(token) {
112
133
  const [selector, verifier] = token.split("|");
113
134
  if (!selector || !verifier) return null;
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "default": "./dist/index.js"
8
8
  }
9
9
  },
10
- "version": "1.0.0-alpha.1",
10
+ "version": "1.0.0-alpha.10",
11
11
  "publishConfig": {
12
12
  "access": "restricted"
13
13
  },
@@ -16,11 +16,15 @@
16
16
  ],
17
17
  "dependencies": {
18
18
  "iso-fns2": "npm:iso-fns@2.0.0-alpha.26",
19
- "@maestro-js/custom-errors": "1.0.0-alpha.1",
20
- "@maestro-js/hash": "1.0.0-alpha.1"
19
+ "@maestro-js/custom-errors": "1.0.0-alpha.10",
20
+ "@maestro-js/service-registry": "1.0.0-alpha.10"
21
+ },
22
+ "peerDependencies": {
23
+ "@maestro-js/hash": "1.0.0-alpha.10"
21
24
  },
22
25
  "devDependencies": {
23
- "@types/node": "^22.19.11"
26
+ "@types/node": "^22.19.11",
27
+ "@maestro-js/hash": "1.0.0-alpha.10"
24
28
  },
25
29
  "license": "UNLICENSED",
26
30
  "engines": {