@maestro-js/signed-url 1.0.0-alpha.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/index.d.ts +67 -0
- package/dist/index.js +69 -0
- package/package.json +38 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Iso } from 'iso-fns2';
|
|
2
|
+
|
|
3
|
+
declare function create(config: SignedUrl.Provider.Config): SignedUrl.SignedUrlService;
|
|
4
|
+
declare function provider(key: SignedUrl.Provider.Key): SignedUrl.SignedUrlService;
|
|
5
|
+
type KeysWithFallback = keyof SignedUrl.Provider.Keys extends never ? {
|
|
6
|
+
default: unknown;
|
|
7
|
+
} : SignedUrl.Provider.Keys;
|
|
8
|
+
/**
|
|
9
|
+
* URL signing and verification backed by a hash service.
|
|
10
|
+
* Call methods directly on the default instance, or use `SignedUrl.Provider.create()` for named instances.
|
|
11
|
+
*/
|
|
12
|
+
declare const SignedUrl: {
|
|
13
|
+
provider: typeof provider;
|
|
14
|
+
/** Signs URL search params with an expiration duration (or `null` for never-expiring), returning new params with `_signature` and optionally `_expires` */
|
|
15
|
+
signURL: (params: URLSearchParams, expiration: Iso.Duration | null) => Promise<URLSearchParams>;
|
|
16
|
+
/** Checks whether signed URL search params have a valid signature — verifies expiration (if present) and the signature */
|
|
17
|
+
hasValidSignature: (params: URLSearchParams) => Promise<boolean>;
|
|
18
|
+
/** Checks whether the signature would be valid if the supplied search params were ignored */
|
|
19
|
+
hasValidSignatureWhileIgnoring: (params: URLSearchParams, ignoreParams: string[]) => Promise<boolean>;
|
|
20
|
+
Provider: {
|
|
21
|
+
create: typeof create;
|
|
22
|
+
register: (name: SignedUrl.Provider.Key, item: SignedUrl.SignedUrlService) => void;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* URL signing and verification backed by a hash service.
|
|
27
|
+
*
|
|
28
|
+
* Signs URL search params with an HMAC signature and an expiration timestamp,
|
|
29
|
+
* then verifies them on the receiving end. Accepts the `Hash` facade or any
|
|
30
|
+
* object that conforms to the `Hash.HashService` interface.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* SignedUrl.Provider.register('default', SignedUrl.Provider.create({ driver: Hash }))
|
|
35
|
+
*
|
|
36
|
+
* const signed = await SignedUrl.signURL(new URLSearchParams({ token: 'abc' }), 'PT1H')
|
|
37
|
+
* await SignedUrl.hasValidSignature(signed) // true
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare namespace SignedUrl {
|
|
41
|
+
/** Any object with `hash` and `verify` methods — the `Hash` facade or a `Hash.HashService` instance both satisfy this */
|
|
42
|
+
type Driver = {
|
|
43
|
+
hash: (value: string) => Promise<string>;
|
|
44
|
+
verify: (options: {
|
|
45
|
+
plaintext: string;
|
|
46
|
+
hash: string;
|
|
47
|
+
}) => Promise<boolean>;
|
|
48
|
+
};
|
|
49
|
+
namespace Provider {
|
|
50
|
+
type Key = keyof KeysWithFallback;
|
|
51
|
+
interface Keys {
|
|
52
|
+
}
|
|
53
|
+
interface Config {
|
|
54
|
+
driver: Driver;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
interface SignedUrlService {
|
|
58
|
+
/** Signs URL search params with an expiration duration (or `null` for never-expiring), returning new params with `_signature` and optionally `_expires` */
|
|
59
|
+
signURL: (params: URLSearchParams, expiration: Iso.Duration | null) => Promise<URLSearchParams>;
|
|
60
|
+
/** Checks whether signed URL search params have a valid signature — verifies expiration (if present) and the signature */
|
|
61
|
+
hasValidSignature: (params: URLSearchParams) => Promise<boolean>;
|
|
62
|
+
/** Checks whether the signature would be valid if the supplied search params were ignored */
|
|
63
|
+
hasValidSignatureWhileIgnoring: (params: URLSearchParams, ignoreParams: string[]) => Promise<boolean>;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { SignedUrl };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { ServiceRegistry } from "@maestro-js/service-registry";
|
|
3
|
+
import { instantFns } from "iso-fns2";
|
|
4
|
+
var SIGNATURE_PARAM = "_signature";
|
|
5
|
+
var EXPIRES_PARAM = "_expires";
|
|
6
|
+
function create(config) {
|
|
7
|
+
async function signURL(params, expiration) {
|
|
8
|
+
const signed = new URLSearchParams(params);
|
|
9
|
+
signed.delete(SIGNATURE_PARAM);
|
|
10
|
+
signed.delete(EXPIRES_PARAM);
|
|
11
|
+
if (expiration !== null) {
|
|
12
|
+
const expiresAt = instantFns.add(instantFns.now(), expiration);
|
|
13
|
+
signed.set(EXPIRES_PARAM, expiresAt);
|
|
14
|
+
}
|
|
15
|
+
const canonical = buildCanonicalString(signed);
|
|
16
|
+
const signature = await config.driver.hash(canonical);
|
|
17
|
+
signed.set(SIGNATURE_PARAM, signature);
|
|
18
|
+
return signed;
|
|
19
|
+
}
|
|
20
|
+
async function hasValidSignature(params) {
|
|
21
|
+
const copy = new URLSearchParams(params);
|
|
22
|
+
const signature = copy.get(SIGNATURE_PARAM);
|
|
23
|
+
const expires = copy.get(EXPIRES_PARAM);
|
|
24
|
+
if (!signature) return false;
|
|
25
|
+
if (expires && instantFns.now() >= expires) return false;
|
|
26
|
+
copy.delete(SIGNATURE_PARAM);
|
|
27
|
+
const canonical = buildCanonicalString(copy);
|
|
28
|
+
return config.driver.verify({ plaintext: canonical, hash: signature });
|
|
29
|
+
}
|
|
30
|
+
async function hasValidSignatureWhileIgnoring(params, ignoreParams) {
|
|
31
|
+
const copy = new URLSearchParams(params);
|
|
32
|
+
for (const param of ignoreParams) {
|
|
33
|
+
copy.delete(param);
|
|
34
|
+
}
|
|
35
|
+
return hasValidSignature(copy);
|
|
36
|
+
}
|
|
37
|
+
function buildCanonicalString(params) {
|
|
38
|
+
params.sort();
|
|
39
|
+
return params.toString();
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
signURL,
|
|
43
|
+
hasValidSignature,
|
|
44
|
+
hasValidSignatureWhileIgnoring
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
var registry = ServiceRegistry.createRegistry(
|
|
48
|
+
ServiceRegistry.proxyFunctionsForObject
|
|
49
|
+
);
|
|
50
|
+
var Provider = {
|
|
51
|
+
create,
|
|
52
|
+
register: registry.register
|
|
53
|
+
};
|
|
54
|
+
function provider(key) {
|
|
55
|
+
const service = registry.resolve(key);
|
|
56
|
+
return {
|
|
57
|
+
signURL: service.signURL,
|
|
58
|
+
hasValidSignature: service.hasValidSignature,
|
|
59
|
+
hasValidSignatureWhileIgnoring: service.hasValidSignatureWhileIgnoring
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
var SignedUrl = {
|
|
63
|
+
Provider,
|
|
64
|
+
...provider("default"),
|
|
65
|
+
provider
|
|
66
|
+
};
|
|
67
|
+
export {
|
|
68
|
+
SignedUrl
|
|
69
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@maestro-js/signed-url",
|
|
3
|
+
"description": "URL signing and verification with pluggable hash drivers for @maestro-js/signed-url. Use when generating tamper-proof signed URLs with expiration (e.g. file download links, email verification) or validating signed URL parameters. Accepts any hash driver compatible with @maestro-js/hash.",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"default": "./dist/index.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"iso-fns2": "npm:iso-fns@2.0.0-alpha.26",
|
|
13
|
+
"@maestro-js/service-registry": "1.0.0-alpha.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^22.19.11",
|
|
17
|
+
"@maestro-js/hash": "1.0.0-alpha.0"
|
|
18
|
+
},
|
|
19
|
+
"version": "1.0.0-alpha.0",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "restricted"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"license": "UNLICENSED",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=22.18.0"
|
|
29
|
+
},
|
|
30
|
+
"repository": "https://github.com/Marcato-Partners/maestro-js",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"test": "beartest ./tests/**/*",
|
|
35
|
+
"format": "prettier --write src/ tests/",
|
|
36
|
+
"lint": "prettier --check src/ tests/"
|
|
37
|
+
}
|
|
38
|
+
}
|