secure-web-token 1.2.6 → 1.2.7

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/sign.d.ts CHANGED
@@ -1,13 +1,35 @@
1
1
  import { StoreType } from "./store";
2
+ /**
3
+ * Options for signing a Secure Web Token.
4
+ */
2
5
  export interface SignOptions {
6
+ /**
7
+ * Token expiration time in seconds. Defaults to 900 (15 minutes).
8
+ */
3
9
  expiresIn?: number;
10
+ /**
11
+ * Whether to enable fingerprint/session mode. If true, generates a device-bound session.
12
+ */
4
13
  fingerprint?: true;
14
+ /**
15
+ * The store type to use for session persistence.
16
+ */
5
17
  store?: StoreType;
6
18
  }
7
19
  /**
8
- * sign() now returns:
9
- * - token (encrypted payload)
10
- * - sessionId (to store in HttpOnly cookie)
20
+ * Signs a payload to create a Secure Web Token (SWT).
21
+ *
22
+ * @param data - The object to be encrypted in the token. Must include `userId` if using fingerprint/session mode.
23
+ * @param secret - The secret key used for encryption and HMAC signing.
24
+ * @param options - Configuration options for the token.
25
+ * @param options.expiresIn - Token expiration time in seconds (default: 900).
26
+ * @param options.fingerprint - Set to true to enable device-bound session mode.
27
+ * @param options.store - The store type to use for session persistence (e.g., 'memory').
28
+ *
29
+ * @returns An object containing the generated `token` and an optional `sessionId` if fingerprinting is enabled.
30
+ *
31
+ * @example
32
+ * const { token, sessionId } = sign({ userId: '123' }, 'my-secret', { fingerprint: true });
11
33
  */
12
34
  export default function sign(data: Record<string, any>, secret: string, options?: SignOptions): {
13
35
  token: string;
@@ -1 +1 @@
1
- {"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../src/sign.ts"],"names":[],"mappings":"AAIA,OAAO,EAAY,SAAS,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,WAAW,WAAW;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,IAAI,CAC1B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,WAAgB,GACxB;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAoDvC"}
1
+ {"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../src/sign.ts"],"names":[],"mappings":"AAIA,OAAO,EAAY,SAAS,EAAE,MAAM,SAAS,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB;;OAEG;IACH,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,OAAO,UAAU,IAAI,CAC1B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,WAAgB,GACxB;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAoDvC"}
package/dist/sign.js CHANGED
@@ -43,9 +43,19 @@ const utils_1 = require("./utils");
43
43
  const device_1 = require("./device");
44
44
  const store_1 = require("./store");
45
45
  /**
46
- * sign() now returns:
47
- * - token (encrypted payload)
48
- * - sessionId (to store in HttpOnly cookie)
46
+ * Signs a payload to create a Secure Web Token (SWT).
47
+ *
48
+ * @param data - The object to be encrypted in the token. Must include `userId` if using fingerprint/session mode.
49
+ * @param secret - The secret key used for encryption and HMAC signing.
50
+ * @param options - Configuration options for the token.
51
+ * @param options.expiresIn - Token expiration time in seconds (default: 900).
52
+ * @param options.fingerprint - Set to true to enable device-bound session mode.
53
+ * @param options.store - The store type to use for session persistence (e.g., 'memory').
54
+ *
55
+ * @returns An object containing the generated `token` and an optional `sessionId` if fingerprinting is enabled.
56
+ *
57
+ * @example
58
+ * const { token, sessionId } = sign({ userId: '123' }, 'my-secret', { fingerprint: true });
49
59
  */
50
60
  function sign(data, secret, options = {}) {
51
61
  if (!secret || typeof secret !== "string")
@@ -1,4 +1,10 @@
1
1
  import { Store } from "./types";
2
2
  export type StoreType = "memory";
3
+ /**
4
+ * Retrieves a session store instance by type.
5
+ *
6
+ * @param type - The type of store to retrieve (e.g., 'memory').
7
+ * @returns The store instance or null if no type is provided or the type is invalid.
8
+ */
3
9
  export declare function getStore(type?: StoreType): Store | null;
4
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/store/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,MAAM,SAAS,GAAG,QAAQ,CAAC;AAEjC,wBAAgB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,IAAI,CASvD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/store/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,MAAM,SAAS,GAAG,QAAQ,CAAC;AAEjC;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,IAAI,CASvD"}
@@ -2,6 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getStore = getStore;
4
4
  const memoryStore_1 = require("./memoryStore");
5
+ /**
6
+ * Retrieves a session store instance by type.
7
+ *
8
+ * @param type - The type of store to retrieve (e.g., 'memory').
9
+ * @returns The store instance or null if no type is provided or the type is invalid.
10
+ */
5
11
  function getStore(type) {
6
12
  if (!type)
7
13
  return null;
@@ -1,16 +1,32 @@
1
+ /**
2
+ * Interface for session storage backends.
3
+ */
1
4
  export interface Store {
5
+ /**
6
+ * Registers a new session in the store.
7
+ * @param session - The session details to store.
8
+ */
2
9
  registerSession(session: {
3
10
  sessionId: string;
4
11
  userId: string | number;
5
12
  deviceId: string;
6
13
  fingerprint: string;
7
14
  }): void;
15
+ /**
16
+ * Retrieves a session by its ID.
17
+ * @param sessionId - The unique identifier for the session.
18
+ * @returns The session details or null if not found.
19
+ */
8
20
  getSession(sessionId: string): {
9
21
  sessionId: string;
10
22
  userId: string | number;
11
23
  deviceId: string;
12
24
  fingerprint: string;
13
25
  } | null;
26
+ /**
27
+ * Revokes (removes) a session from the store.
28
+ * @param sessionId - The unique identifier for the session to revoke.
29
+ */
14
30
  revokeSession(sessionId: string): void;
15
31
  }
16
32
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/store/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,KAAK;IAClB,eAAe,CAAC,OAAO,EAAE;QACrB,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;QACxB,QAAQ,EAAE,MAAM,CAAC;QACjB,WAAW,EAAE,MAAM,CAAC;KACvB,GAAG,IAAI,CAAC;IAET,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG;QAC3B,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;QACxB,QAAQ,EAAE,MAAM,CAAC;QACjB,WAAW,EAAE,MAAM,CAAC;KACvB,GAAG,IAAI,CAAC;IAET,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1C"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/store/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,KAAK;IAClB;;;OAGG;IACH,eAAe,CAAC,OAAO,EAAE;QACrB,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;QACxB,QAAQ,EAAE,MAAM,CAAC;QACjB,WAAW,EAAE,MAAM,CAAC;KACvB,GAAG,IAAI,CAAC;IAET;;;;OAIG;IACH,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG;QAC3B,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;QACxB,QAAQ,EAAE,MAAM,CAAC;QACjB,WAAW,EAAE,MAAM,CAAC;KACvB,GAAG,IAAI,CAAC;IAET;;;OAGG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1C"}
package/dist/verify.d.ts CHANGED
@@ -1,8 +1,33 @@
1
1
  import { StoreType } from "./store";
2
+ /**
3
+ * Options for verifying a Secure Web Token.
4
+ */
2
5
  export interface VerifyOptions {
6
+ /**
7
+ * The session ID to verify against the store. Should be retrieved from an HttpOnly cookie.
8
+ */
3
9
  sessionId?: string;
10
+ /**
11
+ * The unique fingerprint of the device/session.
12
+ */
4
13
  fingerprint?: string;
14
+ /**
15
+ * The store type used to retrieve session data.
16
+ */
5
17
  store?: StoreType;
6
18
  }
19
+ /**
20
+ * Verifies and decrypts a Secure Web Token (SWT).
21
+ *
22
+ * @param token - The SWT string to verify.
23
+ * @param secret - The secret key used for decryption and signature verification.
24
+ * @param options - Verification options.
25
+ * @param options.sessionId - The session ID to verify against the store (Backend-only mode).
26
+ * @param options.fingerprint - The device/session fingerprint to verify.
27
+ * @param options.store - The store type used for session verification.
28
+ *
29
+ * @returns The decrypted payload data.
30
+ * @throws {Error} If the token is invalid, expired, or session verification fails.
31
+ */
7
32
  export default function verify(token: string, secret: string, options?: VerifyOptions): Record<string, any>;
8
33
  //# sourceMappingURL=verify.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"AAGA,OAAO,EAAY,SAAS,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,WAAW,aAAa;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED,MAAM,CAAC,OAAO,UAAU,MAAM,CAC5B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAoCrB"}
1
+ {"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"AAGA,OAAO,EAAY,SAAS,EAAE,MAAM,SAAS,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM,CAC5B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAoCrB"}
package/dist/verify.js CHANGED
@@ -41,6 +41,19 @@ const crypto = __importStar(require("crypto"));
41
41
  const decrypt_1 = __importDefault(require("./decrypt"));
42
42
  const utils_1 = require("./utils");
43
43
  const store_1 = require("./store");
44
+ /**
45
+ * Verifies and decrypts a Secure Web Token (SWT).
46
+ *
47
+ * @param token - The SWT string to verify.
48
+ * @param secret - The secret key used for decryption and signature verification.
49
+ * @param options - Verification options.
50
+ * @param options.sessionId - The session ID to verify against the store (Backend-only mode).
51
+ * @param options.fingerprint - The device/session fingerprint to verify.
52
+ * @param options.store - The store type used for session verification.
53
+ *
54
+ * @returns The decrypted payload data.
55
+ * @throws {Error} If the token is invalid, expired, or session verification fails.
56
+ */
44
57
  function verify(token, secret, options = {}) {
45
58
  if (!token || typeof token !== "string")
46
59
  throw new Error("Token must be string");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "secure-web-token",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "description": "A secure web token utility",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "type": "git",
20
20
  "url": "git+https://github.com/MintuSingh07/node-securewebtoken.git"
21
21
  },
22
- "homepage": "https://securewebtoken.vercel.app/",
22
+ "homepage": "https://securewebtoken.vercel.app",
23
23
  "bugs": {
24
24
  "url": "https://github.com/MintuSingh07/node-securewebtoken/issues"
25
25
  },