secure-web-token 1.0.5 → 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/dist/sign.d.ts +6 -19
- package/dist/sign.d.ts.map +1 -1
- package/dist/sign.js +25 -18
- 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 +16 -0
- package/dist/store/memoryStore.d.ts.map +1 -0
- package/dist/store/memoryStore.js +18 -0
- package/dist/store/types.d.ts +16 -0
- package/dist/store/types.d.ts.map +1 -0
- package/dist/store/types.js +2 -0
- package/dist/verify.d.ts +3 -9
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +21 -21
- package/package.json +1 -1
package/dist/sign.d.ts
CHANGED
|
@@ -1,29 +1,16 @@
|
|
|
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 a device ID
|
|
12
|
-
*/
|
|
13
4
|
fingerprint?: true;
|
|
5
|
+
store?: StoreType;
|
|
14
6
|
}
|
|
15
7
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* data: {...},
|
|
20
|
-
* iat,
|
|
21
|
-
* exp,
|
|
22
|
-
* fp
|
|
23
|
-
* }
|
|
8
|
+
* sign() now returns:
|
|
9
|
+
* - token (encrypted payload)
|
|
10
|
+
* - sessionId (to store in HttpOnly cookie)
|
|
24
11
|
*/
|
|
25
12
|
export default function sign(data: Record<string, any>, secret: string, options?: SignOptions): {
|
|
26
13
|
token: string;
|
|
27
|
-
|
|
14
|
+
sessionId?: string;
|
|
28
15
|
};
|
|
29
16
|
//# sourceMappingURL=sign.d.ts.map
|
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;;;;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"}
|
package/dist/sign.js
CHANGED
|
@@ -41,38 +41,45 @@ 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
|
+
const store_1 = require("./store");
|
|
44
45
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* data: {...},
|
|
49
|
-
* iat,
|
|
50
|
-
* exp,
|
|
51
|
-
* fp
|
|
52
|
-
* }
|
|
46
|
+
* sign() now returns:
|
|
47
|
+
* - token (encrypted payload)
|
|
48
|
+
* - sessionId (to store in HttpOnly cookie)
|
|
53
49
|
*/
|
|
54
50
|
function sign(data, secret, options = {}) {
|
|
55
|
-
if (!secret || typeof secret !== "string")
|
|
56
|
-
throw new Error("Secret
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
51
|
+
if (!secret || typeof secret !== "string")
|
|
52
|
+
throw new Error("Secret required");
|
|
53
|
+
if (!data || typeof data !== "object")
|
|
54
|
+
throw new Error("Data must be object");
|
|
55
|
+
if (!data.userId)
|
|
56
|
+
throw new Error("data.userId is required for session mode");
|
|
61
57
|
const now = Math.floor(Date.now() / 1000);
|
|
62
58
|
const payload = {
|
|
63
59
|
data,
|
|
64
60
|
iat: now,
|
|
65
|
-
exp: now + (options.expiresIn ?? 900)
|
|
61
|
+
exp: now + (options.expiresIn ?? 900),
|
|
66
62
|
};
|
|
63
|
+
let sessionId;
|
|
67
64
|
let deviceId;
|
|
68
|
-
//
|
|
65
|
+
// Backend-only device/session mode
|
|
69
66
|
if (options.fingerprint === true) {
|
|
70
67
|
deviceId = (0, device_1.generateDeviceId)();
|
|
71
68
|
payload.fp = deviceId;
|
|
69
|
+
sessionId = crypto.randomUUID();
|
|
70
|
+
const store = (0, store_1.getStore)(options.store);
|
|
71
|
+
if (store) {
|
|
72
|
+
store.registerSession({
|
|
73
|
+
sessionId,
|
|
74
|
+
userId: data.userId,
|
|
75
|
+
deviceId,
|
|
76
|
+
fingerprint: deviceId,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
72
79
|
}
|
|
73
80
|
const header = {
|
|
74
81
|
alg: "AES-256-GCM+HMAC",
|
|
75
|
-
typ: "SWT"
|
|
82
|
+
typ: "SWT",
|
|
76
83
|
};
|
|
77
84
|
const encodedHeader = (0, utils_1.base64urlEncode)(JSON.stringify(header));
|
|
78
85
|
const encryptedPayload = (0, encrypt_1.default)(payload, secret);
|
|
@@ -83,6 +90,6 @@ function sign(data, secret, options = {}) {
|
|
|
83
90
|
.digest("base64url");
|
|
84
91
|
return {
|
|
85
92
|
token: `${dataToSign}.${signature}`,
|
|
86
|
-
|
|
93
|
+
sessionId, // to set in HttpOnly cookie
|
|
87
94
|
};
|
|
88
95
|
}
|
|
@@ -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,16 @@
|
|
|
1
|
+
import { Store } from "./types";
|
|
2
|
+
interface Session {
|
|
3
|
+
sessionId: string;
|
|
4
|
+
userId: string | number;
|
|
5
|
+
deviceId: string;
|
|
6
|
+
fingerprint: string;
|
|
7
|
+
}
|
|
8
|
+
declare class MemoryStore implements Store {
|
|
9
|
+
private sessions;
|
|
10
|
+
registerSession(session: Session): void;
|
|
11
|
+
getSession(sessionId: string): Session | null;
|
|
12
|
+
revokeSession(sessionId: string): void;
|
|
13
|
+
}
|
|
14
|
+
export declare const memoryStore: MemoryStore;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=memoryStore.d.ts.map
|
|
@@ -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;AAEhC,UAAU,OAAO;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACvB;AAED,cAAM,WAAY,YAAW,KAAK;IAC9B,OAAO,CAAC,QAAQ,CAA8B;IAE9C,eAAe,CAAC,OAAO,EAAE,OAAO;IAIhC,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI;IAI7C,aAAa,CAAC,SAAS,EAAE,MAAM;CAGlC;AAED,eAAO,MAAM,WAAW,EAAE,WAA+B,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
registerSession(session) {
|
|
9
|
+
this.sessions.set(session.sessionId, session);
|
|
10
|
+
}
|
|
11
|
+
getSession(sessionId) {
|
|
12
|
+
return this.sessions.get(sessionId) || null;
|
|
13
|
+
}
|
|
14
|
+
revokeSession(sessionId) {
|
|
15
|
+
this.sessions.delete(sessionId);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.memoryStore = new MemoryStore();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Store {
|
|
2
|
+
registerSession(session: {
|
|
3
|
+
sessionId: string;
|
|
4
|
+
userId: string | number;
|
|
5
|
+
deviceId: string;
|
|
6
|
+
fingerprint: string;
|
|
7
|
+
}): void;
|
|
8
|
+
getSession(sessionId: string): {
|
|
9
|
+
sessionId: string;
|
|
10
|
+
userId: string | number;
|
|
11
|
+
deviceId: string;
|
|
12
|
+
fingerprint: string;
|
|
13
|
+
} | null;
|
|
14
|
+
revokeSession(sessionId: string): void;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/verify.d.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
* Options for verifying a Secure Web Token
|
|
3
|
-
*/
|
|
1
|
+
import { StoreType } from "./store";
|
|
4
2
|
export interface VerifyOptions {
|
|
5
|
-
|
|
6
|
-
* Device fingerprint for verification
|
|
7
|
-
*/
|
|
3
|
+
sessionId?: string;
|
|
8
4
|
fingerprint?: string;
|
|
5
|
+
store?: StoreType;
|
|
9
6
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Verifies and decrypts a Secure Web Token (SWT)
|
|
12
|
-
*/
|
|
13
7
|
export default function verify(token: string, secret: string, options?: VerifyOptions): Record<string, any>;
|
|
14
8
|
//# 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,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"}
|
package/dist/verify.js
CHANGED
|
@@ -40,39 +40,39 @@ 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 (SWT)
|
|
45
|
-
*/
|
|
43
|
+
const store_1 = require("./store");
|
|
46
44
|
function verify(token, secret, options = {}) {
|
|
47
|
-
if (!token || typeof token !== "string")
|
|
48
|
-
throw new Error("Token must be
|
|
49
|
-
}
|
|
45
|
+
if (!token || typeof token !== "string")
|
|
46
|
+
throw new Error("Token must be string");
|
|
50
47
|
const parts = token.split(".");
|
|
51
|
-
if (parts.length !== 3)
|
|
48
|
+
if (parts.length !== 3)
|
|
52
49
|
throw new Error("Invalid token format");
|
|
53
|
-
}
|
|
54
50
|
const [header, encryptedPayload, signature] = parts;
|
|
55
51
|
const dataToVerify = `${header}.${encryptedPayload}`;
|
|
56
52
|
const expectedSignature = crypto
|
|
57
53
|
.createHmac("sha256", secret)
|
|
58
54
|
.update(dataToVerify)
|
|
59
55
|
.digest("base64url");
|
|
60
|
-
if (!(0, utils_1.timingSafeEqual)(Buffer.from(signature), Buffer.from(expectedSignature)))
|
|
56
|
+
if (!(0, utils_1.timingSafeEqual)(Buffer.from(signature), Buffer.from(expectedSignature)))
|
|
61
57
|
throw new Error("Invalid signature");
|
|
62
|
-
}
|
|
63
58
|
const payload = (0, decrypt_1.default)(encryptedPayload, secret);
|
|
64
59
|
const now = Math.floor(Date.now() / 1000);
|
|
65
|
-
if (payload.exp < now)
|
|
60
|
+
if (payload.exp < now)
|
|
66
61
|
throw new Error("Token expired");
|
|
62
|
+
if (!payload.data || typeof payload.data !== "object")
|
|
63
|
+
throw new Error("Invalid payload");
|
|
64
|
+
// Server-side session verification
|
|
65
|
+
if (options.sessionId && options.fingerprint) {
|
|
66
|
+
const store = (0, store_1.getStore)(options.store);
|
|
67
|
+
if (!store)
|
|
68
|
+
throw new Error("No store available");
|
|
69
|
+
const session = store.getSession(options.sessionId);
|
|
70
|
+
if (!session)
|
|
71
|
+
throw new Error("Session revoked or invalid");
|
|
72
|
+
if (session.userId !== payload.data.userId)
|
|
73
|
+
throw new Error("User mismatch");
|
|
74
|
+
if (session.fingerprint !== options.fingerprint)
|
|
75
|
+
throw new Error("Device mismatch");
|
|
67
76
|
}
|
|
68
|
-
|
|
69
|
-
throw new Error("Invalid payload structure");
|
|
70
|
-
}
|
|
71
|
-
// 🔐 Single-device verification
|
|
72
|
-
if (options.fingerprint) {
|
|
73
|
-
if (payload.fp !== options.fingerprint) {
|
|
74
|
-
throw new Error("Fingerprint mismatch");
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return payload; // { data, iat, exp, fp }
|
|
77
|
+
return payload;
|
|
78
78
|
}
|