orange-auth 1.1.0 → 1.2.0

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) Mathieu Dery.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) Mathieu Dery.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE
package/dist/lib.js CHANGED
@@ -130,7 +130,7 @@ export const CreateAuth = ((config) => {
130
130
  session: params.session,
131
131
  });
132
132
  }
133
- // Use the strategy to logout
133
+ // Use the strategy to logout
134
134
  await globalCfg.strategy.logOut(req, globalCfg);
135
135
  // Clears the header.
136
136
  const headers = new Headers();
@@ -1 +1 @@
1
- {"version":3,"file":"Credentials.d.ts","sourceRoot":"","sources":["../../src/providers/Credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAE/D;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,YAAY,SAAS,MAAM,IAAI,QAAQ,CAAC;IAClE;;;OAGG;IACH,IAAI,CAAC,EAAE,aAAa,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;IACrC;;OAEG;IACH,WAAW,EAAE,YAAY,EAAE,CAAC;IAC5B;;;;;OAKG;IACH,SAAS,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;CAC1F,CAAC,CAAC;AAEH;;GAEG;AACH,qBAAa,WAAW,CAAC,YAAY,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,SAAS;IAC5E,OAAO,CAAC,MAAM,CAAkC;gBAEpC,MAAM,EAAE,iBAAiB,CAAC,YAAY,CAAC;IAK7B,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CA6B9F"}
1
+ {"version":3,"file":"Credentials.d.ts","sourceRoot":"","sources":["../../src/providers/Credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAE/D;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,YAAY,SAAS,MAAM,IAAI,QAAQ,CAAC;IAClE;;;OAGG;IACH,IAAI,CAAC,EAAE,aAAa,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;IACrC;;OAEG;IACH,WAAW,EAAE,YAAY,EAAE,CAAC;IAC5B;;;;;OAKG;IACH,SAAS,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;CAC1F,CAAC,CAAC;AAEH;;GAEG;AACH,qBAAa,WAAW,CAAC,YAAY,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,SAAS;IAC5E,OAAO,CAAC,MAAM,CAAkC;gBAEpC,MAAM,EAAE,iBAAiB,CAAC,YAAY,CAAC;IAK7B,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAgC9F"}
@@ -34,6 +34,9 @@ export class Credentials extends IProvider {
34
34
  if (isNil(session))
35
35
  return null;
36
36
  // Create a token
37
- return globalCfg.strategy.serialize(session, globalCfg);
37
+ return globalCfg.strategy.serialize(session, globalCfg).catch((err) => {
38
+ console.log(err);
39
+ return null;
40
+ });
38
41
  }
39
42
  }
@@ -1,9 +1,28 @@
1
- import { type Session } from "../@types/globals";
1
+ import { type MaybePromise, type Session } from "../@types/globals";
2
2
  import type { ConfigOptions } from "../@types/internals";
3
+ /**
4
+ * Strategies callbacks
5
+ */
6
+ export type Callbacks = Partial<{
7
+ /**
8
+ * Pre-serialization callback. This can be used to add some steps to this process.
9
+ * @param session The session to be serialized.
10
+ * @returns A boolean representing if the serialization should occur.
11
+ */
12
+ serialize: (session: Session) => MaybePromise<boolean>;
13
+ /**
14
+ * Post-deserialization callback. This can be used to add some validation to this process.
15
+ * @param session The token that was deserialized.
16
+ * @returns A boolean representing if the deserialization is valid.
17
+ */
18
+ deserialize: (token: string, session: Session) => MaybePromise<boolean>;
19
+ }>;
3
20
  /**
4
21
  * A strategy is used to handle the creation, validation and accessing a user's session.
5
22
  */
6
23
  declare abstract class IStrategy {
24
+ protected callbacks: Callbacks;
25
+ constructor(callbacks: Callbacks);
7
26
  /**
8
27
  * Handles how a session token is generated.
9
28
  * @param session The validated session object.
@@ -1 +1 @@
1
- {"version":3,"file":"IStrategy.d.ts","sourceRoot":"","sources":["../../src/strategies/IStrategy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;GAEG;AACH,uBAAe,SAAS;IACpB;;;;;OAKG;aACa,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAEtF;;;;;OAKG;aACa,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAE7F;;;;OAIG;aACa,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAChF;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
1
+ {"version":3,"file":"IStrategy.d.ts","sourceRoot":"","sources":["../../src/strategies/IStrategy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,OAAO,CAAC;IAC5B;;;;OAIG;IACH,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,YAAY,CAAC,OAAO,CAAC,CAAC;IACvD;;;;OAIG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,YAAY,CAAC,OAAO,CAAC,CAAC;CAC3E,CAAC,CAAC;AAEH;;GAEG;AACH,uBAAe,SAAS;IACpB,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC;gBAEnB,SAAS,EAAE,SAAS;IAIhC;;;;;OAKG;aACa,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAEtF;;;;;OAKG;aACa,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAE7F;;;;OAIG;aACa,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAChF;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -3,5 +3,9 @@ import {} from "../@types/globals";
3
3
  * A strategy is used to handle the creation, validation and accessing a user's session.
4
4
  */
5
5
  class IStrategy {
6
+ callbacks;
7
+ constructor(callbacks) {
8
+ this.callbacks = callbacks;
9
+ }
6
10
  }
7
11
  export { IStrategy };
@@ -1,6 +1,6 @@
1
- import { IStrategy } from "./IStrategy";
2
1
  import type { SignOptions } from "jsonwebtoken";
3
2
  import type { Session } from "../@types/globals";
3
+ import { IStrategy, type Callbacks } from "./IStrategy";
4
4
  import type { ConfigOptions } from "../@types/internals";
5
5
  /**
6
6
  * Basic JWT strategy
@@ -10,7 +10,7 @@ declare class JWT extends IStrategy {
10
10
  * Forwarded standard JWT options
11
11
  */
12
12
  private signOptions;
13
- constructor(options?: SignOptions);
13
+ constructor(options?: SignOptions, callbacks?: Callbacks);
14
14
  serialize(session: Session, globalCfg: ConfigOptions): Promise<string>;
15
15
  deserialize(token: string, globalCfg: ConfigOptions): Promise<Session | null>;
16
16
  logOut(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/strategies/jwt.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAgBzD;;GAEG;AACH,cAAM,GAAI,SAAQ,SAAS;IACvB;;OAEG;IACH,OAAO,CAAC,WAAW,CAAc;gBAErB,OAAO,GAAE,WAAiC;IAMtC,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAKtE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAK7E,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAI1C;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
1
+ {"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/strategies/jwt.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAgBzD;;GAEG;AACH,cAAM,GAAI,SAAQ,SAAS;IACvB;;OAEG;IACH,OAAO,CAAC,WAAW,CAAc;gBAErB,OAAO,GAAE,WAAiC,EAAE,SAAS,GAAE,SAAc;IAM3D,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAY5E,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAS7E,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAI1C;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
@@ -1,18 +1,18 @@
1
- import { isString } from "lodash-es";
2
- import { IStrategy } from "./IStrategy";
1
+ import { isNil, isString } from "lodash-es";
3
2
  import { verify, sign } from "../functions/jwt";
3
+ import { IStrategy } from "./IStrategy";
4
4
  /**
5
5
  * Retrieves either the secret or a private key, depending on the used JWT algorithm
6
6
  * @param secret Secret key, or key pair
7
7
  * @returns The secret or private key
8
8
  */
9
- const secretOrPrivateKey = (secret) => isString(secret) ? secret : secret.privateKey;
9
+ const secretOrPrivateKey = (secret) => (isString(secret) ? secret : secret.privateKey);
10
10
  /**
11
11
  * Retrieves either the secret or a public key, depending on the used JWT algorithm
12
12
  * @param secret Secret key, or key pair
13
13
  * @returns The secret or public key
14
14
  */
15
- const secretOrPublicKey = (secret) => isString(secret) ? secret : secret.publicKey;
15
+ const secretOrPublicKey = (secret) => (isString(secret) ? secret : secret.publicKey);
16
16
  /**
17
17
  * Basic JWT strategy
18
18
  */
@@ -21,17 +21,27 @@ class JWT extends IStrategy {
21
21
  * Forwarded standard JWT options
22
22
  */
23
23
  signOptions;
24
- constructor(options = { expiresIn: "1h" }) {
25
- super();
24
+ constructor(options = { expiresIn: "1h" }, callbacks = {}) {
25
+ super(callbacks);
26
26
  this.signOptions = options;
27
27
  }
28
- serialize(session, globalCfg) {
28
+ async serialize(session, globalCfg) {
29
+ // If there is no callback set, we can just run normally, so fallback to true.
30
+ const shouldRun = await Promise.resolve(this.callbacks.serialize?.(session) ?? true);
31
+ if (!shouldRun) {
32
+ return Promise.reject("Serialize callback rejection");
33
+ }
29
34
  // Directly call the sign function, but make it async.
30
35
  return Promise.resolve(sign(session, secretOrPrivateKey(globalCfg.secret), this.signOptions));
31
36
  }
32
37
  deserialize(token, globalCfg) {
33
38
  // The verify function does everything for us, in this case.
34
- return verify(token, secretOrPublicKey(globalCfg.secret));
39
+ return verify(token, secretOrPublicKey(globalCfg.secret)).then(async (session) => {
40
+ if (isNil(session))
41
+ return null;
42
+ const isValid = await Promise.resolve(this.callbacks.deserialize?.(token, session) ?? true);
43
+ return isValid ? session : null;
44
+ });
35
45
  }
36
46
  logOut() {
37
47
  // Since a JWT does not have any data in a DB, there is nothing to do here.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-auth",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "description": "Simple modular auth library",
6
6
  "main": "dist/index.js",