@maestro-js/password 1.0.0-alpha.19 → 1.0.0-alpha.2

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,62 +4,38 @@ import { Hash } from '@maestro-js/hash';
4
4
 
5
5
  declare function create<Id, Credentials extends {
6
6
  password: string;
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;
7
+ }>(config: Password.Config<Id, Credentials>): Password.Service<Id, Credentials>;
8
+ declare const Password: {
9
+ create: typeof create;
19
10
  errors: {
20
11
  ThrottledError: CustomErrors.CustomErrorConstructor<undefined, {}, "tooManyRequests">;
21
12
  InvalidTokenError: CustomErrors.CustomErrorConstructor<undefined, {}, "unprocessableContent">;
22
13
  ExpiredTokenError: CustomErrors.CustomErrorConstructor<undefined, {}, "unprocessableContent">;
23
14
  };
24
15
  };
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;
34
16
  declare namespace Password {
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, options: {
44
- expirationMinutes: number;
45
- }) => Promise<string>;
46
- resetPassword: (options: {
47
- token: string;
48
- newPassword: string;
49
- }) => Promise<void>;
50
- }
51
- interface Config<Id, Credentials extends {
52
- password: string;
53
- }> {
54
- getAuthenticatableByCredentials(credentials: Omit<Credentials, 'password'>): Promise<(Password.Authenticatable<Id> & {
55
- passwordHash: string;
56
- }) | null>;
57
- updatePasswordHash(id: Id, passwordHash: string): Promise<void>;
58
- updateCredentialsInvalidBefore(id: Id, date: Iso.Instant): Promise<void>;
59
- /** Used for password hashing and verification. `Hash.drivers.bcrypt()` is recommended. */
60
- hash: Hash.HashService;
61
- passwordResetOptions?: Password.ResetOptions<Id>;
62
- }
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>;
63
39
  }
64
40
  interface Authenticatable<Id> {
65
41
  id: Id;
@@ -77,14 +53,11 @@ declare namespace Password {
77
53
  getBySelector(selector: string): Promise<Password.ResetTokenRecord<Id> | null>;
78
54
  getByAuthenticatableId(authenticatableId: Id): Promise<Password.ResetTokenRecord<Id> | null>;
79
55
  deleteByAuthenticatableId(authenticatableId: Id): Promise<void>;
56
+ /** Token lifetime in minutes. Default: 60 */
57
+ tokenExpirationMinutes?: number;
80
58
  /** Minimum seconds between createToken calls for the same authenticatable. Default: 60 */
81
59
  throttleSeconds?: number;
82
60
  }
83
- namespace Provider {
84
- type Key = keyof KeysWithFallback;
85
- interface Keys {
86
- }
87
- }
88
61
  }
89
62
 
90
63
  export { Password };
package/dist/index.js CHANGED
@@ -1,7 +1,6 @@
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";
5
4
  import { instantFns } from "iso-fns2";
6
5
  function create(config) {
7
6
  const hashService = config.hash;
@@ -24,7 +23,7 @@ function create(config) {
24
23
  }
25
24
  return { id: authenticatable.id, credentialsInvalidBefore: authenticatable.credentialsInvalidBefore };
26
25
  }
27
- async function createPasswordResetToken(authenticatableId, options) {
26
+ async function createPasswordResetToken(authenticatableId) {
28
27
  if (!passwordResetOptions) {
29
28
  throw new Error("Password reset is not configured. Provide `passwordResetOptions` when creating the password service.");
30
29
  }
@@ -39,7 +38,7 @@ function create(config) {
39
38
  const verifier = crypto.randomBytes(32).toString("base64url");
40
39
  const verifierHash = await config.hash.hash(verifier);
41
40
  const now = instantFns.now();
42
- const expirationDate = instantFns.add(now, { minutes: options.expirationMinutes });
41
+ const expirationDate = instantFns.add(now, { minutes: passwordResetOptions.tokenExpirationMinutes ?? 60 });
43
42
  await passwordResetOptions.create({
44
43
  authenticatableId,
45
44
  selector,
@@ -49,7 +48,11 @@ function create(config) {
49
48
  });
50
49
  return `${selector}|${verifier}`;
51
50
  }
52
- async function resetPassword({ token, newPassword }) {
51
+ async function resetPassword({
52
+ authenticatableId,
53
+ token,
54
+ newPassword
55
+ }) {
53
56
  if (!passwordResetOptions) {
54
57
  throw new Error("Password reset is not configured. Provide `passwordResetOptions` when creating the password service.");
55
58
  }
@@ -68,10 +71,13 @@ function create(config) {
68
71
  if (record.expirationDate < instantFns.now()) {
69
72
  throw new PasswordResetExpiredTokenError();
70
73
  }
74
+ if (record.authenticatableId !== authenticatableId) {
75
+ throw new PasswordResetInvalidTokenError();
76
+ }
71
77
  const passwordHash = await config.hash.hash(newPassword);
72
- await config.updatePasswordHash(record.authenticatableId, passwordHash);
73
- await config.updateCredentialsInvalidBefore(record.authenticatableId, instantFns.now());
74
- await passwordResetOptions.deleteByAuthenticatableId(record.authenticatableId);
78
+ await config.updatePasswordHash(authenticatableId, passwordHash);
79
+ await config.updateCredentialsInvalidBefore(authenticatableId, instantFns.now());
80
+ await passwordResetOptions.deleteByAuthenticatableId(authenticatableId);
75
81
  }
76
82
  return {
77
83
  verifyCredentials,
@@ -79,21 +85,6 @@ function create(config) {
79
85
  resetPassword
80
86
  };
81
87
  }
82
- var registry = ServiceRegistry.createRegistry(
83
- ServiceRegistry.proxyFunctionsForObject
84
- );
85
- var Provider = {
86
- create,
87
- register: registry.register
88
- };
89
- function provider(key) {
90
- const service = registry.resolve(key);
91
- return {
92
- verifyCredentials: service.verifyCredentials,
93
- createPasswordResetToken: service.createPasswordResetToken,
94
- resetPassword: service.resetPassword
95
- };
96
- }
97
88
  var PasswordResetThrottledError = CustomErrors.create({
98
89
  name: "PasswordResetThrottledError",
99
90
  message: "Password reset requests are being throttled. Please try again later.",
@@ -109,19 +100,14 @@ var PasswordResetExpiredTokenError = CustomErrors.create({
109
100
  message: "The password reset token has expired.",
110
101
  httpStatusCode: "unprocessableContent"
111
102
  });
112
- var PasswordBase = {
113
- Provider,
114
- provider,
103
+ var Password = {
104
+ create,
115
105
  errors: {
116
106
  ThrottledError: PasswordResetThrottledError,
117
107
  InvalidTokenError: PasswordResetInvalidTokenError,
118
108
  ExpiredTokenError: PasswordResetExpiredTokenError
119
109
  }
120
110
  };
121
- var Password = {
122
- ...PasswordBase,
123
- ...provider("default")
124
- };
125
111
  function decodePasswordResetToken(token) {
126
112
  const [selector, verifier] = token.split("|");
127
113
  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.19",
10
+ "version": "1.0.0-alpha.2",
11
11
  "publishConfig": {
12
12
  "access": "restricted"
13
13
  },
@@ -16,15 +16,11 @@
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.19",
20
- "@maestro-js/service-registry": "1.0.0-alpha.19"
21
- },
22
- "peerDependencies": {
23
- "@maestro-js/hash": "1.0.0-alpha.19"
19
+ "@maestro-js/custom-errors": "1.0.0-alpha.2",
20
+ "@maestro-js/hash": "1.0.0-alpha.2"
24
21
  },
25
22
  "devDependencies": {
26
- "@types/node": "^22.19.11",
27
- "@maestro-js/hash": "1.0.0-alpha.19"
23
+ "@types/node": "^22.19.11"
28
24
  },
29
25
  "license": "UNLICENSED",
30
26
  "engines": {