@tozielinski/next-upstash-nonce 1.2.2 → 1.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.3.0](https://github.com/tozielinski/next-upstash-nonce/compare/v1.2.3...v1.3.0) (2025-11-22)
4
+
5
+
6
+ ### Features
7
+
8
+ * create two new methods, to check nonces in the header of a request [verifyNonceFromRequest(), verifyAndDeleteNonceFromRequest()] ([a768479](https://github.com/tozielinski/next-upstash-nonce/commit/a76847993bb7fe66bf21900d83eb1ca5760d8afa))
9
+
10
+ ## [1.2.3](https://github.com/tozielinski/next-upstash-nonce/compare/v1.2.2...v1.2.3) (2025-11-21)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * dist folder was missing on build ([8afc1d5](https://github.com/tozielinski/next-upstash-nonce/commit/8afc1d5a0add63b1779a08cc4b1e4249ba3cfed9))
16
+
3
17
  ## [1.2.2](https://github.com/tozielinski/next-upstash-nonce/compare/v1.2.1...v1.2.2) (2025-11-21)
4
18
 
5
19
 
package/dist/index.js ADDED
@@ -0,0 +1,128 @@
1
+ 'use server';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.NonceManager = void 0;
5
+ const uuid_1 = require("uuid");
6
+ class NonceManager {
7
+ redis;
8
+ length;
9
+ ttlSeconds;
10
+ prefix;
11
+ constructor(redis, opts = {}) {
12
+ this.redis = redis;
13
+ this.length = opts.length ?? 32; // default 32 bytes -> 64 hex chars
14
+ this.ttlSeconds = opts.ttlSeconds ?? 60 * 5; // default 5 minutes
15
+ this.prefix = opts.prefix ?? "nonce:";
16
+ }
17
+ /**
18
+ * generates a new, secure nonce,
19
+ * inserts it into Redis with a TTL,
20
+ * and returns the nonce string.
21
+ */
22
+ async create() {
23
+ const nonce = (0, uuid_1.v4)();
24
+ const key = this.prefix + nonce;
25
+ await this.redis.set(key, "1", { ex: this.ttlSeconds });
26
+ return nonce;
27
+ }
28
+ /**
29
+ * verifies a nonce and deletes it from Redis,
30
+ * returning true if the nonce exists and has not expired.
31
+ */
32
+ async verify(nonce) {
33
+ if (!nonce)
34
+ return false;
35
+ try {
36
+ const res = await this.redis.get(`nonce:${nonce}`);
37
+ return res !== null;
38
+ }
39
+ catch (err) {
40
+ console.error("verify error:", err);
41
+ return false;
42
+ }
43
+ }
44
+ /**
45
+ * verifies a nonce and deletes it from Redis,
46
+ * returning true if the nonce exists and has not expired.
47
+ */
48
+ async verifyAndDelete(nonce) {
49
+ if (!nonce)
50
+ return false;
51
+ try {
52
+ const res = await this.redis.get(`nonce:${nonce}`);
53
+ // const res = await (this.redis as any).eval(script, { keys: [key] });
54
+ if (res)
55
+ await this.redis.del(`nonce:${nonce}`);
56
+ return res !== null;
57
+ }
58
+ catch (err) {
59
+ console.error("verifyAndDelete error:", err);
60
+ return false;
61
+ }
62
+ }
63
+ /**
64
+ * verifies a nonce from the Header of a request
65
+ * returns a NonceCheckResult if the nonce exists and has not expired.
66
+ */
67
+ async verifyNonceFromRequest(req) {
68
+ const nonce = req.headers.get("x-api-nonce");
69
+ if (!nonce) {
70
+ const response = Response.json({ error: "Missing x-api-nonce header" }, { status: 403 });
71
+ return {
72
+ valid: false,
73
+ reason: "missing-header",
74
+ response,
75
+ };
76
+ }
77
+ const valid = await this.verify(nonce);
78
+ if (!valid) {
79
+ const response = Response.json({ error: "Invalid or expired nonce" }, { status: 403 });
80
+ return {
81
+ valid: false,
82
+ reason: "invalid-or-expired",
83
+ response,
84
+ };
85
+ }
86
+ return {
87
+ valid: true,
88
+ nonce,
89
+ };
90
+ }
91
+ /**
92
+ * verifies a nonce from the Header of a request and deletes it from Redis
93
+ * returns a NonceCheckResult if the nonce exists and has not expired.
94
+ */
95
+ async verifyAndDeleteNonceFromRequest(req) {
96
+ const nonce = req.headers.get("x-api-nonce");
97
+ if (!nonce) {
98
+ const response = Response.json({ error: "Missing x-api-nonce header" }, { status: 403 });
99
+ return {
100
+ valid: false,
101
+ reason: "missing-header",
102
+ response,
103
+ };
104
+ }
105
+ const valid = await this.verifyAndDelete(nonce);
106
+ if (!valid) {
107
+ const response = Response.json({ error: "Invalid or expired nonce" }, { status: 403 });
108
+ return {
109
+ valid: false,
110
+ reason: "invalid-or-expired",
111
+ response,
112
+ };
113
+ }
114
+ return {
115
+ valid: true,
116
+ nonce,
117
+ };
118
+ }
119
+ /**
120
+ * Optional: delete a nonce from Redis manually
121
+ */
122
+ async delete(nonce) {
123
+ const key = this.prefix + nonce;
124
+ await this.redis.del(key);
125
+ }
126
+ }
127
+ exports.NonceManager = NonceManager;
128
+ exports.default = NonceManager;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@tozielinski/next-upstash-nonce",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "Create, store, verify and delete nonces in Redis by Upstash for Next.js",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -32,7 +32,6 @@
32
32
  "homepage": "https://github.com/tozielinski/next-upstash-nonce#readme",
33
33
  "dependencies": {
34
34
  "@upstash/redis": "1.22.0",
35
- "crypto": "^1.0.1",
36
35
  "uuid": "^13.0.0"
37
36
  },
38
37
  "devDependencies": {
package/src/index.js DELETED
@@ -1,70 +0,0 @@
1
- 'use server';
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.NonceManager = void 0;
5
- const uuid_1 = require("uuid");
6
- class NonceManager {
7
- redis;
8
- length;
9
- ttlSeconds;
10
- prefix;
11
- constructor(redis, opts = {}) {
12
- this.redis = redis;
13
- this.length = opts.length ?? 32; // default 32 bytes -> 64 hex chars
14
- this.ttlSeconds = opts.ttlSeconds ?? 60 * 5; // default 5 minutes
15
- this.prefix = opts.prefix ?? "nonce:";
16
- }
17
- /**
18
- * generates a new, secure nonce,
19
- * inserts it into Redis with a TTL,
20
- * and returns the nonce string.
21
- */
22
- async create() {
23
- // const buffer = crypto.randomBytes(this.length);
24
- // const nonce = buffer.toString("hex");
25
- const nonce = (0, uuid_1.v4)();
26
- const key = this.prefix + nonce;
27
- // console.log("creating nonce:", nonce);
28
- // set with ttl (nx not required — collisions extremely unlikely)
29
- await this.redis.set(key, "1", { ex: this.ttlSeconds });
30
- return nonce;
31
- }
32
- /**
33
- * verifies a nonce and deletes it from Redis,
34
- * returning true if the nonce exists and has not expired.
35
- */
36
- async verifyAndDelete(nonce) {
37
- if (!nonce)
38
- return false;
39
- // const key = this.prefix + nonce;
40
- //
41
- // const script = `
42
- // local v = redis.call('GET', KEYS[1])
43
- // if v then
44
- // redis.call('DEL', KEYS[1])
45
- // return v
46
- // end
47
- // return nil
48
- // `;
49
- try {
50
- const res = await this.redis.get(`nonce:${nonce}`);
51
- // const res = await (this.redis as any).eval(script, { keys: [key] });
52
- if (res)
53
- await this.redis.del(`nonce:${nonce}`);
54
- return res !== null;
55
- }
56
- catch (err) {
57
- console.error("verifyAndDelete error:", err);
58
- return false;
59
- }
60
- }
61
- /**
62
- * Optional: delete a nonce from Redis manually
63
- */
64
- async delete(nonce) {
65
- const key = this.prefix + nonce;
66
- await this.redis.del(key);
67
- }
68
- }
69
- exports.NonceManager = NonceManager;
70
- exports.default = NonceManager;
package/src/index.ts DELETED
@@ -1,85 +0,0 @@
1
- 'use server'
2
-
3
- import { Redis } from "@upstash/redis";
4
- import {v4 as uuid} from "uuid";
5
- // import crypto from "crypto";
6
-
7
- export type NonceOptions = {
8
- length?: number; // bytes
9
- ttlSeconds?: number; // Time-to-live in Redis
10
- prefix?: string;
11
- };
12
-
13
- export class NonceManager {
14
- private redis: Redis;
15
- private length: number;
16
- private ttlSeconds: number;
17
- private prefix: string;
18
-
19
-
20
- constructor(redis: Redis, opts: NonceOptions = {}) {
21
- this.redis = redis;
22
- this.length = opts.length ?? 32; // default 32 bytes -> 64 hex chars
23
- this.ttlSeconds = opts.ttlSeconds ?? 60 * 5; // default 5 minutes
24
- this.prefix = opts.prefix ?? "nonce:";
25
- }
26
-
27
-
28
- /**
29
- * generates a new, secure nonce,
30
- * inserts it into Redis with a TTL,
31
- * and returns the nonce string.
32
- */
33
- async create(): Promise<string> {
34
- // const buffer = crypto.randomBytes(this.length);
35
- // const nonce = buffer.toString("hex");
36
- const nonce = uuid();
37
- const key = this.prefix + nonce;
38
-
39
- // console.log("creating nonce:", nonce);
40
- // set with ttl (nx not required — collisions extremely unlikely)
41
- await this.redis.set(key, "1", { ex: this.ttlSeconds });
42
- return nonce;
43
- }
44
-
45
-
46
- /**
47
- * verifies a nonce and deletes it from Redis,
48
- * returning true if the nonce exists and has not expired.
49
- */
50
- async verifyAndDelete(nonce: string): Promise<boolean> {
51
- if (!nonce) return false;
52
- // const key = this.prefix + nonce;
53
- //
54
- // const script = `
55
- // local v = redis.call('GET', KEYS[1])
56
- // if v then
57
- // redis.call('DEL', KEYS[1])
58
- // return v
59
- // end
60
- // return nil
61
- // `;
62
-
63
- try {
64
- const res = await (this.redis as any).get(`nonce:${nonce}`);
65
- // const res = await (this.redis as any).eval(script, { keys: [key] });
66
- if (res) await this.redis.del(`nonce:${nonce}`);
67
- return res !== null;
68
- } catch (err) {
69
- console.error("verifyAndDelete error:", err);
70
- return false;
71
- }
72
- }
73
-
74
-
75
- /**
76
- * Optional: delete a nonce from Redis manually
77
- */
78
- async delete(nonce: string): Promise<void> {
79
- const key = this.prefix + nonce;
80
- await this.redis.del(key);
81
- }
82
- }
83
-
84
-
85
- export default NonceManager;
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "esModuleInterop": true,
7
- "forceConsistentCasingInFileNames": true,
8
- "strict": true,
9
- "skipLibCheck": true,
10
- "types": ["node"]
11
- },
12
- "include": ["src"]
13
- }