secure-web-token 1.2.0 → 1.2.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/README.md +29 -116
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,129 +1,42 @@
|
|
|
1
1
|
# 🔐 Secure Web Token (SWT)
|
|
2
2
|
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
##
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
**Secure Web Token (SWT)** solves these problems by:
|
|
21
|
-
|
|
22
|
-
- Encrypting the payload using **AES-256-GCM**
|
|
23
|
-
- Making payload data **completely unreadable without the secret**
|
|
24
|
-
- Allowing tokens to be bound to **one or more device fingerprints**
|
|
25
|
-
- Preventing token reuse from unauthorized devices
|
|
26
|
-
- Supporting auto-generated device IDs for stronger protection
|
|
27
|
-
|
|
28
|
-
This makes SWT especially useful for:
|
|
29
|
-
- Course platforms (anti-piracy)
|
|
30
|
-
- SaaS dashboards
|
|
31
|
-
- Admin panels
|
|
32
|
-
- Device-restricted systems
|
|
33
|
-
|
|
34
|
-
---
|
|
35
|
-
|
|
36
|
-
## 3. Available Functions
|
|
37
|
-
|
|
38
|
-
### `sign()`
|
|
39
|
-
Creates an encrypted and signed token.
|
|
40
|
-
|
|
41
|
-
**Features:**
|
|
42
|
-
- Encrypts payload
|
|
43
|
-
- Adds expiry (`iat`, `exp`)
|
|
44
|
-
- Supports device fingerprint binding
|
|
45
|
-
- Can auto-generate a device ID
|
|
46
|
-
|
|
47
|
-
---
|
|
3
|
+
## About the Package
|
|
4
|
+
SWT is a secure token system that encrypts payloads and binds them to devices. Unlike JWT, it prevents token reuse on other devices.
|
|
5
|
+
|
|
6
|
+
## What Problem Does It Solve?
|
|
7
|
+
- JWT can be decoded easily.
|
|
8
|
+
- Tokens can be used on any device if leaked.
|
|
9
|
+
- SWT encrypts payloads and binds tokens to device fingerprints.
|
|
10
|
+
|
|
11
|
+
## Functions
|
|
12
|
+
|
|
13
|
+
### sign()
|
|
14
|
+
Creates a secure token.
|
|
15
|
+
```ts
|
|
16
|
+
import { sign } from "secure-web-token";
|
|
17
|
+
const { token, deviceId } = sign({ userId:1 }, "secret", { fingerprint: true });
|
|
18
|
+
console.log(token, deviceId);
|
|
19
|
+
```
|
|
48
20
|
|
|
49
|
-
###
|
|
21
|
+
### verify()
|
|
50
22
|
Verifies and decrypts a token.
|
|
23
|
+
```ts
|
|
24
|
+
import { verify } from "secure-web-token";
|
|
25
|
+
const payload = verify(token, "secret", { fingerprint: deviceId });
|
|
26
|
+
console.log(payload.data);
|
|
27
|
+
```
|
|
51
28
|
|
|
52
|
-
|
|
53
|
-
- Token format
|
|
54
|
-
- Signature integrity
|
|
55
|
-
- Token expiry
|
|
56
|
-
- Device fingerprint validation
|
|
57
|
-
|
|
58
|
-
---
|
|
59
|
-
|
|
60
|
-
## 4. Sample Code
|
|
61
|
-
|
|
62
|
-
### Installation
|
|
63
|
-
|
|
29
|
+
## Installation
|
|
64
30
|
```bash
|
|
65
31
|
npm install secure-web-token
|
|
66
32
|
```
|
|
67
33
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
```js
|
|
34
|
+
## Importing
|
|
35
|
+
```ts
|
|
36
|
+
import { sign, verify } from "secure-web-token";
|
|
37
|
+
// or
|
|
73
38
|
const { sign, verify } = require("secure-web-token");
|
|
74
39
|
```
|
|
75
40
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
### Signing a Token (Auto Device Registration)
|
|
79
|
-
|
|
80
|
-
```js
|
|
81
|
-
const secret = "my-super-secret";
|
|
82
|
-
|
|
83
|
-
const { token, deviceId } = sign(
|
|
84
|
-
{ userId: 1, role: "admin" },
|
|
85
|
-
secret,
|
|
86
|
-
{ fingerprint: true }
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
console.log("TOKEN:", token);
|
|
90
|
-
console.log("DEVICE ID:", deviceId);
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
---
|
|
94
|
-
|
|
95
|
-
### Verifying the Token
|
|
96
|
-
|
|
97
|
-
```js
|
|
98
|
-
try {
|
|
99
|
-
const payload = verify(token, secret, {
|
|
100
|
-
fingerprint: deviceId
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
console.log("USER DATA:", payload.data);
|
|
104
|
-
} catch (err) {
|
|
105
|
-
console.error("AUTH ERROR:", err.message);
|
|
106
|
-
}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
---
|
|
110
|
-
|
|
111
|
-
## Payload Structure (Internal)
|
|
112
|
-
|
|
113
|
-
```js
|
|
114
|
-
{
|
|
115
|
-
data: {
|
|
116
|
-
userId: 1,
|
|
117
|
-
role: "admin"
|
|
118
|
-
},
|
|
119
|
-
iat: 1768368114,
|
|
120
|
-
exp: 1768369014,
|
|
121
|
-
fp: ["device-id"]
|
|
122
|
-
}
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
---
|
|
126
|
-
|
|
127
|
-
## License
|
|
41
|
+
This simple version focuses on **encrypted tokens and device binding**.
|
|
128
42
|
|
|
129
|
-
MIT License
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export { default as sign } from "./sign";
|
|
6
6
|
export { default as verify } from "./verify";
|
|
7
|
+
export { getStore, StoreType } from "./store";
|
|
7
8
|
export type { SignOptions } from "./sign";
|
|
8
9
|
export type { VerifyOptions } from "./verify";
|
|
10
|
+
export type { Store } from "./store/types";
|
|
9
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,QAAQ,CAAC;AACzC,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,QAAQ,CAAC;AACzC,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAG7C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAG9C,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAC1C,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,YAAY,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7,8 +7,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
7
7
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
8
|
};
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.verify = exports.sign = void 0;
|
|
10
|
+
exports.getStore = exports.verify = exports.sign = void 0;
|
|
11
11
|
var sign_1 = require("./sign");
|
|
12
12
|
Object.defineProperty(exports, "sign", { enumerable: true, get: function () { return __importDefault(sign_1).default; } });
|
|
13
13
|
var verify_1 = require("./verify");
|
|
14
14
|
Object.defineProperty(exports, "verify", { enumerable: true, get: function () { return __importDefault(verify_1).default; } });
|
|
15
|
+
// Export store helpers
|
|
16
|
+
var store_1 = require("./store");
|
|
17
|
+
Object.defineProperty(exports, "getStore", { enumerable: true, get: function () { return store_1.getStore; } });
|