secure-web-token 1.0.4 → 1.1.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/dist/device.d.ts +1 -4
- package/dist/device.d.ts.map +1 -1
- package/dist/device.js +3 -39
- package/dist/sign.d.ts +2 -15
- package/dist/sign.d.ts.map +1 -1
- package/dist/sign.js +11 -7
- package/dist/store/index.d.ts +4 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +14 -0
- package/dist/store/memoryStore.d.ts +3 -0
- package/dist/store/memoryStore.d.ts.map +1 -0
- package/dist/store/memoryStore.js +21 -0
- package/dist/store/types.d.ts +6 -0
- package/dist/store/types.d.ts.map +1 -0
- package/dist/store/types.js +2 -0
- package/dist/verify.d.ts +2 -9
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +11 -8
- package/package.json +1 -1
package/dist/device.d.ts
CHANGED
package/dist/device.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"device.d.ts","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"device.d.ts","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC"}
|
package/dist/device.js
CHANGED
|
@@ -1,46 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
3
|
exports.generateDeviceId = generateDeviceId;
|
|
37
|
-
const
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
38
5
|
/**
|
|
39
|
-
*
|
|
40
|
-
* Used for Device Registration Model.
|
|
41
|
-
*
|
|
42
|
-
* @returns UUID v4 string
|
|
6
|
+
* Generates a unique device identifier
|
|
43
7
|
*/
|
|
44
8
|
function generateDeviceId() {
|
|
45
|
-
return
|
|
9
|
+
return (0, crypto_1.randomUUID)();
|
|
46
10
|
}
|
package/dist/sign.d.ts
CHANGED
|
@@ -1,22 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
* Options for signing a Secure Web Token (SWT)
|
|
3
|
-
*/
|
|
1
|
+
import { StoreType } from "./store";
|
|
4
2
|
export interface SignOptions {
|
|
5
|
-
/**
|
|
6
|
-
* Token expiry time in seconds
|
|
7
|
-
* @default 900 (15 minutes)
|
|
8
|
-
*/
|
|
9
3
|
expiresIn?: number;
|
|
10
|
-
/**
|
|
11
|
-
* true → auto-generate device ID
|
|
12
|
-
* string | string[] → custom fingerprints
|
|
13
|
-
*/
|
|
14
4
|
fingerprint?: true;
|
|
5
|
+
store?: StoreType;
|
|
15
6
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Creates a Secure Web Token (SWT).
|
|
18
|
-
* User data is stored inside `payload.data`.
|
|
19
|
-
*/
|
|
20
7
|
export default function sign(data: Record<string, any>, secret: string, options?: SignOptions): {
|
|
21
8
|
token: string;
|
|
22
9
|
deviceId?: string;
|
package/dist/sign.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../src/sign.ts"],"names":[],"mappings":"
|
|
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,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,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAsDtC"}
|
package/dist/sign.js
CHANGED
|
@@ -41,10 +41,7 @@ const crypto = __importStar(require("crypto"));
|
|
|
41
41
|
const encrypt_1 = __importDefault(require("./encrypt"));
|
|
42
42
|
const utils_1 = require("./utils");
|
|
43
43
|
const device_1 = require("./device");
|
|
44
|
-
|
|
45
|
-
* Creates a Secure Web Token (SWT).
|
|
46
|
-
* User data is stored inside `payload.data`.
|
|
47
|
-
*/
|
|
44
|
+
const store_1 = require("./store");
|
|
48
45
|
function sign(data, secret, options = {}) {
|
|
49
46
|
if (!secret || typeof secret !== "string") {
|
|
50
47
|
throw new Error("Secret must be a non-empty string");
|
|
@@ -52,17 +49,24 @@ function sign(data, secret, options = {}) {
|
|
|
52
49
|
if (!data || typeof data !== "object") {
|
|
53
50
|
throw new Error("Data must be an object");
|
|
54
51
|
}
|
|
52
|
+
if (!data.userId) {
|
|
53
|
+
throw new Error("data.userId is required when using store");
|
|
54
|
+
}
|
|
55
55
|
const now = Math.floor(Date.now() / 1000);
|
|
56
56
|
const payload = {
|
|
57
|
-
data,
|
|
57
|
+
data,
|
|
58
58
|
iat: now,
|
|
59
59
|
exp: now + (options.expiresIn ?? 900),
|
|
60
60
|
};
|
|
61
61
|
let deviceId;
|
|
62
|
-
// 🔐
|
|
62
|
+
// 🔐 Fingerprint + store registration
|
|
63
63
|
if (options.fingerprint === true) {
|
|
64
64
|
deviceId = (0, device_1.generateDeviceId)();
|
|
65
|
-
payload.fp =
|
|
65
|
+
payload.fp = deviceId;
|
|
66
|
+
const store = (0, store_1.getStore)(options.store);
|
|
67
|
+
if (store) {
|
|
68
|
+
store.register(data.userId, deviceId);
|
|
69
|
+
}
|
|
66
70
|
}
|
|
67
71
|
const header = {
|
|
68
72
|
alg: "AES-256-GCM+HMAC",
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStore = getStore;
|
|
4
|
+
const memoryStore_1 = require("./memoryStore");
|
|
5
|
+
function getStore(type) {
|
|
6
|
+
if (!type)
|
|
7
|
+
return null;
|
|
8
|
+
switch (type) {
|
|
9
|
+
case "memory":
|
|
10
|
+
return memoryStore_1.memoryStore;
|
|
11
|
+
default:
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoryStore.d.ts","sourceRoot":"","sources":["../../src/store/memoryStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAsBhC,eAAO,MAAM,WAAW,EAAE,KAAyB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memoryStore = void 0;
|
|
4
|
+
class MemoryStore {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.sessions = new Map();
|
|
7
|
+
}
|
|
8
|
+
makeKey(userId, fingerprint) {
|
|
9
|
+
return `${userId}:${fingerprint}`;
|
|
10
|
+
}
|
|
11
|
+
register(userId, fingerprint) {
|
|
12
|
+
this.sessions.set(this.makeKey(userId, fingerprint), true);
|
|
13
|
+
}
|
|
14
|
+
isValid(userId, fingerprint) {
|
|
15
|
+
return this.sessions.has(this.makeKey(userId, fingerprint));
|
|
16
|
+
}
|
|
17
|
+
revoke(userId, fingerprint) {
|
|
18
|
+
this.sessions.delete(this.makeKey(userId, fingerprint));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.memoryStore = new MemoryStore();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/store/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,KAAK;IAClB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7D,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC;IAC/D,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9D"}
|
package/dist/verify.d.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
* Options for verifying a Secure Web Token
|
|
3
|
-
*/
|
|
1
|
+
import { StoreType } from "./store";
|
|
4
2
|
export interface VerifyOptions {
|
|
5
|
-
/**
|
|
6
|
-
* Device fingerprint(s) allowed to verify token
|
|
7
|
-
*/
|
|
8
3
|
fingerprint?: string;
|
|
4
|
+
store?: StoreType;
|
|
9
5
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Verifies and decrypts a Secure Web Token.
|
|
12
|
-
*/
|
|
13
6
|
export default function verify(token: string, secret: string, options?: VerifyOptions): Record<string, any>;
|
|
14
7
|
//# sourceMappingURL=verify.d.ts.map
|
package/dist/verify.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"
|
|
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,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,CAuDrB"}
|
package/dist/verify.js
CHANGED
|
@@ -40,9 +40,7 @@ exports.default = verify;
|
|
|
40
40
|
const crypto = __importStar(require("crypto"));
|
|
41
41
|
const decrypt_1 = __importDefault(require("./decrypt"));
|
|
42
42
|
const utils_1 = require("./utils");
|
|
43
|
-
|
|
44
|
-
* Verifies and decrypts a Secure Web Token.
|
|
45
|
-
*/
|
|
43
|
+
const store_1 = require("./store");
|
|
46
44
|
function verify(token, secret, options = {}) {
|
|
47
45
|
if (!token || typeof token !== "string") {
|
|
48
46
|
throw new Error("Token must be a string");
|
|
@@ -68,13 +66,18 @@ function verify(token, secret, options = {}) {
|
|
|
68
66
|
if (!payload.data || typeof payload.data !== "object") {
|
|
69
67
|
throw new Error("Invalid payload structure");
|
|
70
68
|
}
|
|
69
|
+
// 🔐 Fingerprint + store validation
|
|
71
70
|
if (options.fingerprint) {
|
|
72
|
-
|
|
73
|
-
const stored = payload.fp;
|
|
74
|
-
const matched = provided === stored;
|
|
75
|
-
if (!matched) {
|
|
71
|
+
if (payload.fp !== options.fingerprint) {
|
|
76
72
|
throw new Error("Fingerprint mismatch");
|
|
77
73
|
}
|
|
74
|
+
const store = (0, store_1.getStore)(options.store);
|
|
75
|
+
if (store) {
|
|
76
|
+
const valid = store.isValid(payload.data.userId, payload.fp);
|
|
77
|
+
if (!valid) {
|
|
78
|
+
throw new Error("Session revoked or invalid device");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
78
81
|
}
|
|
79
|
-
return payload;
|
|
82
|
+
return payload;
|
|
80
83
|
}
|