altcha-lib 2.0.3 → 2.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.
@@ -21,7 +21,7 @@ function create(options) {
21
21
  hmacKeySignatureSecret,
22
22
  ...createChallengeParameters(),
23
23
  });
24
- res.json({
24
+ res.set('Cache-Control', 'no-store').json({
25
25
  configuration: setCookie
26
26
  ? {
27
27
  setCookie,
@@ -55,8 +55,7 @@ function create(options) {
55
55
  res.clearCookie(setCookie.name);
56
56
  }
57
57
  if (error && throwOnFailure) {
58
- res.status(403).json({ error });
59
- return;
58
+ throw Object.assign(new Error(error), { status: 403 });
60
59
  }
61
60
  next();
62
61
  });
@@ -14,7 +14,7 @@ export declare function create(options: AltchaOptions): {
14
14
  challengeHandler: (request: FastifyRequest, reply: FastifyReply) => Promise<never>;
15
15
  verifyHandler: (request: FastifyRequest, reply: FastifyReply) => Promise<never>;
16
16
  getPayloadFromRequest: (request: FastifyRequest, cookieName?: string) => Promise<string | undefined>;
17
- middleware: (options?: AltchaMiddlewareOptions) => (request: FastifyRequest, reply: FastifyReply) => Promise<undefined>;
17
+ middleware: (options?: AltchaMiddlewareOptions) => (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
18
18
  verify: typeof verify;
19
19
  };
20
20
  declare const _default: {
@@ -40,7 +40,7 @@ function create(options) {
40
40
  hmacKeySignatureSecret,
41
41
  ...createChallengeParameters(),
42
42
  });
43
- return reply.send({
43
+ return reply.header('Cache-Control', 'no-store').send({
44
44
  configuration: setCookie
45
45
  ? {
46
46
  setCookie,
@@ -68,7 +68,7 @@ function create(options) {
68
68
  deleteCookie(reply, setCookie.name, setCookie.path);
69
69
  }
70
70
  if (error && throwOnFailure) {
71
- return reply.code(403).send({ error });
71
+ throw Object.assign(new Error(error), { statusCode: 403 });
72
72
  }
73
73
  };
74
74
  };
@@ -12,7 +12,8 @@ const shared_js_1 = require("./shared.js");
12
12
  Object.defineProperty(exports, "deriveHmacKeySecret", { enumerable: true, get: function () { return shared_js_1.deriveHmacKeySecret; } });
13
13
  function create(options) {
14
14
  const { createChallengeParameters, deriveKey, fieldName = 'altcha', hmacSignatureSecret, hmacKeySignatureSecret, setCookie, store, } = options;
15
- const challengeHandler = (0, h3_1.defineEventHandler)(async () => {
15
+ const challengeHandler = (0, h3_1.defineEventHandler)(async (event) => {
16
+ (0, h3_1.setResponseHeader)(event, 'Cache-Control', 'no-store');
16
17
  return {
17
18
  configuration: setCookie
18
19
  ? {
@@ -14,6 +14,7 @@ Object.defineProperty(exports, "deriveHmacKeySecret", { enumerable: true, get: f
14
14
  function create(options) {
15
15
  const { createChallengeParameters, deriveKey, fieldName = 'altcha', hmacSignatureSecret, hmacKeySignatureSecret, setCookie, store, } = options;
16
16
  const challengeHandler = async (c) => {
17
+ c.header('Cache-Control', 'no-store');
17
18
  return c.json({
18
19
  configuration: setCookie
19
20
  ? {
@@ -116,6 +116,7 @@ let AltchaController = class AltchaController {
116
116
  exports.AltchaController = AltchaController;
117
117
  __decorate([
118
118
  (0, common_1.Get)('challenge'),
119
+ (0, common_1.Header)('Cache-Control', 'no-store'),
119
120
  __metadata("design:type", Function),
120
121
  __metadata("design:paramtypes", []),
121
122
  __metadata("design:returntype", Promise)
@@ -38,7 +38,9 @@ function create(options) {
38
38
  : undefined,
39
39
  ...challenge,
40
40
  };
41
- const response = Response.json(body);
41
+ const response = Response.json(body, {
42
+ headers: { 'Cache-Control': 'no-store' },
43
+ });
42
44
  return response;
43
45
  }
44
46
  async function verifyHandler(req) {
@@ -42,7 +42,7 @@ function create(options) {
42
42
  }
43
43
  : undefined,
44
44
  ...challenge,
45
- });
45
+ }, { headers: { 'Cache-Control': 'no-store' } });
46
46
  }
47
47
  async function verifyHandler(event) {
48
48
  const payload = await getPayloadFromEvent(event, fieldName);
@@ -17,8 +17,6 @@ export declare function delay(ms: number): Promise<void>;
17
17
  export declare function hash(algorithm: string, data: ArrayBuffer | string): Promise<Uint8Array>;
18
18
  /** Computes an HMAC signature using the Web Crypto API. */
19
19
  export declare function hmac(algorithm: HmacAlgorithm, data: string | Uint8Array, keyStr: string): Promise<Uint8Array>;
20
- /** Inject CSS tag into the document */
21
- export declare function injectCss(css: string, id?: string): void;
22
20
  /** Generates a random integer between the specified minimum and maximum values (inclusive). */
23
21
  export declare function randomInt(max: number, min?: number): number;
24
22
  /** Recursively sorts object keys alphabetically for deterministic serialization. */
@@ -9,7 +9,6 @@ exports.constantTimeEqual = constantTimeEqual;
9
9
  exports.delay = delay;
10
10
  exports.hash = hash;
11
11
  exports.hmac = hmac;
12
- exports.injectCss = injectCss;
13
12
  exports.randomInt = randomInt;
14
13
  exports.sortKeys = sortKeys;
15
14
  exports.timeDuration = timeDuration;
@@ -88,17 +87,6 @@ async function hmac(algorithm, data, keyStr) {
88
87
  : data);
89
88
  return new Uint8Array(signature);
90
89
  }
91
- /** Inject CSS tag into the document */
92
- function injectCss(css, id = 'altcha-css') {
93
- if (typeof document !== 'undefined' &&
94
- document &&
95
- !document.getElementById(id)) {
96
- const style = document.createElement('style');
97
- style.id = id;
98
- style.textContent = css;
99
- document.head.appendChild(style);
100
- }
101
- }
102
90
  /** Generates a random integer between the specified minimum and maximum values (inclusive). */
103
91
  function randomInt(max, min = 1) {
104
92
  const ab = new Uint32Array(1);
@@ -15,7 +15,7 @@ export function create(options) {
15
15
  hmacKeySignatureSecret,
16
16
  ...createChallengeParameters(),
17
17
  });
18
- res.json({
18
+ res.set('Cache-Control', 'no-store').json({
19
19
  configuration: setCookie
20
20
  ? {
21
21
  setCookie,
@@ -49,8 +49,7 @@ export function create(options) {
49
49
  res.clearCookie(setCookie.name);
50
50
  }
51
51
  if (error && throwOnFailure) {
52
- res.status(403).json({ error });
53
- return;
52
+ throw Object.assign(new Error(error), { status: 403 });
54
53
  }
55
54
  next();
56
55
  });
@@ -14,7 +14,7 @@ export declare function create(options: AltchaOptions): {
14
14
  challengeHandler: (request: FastifyRequest, reply: FastifyReply) => Promise<never>;
15
15
  verifyHandler: (request: FastifyRequest, reply: FastifyReply) => Promise<never>;
16
16
  getPayloadFromRequest: (request: FastifyRequest, cookieName?: string) => Promise<string | undefined>;
17
- middleware: (options?: AltchaMiddlewareOptions) => (request: FastifyRequest, reply: FastifyReply) => Promise<undefined>;
17
+ middleware: (options?: AltchaMiddlewareOptions) => (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
18
18
  verify: typeof verify;
19
19
  };
20
20
  declare const _default: {
@@ -34,7 +34,7 @@ export function create(options) {
34
34
  hmacKeySignatureSecret,
35
35
  ...createChallengeParameters(),
36
36
  });
37
- return reply.send({
37
+ return reply.header('Cache-Control', 'no-store').send({
38
38
  configuration: setCookie
39
39
  ? {
40
40
  setCookie,
@@ -62,7 +62,7 @@ export function create(options) {
62
62
  deleteCookie(reply, setCookie.name, setCookie.path);
63
63
  }
64
64
  if (error && throwOnFailure) {
65
- return reply.code(403).send({ error });
65
+ throw Object.assign(new Error(error), { statusCode: 403 });
66
66
  }
67
67
  };
68
68
  };
@@ -1,4 +1,4 @@
1
- import { HTTPError, defineEventHandler, getCookie, deleteCookie, readBody, } from 'h3';
1
+ import { HTTPError, defineEventHandler, getCookie, deleteCookie, readBody, setResponseHeader, } from 'h3';
2
2
  import { createChallenge } from '../pow.js';
3
3
  import { randomInt } from '../helpers.js';
4
4
  import { CappedMap } from '../capped-map.js';
@@ -6,7 +6,8 @@ import { deriveHmacKeySecret, verify } from './shared.js';
6
6
  export { CappedMap, deriveHmacKeySecret, randomInt };
7
7
  export function create(options) {
8
8
  const { createChallengeParameters, deriveKey, fieldName = 'altcha', hmacSignatureSecret, hmacKeySignatureSecret, setCookie, store, } = options;
9
- const challengeHandler = defineEventHandler(async () => {
9
+ const challengeHandler = defineEventHandler(async (event) => {
10
+ setResponseHeader(event, 'Cache-Control', 'no-store');
10
11
  return {
11
12
  configuration: setCookie
12
13
  ? {
@@ -8,6 +8,7 @@ export { CappedMap, deriveHmacKeySecret, randomInt };
8
8
  export function create(options) {
9
9
  const { createChallengeParameters, deriveKey, fieldName = 'altcha', hmacSignatureSecret, hmacKeySignatureSecret, setCookie, store, } = options;
10
10
  const challengeHandler = async (c) => {
11
+ c.header('Cache-Control', 'no-store');
11
12
  return c.json({
12
13
  configuration: setCookie
13
14
  ? {
@@ -11,7 +11,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
11
  return function (target, key) { decorator(target, key, paramIndex); }
12
12
  };
13
13
  var AltchaModule_1;
14
- import { Module, Controller, Get, Post, Req, Injectable, Inject, HttpException, HttpStatus, createParamDecorator, } from '@nestjs/common';
14
+ import { Module, Controller, Get, Post, Req, Header, Injectable, Inject, HttpException, HttpStatus, createParamDecorator, } from '@nestjs/common';
15
15
  import { randomInt } from '../helpers.js';
16
16
  import { createChallenge } from '../pow.js';
17
17
  import { deriveHmacKeySecret, verify } from './shared.js';
@@ -118,6 +118,7 @@ let AltchaController = class AltchaController {
118
118
  };
119
119
  __decorate([
120
120
  Get('challenge'),
121
+ Header('Cache-Control', 'no-store'),
121
122
  __metadata("design:type", Function),
122
123
  __metadata("design:paramtypes", []),
123
124
  __metadata("design:returntype", Promise)
@@ -32,7 +32,9 @@ export function create(options) {
32
32
  : undefined,
33
33
  ...challenge,
34
34
  };
35
- const response = Response.json(body);
35
+ const response = Response.json(body, {
36
+ headers: { 'Cache-Control': 'no-store' },
37
+ });
36
38
  return response;
37
39
  }
38
40
  async function verifyHandler(req) {
@@ -36,7 +36,7 @@ export function create(options) {
36
36
  }
37
37
  : undefined,
38
38
  ...challenge,
39
- });
39
+ }, { headers: { 'Cache-Control': 'no-store' } });
40
40
  }
41
41
  async function verifyHandler(event) {
42
42
  const payload = await getPayloadFromEvent(event, fieldName);
@@ -17,8 +17,6 @@ export declare function delay(ms: number): Promise<void>;
17
17
  export declare function hash(algorithm: string, data: ArrayBuffer | string): Promise<Uint8Array>;
18
18
  /** Computes an HMAC signature using the Web Crypto API. */
19
19
  export declare function hmac(algorithm: HmacAlgorithm, data: string | Uint8Array, keyStr: string): Promise<Uint8Array>;
20
- /** Inject CSS tag into the document */
21
- export declare function injectCss(css: string, id?: string): void;
22
20
  /** Generates a random integer between the specified minimum and maximum values (inclusive). */
23
21
  export declare function randomInt(max: number, min?: number): number;
24
22
  /** Recursively sorts object keys alphabetically for deterministic serialization. */
@@ -73,17 +73,6 @@ export async function hmac(algorithm, data, keyStr) {
73
73
  : data);
74
74
  return new Uint8Array(signature);
75
75
  }
76
- /** Inject CSS tag into the document */
77
- export function injectCss(css, id = 'altcha-css') {
78
- if (typeof document !== 'undefined' &&
79
- document &&
80
- !document.getElementById(id)) {
81
- const style = document.createElement('style');
82
- style.id = id;
83
- style.textContent = css;
84
- document.head.appendChild(style);
85
- }
86
- }
87
76
  /** Generates a random integer between the specified minimum and maximum values (inclusive). */
88
77
  export function randomInt(max, min = 1) {
89
78
  const ab = new Uint32Array(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "altcha-lib",
3
- "version": "2.0.3",
3
+ "version": "2.2.0",
4
4
  "description": "A lightweight library for creating and verifying ALTCHA challenges on the server.",
5
5
  "author": {
6
6
  "name": "Daniel Regeci",