applesauce-common 6.1.0 → 6.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/casts/trusted-assertions.d.ts +2 -2
- package/dist/casts/trusted-assertions.js +1 -1
- package/dist/factories/index.d.ts +1 -0
- package/dist/factories/index.js +1 -0
- package/dist/factories/nostr-web-token.d.ts +39 -0
- package/dist/factories/nostr-web-token.js +95 -0
- package/dist/helpers/external-id.d.ts +1 -0
- package/dist/helpers/external-id.js +8 -27
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/nostr-web-token.d.ts +54 -0
- package/dist/helpers/nostr-web-token.js +124 -0
- package/dist/operations/index.d.ts +1 -0
- package/dist/operations/index.js +1 -0
- package/dist/operations/nostr-web-token.d.ts +27 -0
- package/dist/operations/nostr-web-token.js +58 -0
- package/package.json +3 -3
|
@@ -4,8 +4,8 @@ import { TrustedProvider, TrustedProviderListEvent, UserAssertionEvent } from ".
|
|
|
4
4
|
import { CastRefEventStore, EventCast, PubkeyCast } from "./cast.js";
|
|
5
5
|
/** A cast for a service provider pubkey that publishes NIP-85 assertion events */
|
|
6
6
|
export declare class AssertionProvider extends PubkeyCast {
|
|
7
|
-
/** @internal Per-class singleton cache used by castPubkey */
|
|
8
|
-
static cache: Map<string, AssertionProvider
|
|
7
|
+
/** @internal Per-class weak singleton cache used by castPubkey */
|
|
8
|
+
static cache: Map<string, WeakRef<AssertionProvider>>;
|
|
9
9
|
/** Relay hints declared for this provider in the trusted provider list */
|
|
10
10
|
get providerRelays(): string[];
|
|
11
11
|
/** Returns the relays to use when querying this provider's assertion events */
|
|
@@ -9,7 +9,7 @@ import { castUser } from "./user.js";
|
|
|
9
9
|
// ─── AssertionProvider ───────────────────────────────────────────────────────
|
|
10
10
|
/** A cast for a service provider pubkey that publishes NIP-85 assertion events */
|
|
11
11
|
export class AssertionProvider extends PubkeyCast {
|
|
12
|
-
/** @internal Per-class singleton cache used by castPubkey */
|
|
12
|
+
/** @internal Per-class weak singleton cache used by castPubkey */
|
|
13
13
|
static cache = new Map();
|
|
14
14
|
/** Relay hints declared for this provider in the trusted provider list */
|
|
15
15
|
get providerRelays() {
|
package/dist/factories/index.js
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { EventFactory } from "applesauce-core/factories";
|
|
2
|
+
import { KnownEventTemplate, NostrEvent } from "applesauce-core/helpers";
|
|
3
|
+
import { NOSTR_WEB_TOKEN_KIND, NostrWebTokenClaims } from "../helpers/nostr-web-token.js";
|
|
4
|
+
export type NostrWebTokenTemplate = KnownEventTemplate<typeof NOSTR_WEB_TOKEN_KIND>;
|
|
5
|
+
/** Factory for Nostr Web Token events (kind 27519). @see https://github.com/Open-Ranking/nostr-web-tokens */
|
|
6
|
+
export declare class NostrWebTokenFactory extends EventFactory<typeof NOSTR_WEB_TOKEN_KIND, NostrWebTokenTemplate> {
|
|
7
|
+
/** Creates a Nostr Web Token factory. */
|
|
8
|
+
static create(claims?: Partial<NostrWebTokenClaims>): NostrWebTokenFactory;
|
|
9
|
+
/** Creates a factory configured to modify an existing Nostr Web Token. */
|
|
10
|
+
static modify(event: NostrEvent): NostrWebTokenFactory;
|
|
11
|
+
/** Sets or removes the issuer claim. */
|
|
12
|
+
issuer(value: string | null): this;
|
|
13
|
+
/** Sets or removes the subject claim. */
|
|
14
|
+
subject(value: string | null): this;
|
|
15
|
+
/** Replaces all audience claims. */
|
|
16
|
+
audiences(values: string[]): this;
|
|
17
|
+
/** Adds an audience claim. */
|
|
18
|
+
addAudience(value: string, replace?: boolean): this;
|
|
19
|
+
/** Removes a matching audience claim. */
|
|
20
|
+
removeAudience(value: string): this;
|
|
21
|
+
/** Clears all audience claims. */
|
|
22
|
+
clearAudiences(): this;
|
|
23
|
+
/** Sets or removes the issued-at claim. */
|
|
24
|
+
issuedAt(value: number | null): this;
|
|
25
|
+
/** Sets or removes the expiration claim. */
|
|
26
|
+
expiration(value: number | null): this;
|
|
27
|
+
/** Sets or removes the not-before claim. */
|
|
28
|
+
notBefore(value: number | null): this;
|
|
29
|
+
/** Sets or removes a singleton claim. */
|
|
30
|
+
claim(name: string, value: string | number | null): this;
|
|
31
|
+
/** Adds an application-defined claim. */
|
|
32
|
+
addClaim(name: string, value: string | number, replace?: boolean): this;
|
|
33
|
+
/** Removes matching values for a claim, or all tags with the claim name when value is omitted. */
|
|
34
|
+
removeClaim(name: string, value?: string | number): this;
|
|
35
|
+
/** Clears all tags with the claim name. */
|
|
36
|
+
clearClaim(name: string): this;
|
|
37
|
+
/** Sets the human-readable signing message. */
|
|
38
|
+
message(content: string): this;
|
|
39
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { blankEventTemplate, EventFactory, toEventTemplate } from "applesauce-core/factories";
|
|
2
|
+
import { NOSTR_WEB_TOKEN_KIND } from "../helpers/nostr-web-token.js";
|
|
3
|
+
import * as NostrWebToken from "../operations/nostr-web-token.js";
|
|
4
|
+
/** Factory for Nostr Web Token events (kind 27519). @see https://github.com/Open-Ranking/nostr-web-tokens */
|
|
5
|
+
export class NostrWebTokenFactory extends EventFactory {
|
|
6
|
+
/** Creates a Nostr Web Token factory. */
|
|
7
|
+
static create(claims) {
|
|
8
|
+
const factory = new NostrWebTokenFactory((res) => res(blankEventTemplate(NOSTR_WEB_TOKEN_KIND)));
|
|
9
|
+
if (!claims)
|
|
10
|
+
return factory;
|
|
11
|
+
let result = factory;
|
|
12
|
+
if (claims.issuer !== undefined)
|
|
13
|
+
result = result.issuer(claims.issuer);
|
|
14
|
+
if (claims.subject !== undefined)
|
|
15
|
+
result = result.subject(claims.subject);
|
|
16
|
+
if (claims.audiences !== undefined)
|
|
17
|
+
result = result.audiences(claims.audiences);
|
|
18
|
+
if (claims.issuedAt !== undefined)
|
|
19
|
+
result = result.issuedAt(claims.issuedAt);
|
|
20
|
+
if (claims.expiration !== undefined)
|
|
21
|
+
result = result.expiration(claims.expiration);
|
|
22
|
+
if (claims.notBefore !== undefined)
|
|
23
|
+
result = result.notBefore(claims.notBefore);
|
|
24
|
+
if (claims.claims) {
|
|
25
|
+
for (const [name, values] of Object.entries(claims.claims)) {
|
|
26
|
+
result = result.clearClaim(name);
|
|
27
|
+
for (const value of values)
|
|
28
|
+
result = result.addClaim(name, value, false);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
/** Creates a factory configured to modify an existing Nostr Web Token. */
|
|
34
|
+
static modify(event) {
|
|
35
|
+
if (event.kind !== NOSTR_WEB_TOKEN_KIND)
|
|
36
|
+
throw new Error("Expected a Nostr Web Token event");
|
|
37
|
+
return new NostrWebTokenFactory((res) => res(toEventTemplate(event)));
|
|
38
|
+
}
|
|
39
|
+
/** Sets or removes the issuer claim. */
|
|
40
|
+
issuer(value) {
|
|
41
|
+
return this.chain(NostrWebToken.setIssuer(value));
|
|
42
|
+
}
|
|
43
|
+
/** Sets or removes the subject claim. */
|
|
44
|
+
subject(value) {
|
|
45
|
+
return this.chain(NostrWebToken.setSubject(value));
|
|
46
|
+
}
|
|
47
|
+
/** Replaces all audience claims. */
|
|
48
|
+
audiences(values) {
|
|
49
|
+
return this.chain(NostrWebToken.setAudiences(values));
|
|
50
|
+
}
|
|
51
|
+
/** Adds an audience claim. */
|
|
52
|
+
addAudience(value, replace = true) {
|
|
53
|
+
return this.chain(NostrWebToken.addAudience(value, replace));
|
|
54
|
+
}
|
|
55
|
+
/** Removes a matching audience claim. */
|
|
56
|
+
removeAudience(value) {
|
|
57
|
+
return this.chain(NostrWebToken.removeAudience(value));
|
|
58
|
+
}
|
|
59
|
+
/** Clears all audience claims. */
|
|
60
|
+
clearAudiences() {
|
|
61
|
+
return this.chain(NostrWebToken.clearAudiences());
|
|
62
|
+
}
|
|
63
|
+
/** Sets or removes the issued-at claim. */
|
|
64
|
+
issuedAt(value) {
|
|
65
|
+
return this.chain(NostrWebToken.setIssuedAt(value));
|
|
66
|
+
}
|
|
67
|
+
/** Sets or removes the expiration claim. */
|
|
68
|
+
expiration(value) {
|
|
69
|
+
return this.chain(NostrWebToken.setExpiration(value));
|
|
70
|
+
}
|
|
71
|
+
/** Sets or removes the not-before claim. */
|
|
72
|
+
notBefore(value) {
|
|
73
|
+
return this.chain(NostrWebToken.setNotBefore(value));
|
|
74
|
+
}
|
|
75
|
+
/** Sets or removes a singleton claim. */
|
|
76
|
+
claim(name, value) {
|
|
77
|
+
return this.chain(NostrWebToken.setClaim(name, value));
|
|
78
|
+
}
|
|
79
|
+
/** Adds an application-defined claim. */
|
|
80
|
+
addClaim(name, value, replace = true) {
|
|
81
|
+
return this.chain(NostrWebToken.addClaim(name, value, replace));
|
|
82
|
+
}
|
|
83
|
+
/** Removes matching values for a claim, or all tags with the claim name when value is omitted. */
|
|
84
|
+
removeClaim(name, value) {
|
|
85
|
+
return this.chain(NostrWebToken.removeClaim(name, value));
|
|
86
|
+
}
|
|
87
|
+
/** Clears all tags with the claim name. */
|
|
88
|
+
clearClaim(name) {
|
|
89
|
+
return this.chain(NostrWebToken.clearClaim(name));
|
|
90
|
+
}
|
|
91
|
+
/** Sets the human-readable signing message. */
|
|
92
|
+
message(content) {
|
|
93
|
+
return this.content(content);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -24,6 +24,8 @@ export function parseExternalPointer(identifier) {
|
|
|
24
24
|
return { kind: "#", identifier: identifier };
|
|
25
25
|
if (identifier.startsWith("geo:"))
|
|
26
26
|
return { kind: "geo", identifier: identifier };
|
|
27
|
+
if (identifier.startsWith("iso3166:"))
|
|
28
|
+
return { kind: "iso3166", identifier: identifier };
|
|
27
29
|
if (identifier.startsWith("isbn:"))
|
|
28
30
|
return { kind: "isbn", identifier: identifier };
|
|
29
31
|
if (identifier.startsWith("podcast:guid:"))
|
|
@@ -37,33 +39,12 @@ export function parseExternalPointer(identifier) {
|
|
|
37
39
|
if (identifier.startsWith("doi:"))
|
|
38
40
|
return { kind: "doi", identifier: identifier };
|
|
39
41
|
// Check for blockchain identifiers
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return { kind:
|
|
46
|
-
}
|
|
47
|
-
// Ethereum: ethereum:<chainId>:tx:<txHash> or ethereum:<chainId>:address:<address>
|
|
48
|
-
const ethereumTxMatch = identifier.match(/^ethereum:(\d+):tx:(.+)$/);
|
|
49
|
-
if (ethereumTxMatch) {
|
|
50
|
-
return { kind: "ethereum:tx", identifier: identifier };
|
|
51
|
-
}
|
|
52
|
-
const ethereumAddressMatch = identifier.match(/^ethereum:(\d+):address:(.+)$/);
|
|
53
|
-
if (ethereumAddressMatch) {
|
|
54
|
-
return { kind: "ethereum:address", identifier: identifier };
|
|
55
|
-
}
|
|
56
|
-
// Other blockchains: <blockchain>:tx:<txid> or <blockchain>:address:<address>
|
|
57
|
-
// Exclude known prefixes to avoid false matches
|
|
58
|
-
const blockchainTxMatch = identifier.match(/^([a-z0-9]+):tx:(.+)$/);
|
|
59
|
-
if (blockchainTxMatch && !identifier.startsWith("bitcoin:") && !identifier.startsWith("ethereum:")) {
|
|
60
|
-
const blockchain = blockchainTxMatch[1];
|
|
61
|
-
return { kind: `${blockchain}:tx`, identifier: identifier };
|
|
62
|
-
}
|
|
63
|
-
const blockchainAddressMatch = identifier.match(/^([a-z0-9]+):address:(.+)$/);
|
|
64
|
-
if (blockchainAddressMatch && !identifier.startsWith("bitcoin:") && !identifier.startsWith("ethereum:")) {
|
|
65
|
-
const blockchain = blockchainAddressMatch[1];
|
|
66
|
-
return { kind: `${blockchain}:address`, identifier: identifier };
|
|
42
|
+
// Generic format (NIP-73): <blockchain>:[<chainId>:]tx:<txid> or <blockchain>:[<chainId>:]address:<address>
|
|
43
|
+
// The optional chainId (e.g. ethereum:1:tx:...) is not part of the "k" tag kind
|
|
44
|
+
const blockchainMatch = identifier.match(/^([a-z0-9]+):(?:\d+:)?(tx|address):.+$/);
|
|
45
|
+
if (blockchainMatch) {
|
|
46
|
+
const [, blockchain, type] = blockchainMatch;
|
|
47
|
+
return { kind: `${blockchain}:${type}`, identifier: identifier };
|
|
67
48
|
}
|
|
68
49
|
// Check for URL (must be a valid URL, normalized, no fragment)
|
|
69
50
|
// URLs don't have a prefix, so we check if it's a valid URL after checking all prefixes
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ export * from "./lists.js";
|
|
|
27
27
|
export * from "./lnurl.js";
|
|
28
28
|
export * from "./messages.js";
|
|
29
29
|
export * from "./mute.js";
|
|
30
|
+
export * from "./nostr-web-token.js";
|
|
30
31
|
export * from "./picture-post.js";
|
|
31
32
|
export * from "./poll.js";
|
|
32
33
|
export * from "./profile-badges.js";
|
package/dist/helpers/index.js
CHANGED
|
@@ -27,6 +27,7 @@ export * from "./lists.js";
|
|
|
27
27
|
export * from "./lnurl.js";
|
|
28
28
|
export * from "./messages.js";
|
|
29
29
|
export * from "./mute.js";
|
|
30
|
+
export * from "./nostr-web-token.js";
|
|
30
31
|
export * from "./picture-post.js";
|
|
31
32
|
export * from "./poll.js";
|
|
32
33
|
export * from "./profile-badges.js";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { KnownEvent, NostrEvent } from "applesauce-core/helpers/event";
|
|
2
|
+
/** @see https://github.com/Open-Ranking/nostr-web-tokens */
|
|
3
|
+
export declare const NOSTR_WEB_TOKEN_KIND = 27519;
|
|
4
|
+
export type NostrWebTokenEvent = KnownEvent<typeof NOSTR_WEB_TOKEN_KIND>;
|
|
5
|
+
export type NostrWebTokenClaims = {
|
|
6
|
+
/** Issuer claim, defaults to the signing pubkey when omitted */
|
|
7
|
+
issuer: string;
|
|
8
|
+
/** Subject claim, defaults to the signing pubkey when omitted */
|
|
9
|
+
subject: string;
|
|
10
|
+
/** Audience claims */
|
|
11
|
+
audiences: string[];
|
|
12
|
+
/** Issued-at timestamp, defaults to the event created_at when omitted */
|
|
13
|
+
issuedAt: number;
|
|
14
|
+
/** Expiration timestamp */
|
|
15
|
+
expiration?: number;
|
|
16
|
+
/** Not-before timestamp */
|
|
17
|
+
notBefore?: number;
|
|
18
|
+
/** Additional application-defined claims */
|
|
19
|
+
claims: Record<string, string[]>;
|
|
20
|
+
};
|
|
21
|
+
export declare const NostrWebTokenClaimsSymbol: unique symbol;
|
|
22
|
+
/** Parses all registered and custom claims from a Nostr Web Token event. */
|
|
23
|
+
export declare function parseNostrWebTokenClaims(event: NostrWebTokenEvent): NostrWebTokenClaims;
|
|
24
|
+
export declare function parseNostrWebTokenClaims(event: NostrEvent): NostrWebTokenClaims | undefined;
|
|
25
|
+
/** Gets the issuer claim, or the signing pubkey when absent. */
|
|
26
|
+
export declare function getNostrWebTokenIssuer(event: NostrWebTokenEvent): string;
|
|
27
|
+
export declare function getNostrWebTokenIssuer(event: NostrEvent): string | undefined;
|
|
28
|
+
/** Gets the subject claim, or the signing pubkey when absent. */
|
|
29
|
+
export declare function getNostrWebTokenSubject(event: NostrWebTokenEvent): string;
|
|
30
|
+
export declare function getNostrWebTokenSubject(event: NostrEvent): string | undefined;
|
|
31
|
+
/** Gets all audience claims. */
|
|
32
|
+
export declare function getNostrWebTokenAudiences(event: NostrWebTokenEvent): string[];
|
|
33
|
+
export declare function getNostrWebTokenAudiences(event: NostrEvent): string[] | undefined;
|
|
34
|
+
/** Gets the issued-at claim, or the event created_at when absent. */
|
|
35
|
+
export declare function getNostrWebTokenIssuedAt(event: NostrWebTokenEvent): number;
|
|
36
|
+
export declare function getNostrWebTokenIssuedAt(event: NostrEvent): number | undefined;
|
|
37
|
+
/** Gets the expiration claim. */
|
|
38
|
+
export declare function getNostrWebTokenExpiration(event: NostrEvent): number | undefined;
|
|
39
|
+
/** Gets the not-before claim. */
|
|
40
|
+
export declare function getNostrWebTokenNotBefore(event: NostrEvent): number | undefined;
|
|
41
|
+
/** Checks if an event is structurally a valid Nostr Web Token. Does not verify the event signature. */
|
|
42
|
+
export declare function isValidNostrWebToken(event?: NostrEvent): event is NostrWebTokenEvent;
|
|
43
|
+
/** Checks the exp and nbf claims against a timestamp. */
|
|
44
|
+
export declare function isNostrWebTokenActive(event: NostrEvent, now?: number, clockSkew?: number): boolean;
|
|
45
|
+
/** Checks if a token contains a matching audience claim. */
|
|
46
|
+
export declare function hasNostrWebTokenAudience(event: NostrEvent, audience: string): boolean;
|
|
47
|
+
/** Encodes a Nostr Web Token event for HTTP Authorization transport. */
|
|
48
|
+
export declare function encodeNostrWebToken(event: NostrEvent): string;
|
|
49
|
+
/** Decodes a Nostr Web Token from Base64URL transport encoding. */
|
|
50
|
+
export declare function decodeNostrWebToken(token: string): NostrEvent | undefined;
|
|
51
|
+
/** Creates an HTTP Authorization header value for a Nostr Web Token event. */
|
|
52
|
+
export declare function createNostrWebTokenAuthorizationHeader(event: NostrEvent): string;
|
|
53
|
+
/** Parses an HTTP Authorization header value containing a Nostr Web Token. */
|
|
54
|
+
export declare function parseNostrWebTokenAuthorizationHeader(header: string | null | undefined): NostrEvent | undefined;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { base64urlnopad, utf8 } from "@scure/base";
|
|
2
|
+
import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
|
|
3
|
+
import { getTagValue, isEvent } from "applesauce-core/helpers/event";
|
|
4
|
+
/** @see https://github.com/Open-Ranking/nostr-web-tokens */
|
|
5
|
+
export const NOSTR_WEB_TOKEN_KIND = 27519;
|
|
6
|
+
export const NostrWebTokenClaimsSymbol = Symbol.for("nostr-web-token-claims");
|
|
7
|
+
const singletonClaims = new Set(["iss", "sub", "iat", "exp", "nbf"]);
|
|
8
|
+
const registeredClaims = new Set(["iss", "sub", "aud", "iat", "exp", "nbf"]);
|
|
9
|
+
function parseTimestamp(value) {
|
|
10
|
+
if (value === undefined || !/^\d+$/.test(value))
|
|
11
|
+
return undefined;
|
|
12
|
+
return Number.parseInt(value, 10);
|
|
13
|
+
}
|
|
14
|
+
function isValidTimestamp(value) {
|
|
15
|
+
return parseTimestamp(value) !== undefined;
|
|
16
|
+
}
|
|
17
|
+
export function parseNostrWebTokenClaims(event) {
|
|
18
|
+
if (event.kind !== NOSTR_WEB_TOKEN_KIND)
|
|
19
|
+
return undefined;
|
|
20
|
+
return getOrComputeCachedValue(event, NostrWebTokenClaimsSymbol, () => {
|
|
21
|
+
const claims = {};
|
|
22
|
+
const audiences = [];
|
|
23
|
+
for (const tag of event.tags) {
|
|
24
|
+
const [name, value] = tag;
|
|
25
|
+
if (value === undefined)
|
|
26
|
+
continue;
|
|
27
|
+
if (name === "aud")
|
|
28
|
+
audiences.push(value);
|
|
29
|
+
else if (!registeredClaims.has(name))
|
|
30
|
+
claims[name] = claims[name] ? [...claims[name], value] : [value];
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
issuer: getTagValue(event, "iss") ?? event.pubkey,
|
|
34
|
+
subject: getTagValue(event, "sub") ?? event.pubkey,
|
|
35
|
+
audiences,
|
|
36
|
+
issuedAt: parseTimestamp(getTagValue(event, "iat")) ?? event.created_at,
|
|
37
|
+
expiration: parseTimestamp(getTagValue(event, "exp")),
|
|
38
|
+
notBefore: parseTimestamp(getTagValue(event, "nbf")),
|
|
39
|
+
claims,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
export function getNostrWebTokenIssuer(event) {
|
|
44
|
+
return parseNostrWebTokenClaims(event)?.issuer;
|
|
45
|
+
}
|
|
46
|
+
export function getNostrWebTokenSubject(event) {
|
|
47
|
+
return parseNostrWebTokenClaims(event)?.subject;
|
|
48
|
+
}
|
|
49
|
+
export function getNostrWebTokenAudiences(event) {
|
|
50
|
+
return parseNostrWebTokenClaims(event)?.audiences;
|
|
51
|
+
}
|
|
52
|
+
export function getNostrWebTokenIssuedAt(event) {
|
|
53
|
+
return parseNostrWebTokenClaims(event)?.issuedAt;
|
|
54
|
+
}
|
|
55
|
+
/** Gets the expiration claim. */
|
|
56
|
+
export function getNostrWebTokenExpiration(event) {
|
|
57
|
+
return parseNostrWebTokenClaims(event)?.expiration;
|
|
58
|
+
}
|
|
59
|
+
/** Gets the not-before claim. */
|
|
60
|
+
export function getNostrWebTokenNotBefore(event) {
|
|
61
|
+
return parseNostrWebTokenClaims(event)?.notBefore;
|
|
62
|
+
}
|
|
63
|
+
/** Checks if an event is structurally a valid Nostr Web Token. Does not verify the event signature. */
|
|
64
|
+
export function isValidNostrWebToken(event) {
|
|
65
|
+
if (!event || event.kind !== NOSTR_WEB_TOKEN_KIND)
|
|
66
|
+
return false;
|
|
67
|
+
if (!isEvent(event))
|
|
68
|
+
return false;
|
|
69
|
+
for (const name of registeredClaims) {
|
|
70
|
+
const tags = event.tags.filter((tag) => tag[0] === name);
|
|
71
|
+
if (tags.some((tag) => tag[1] === undefined))
|
|
72
|
+
return false;
|
|
73
|
+
if (singletonClaims.has(name) && tags.length > 1)
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
for (const name of ["iat", "exp", "nbf"]) {
|
|
77
|
+
const tag = event.tags.find((t) => t[0] === name);
|
|
78
|
+
if (tag && !isValidTimestamp(tag[1]))
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
/** Checks the exp and nbf claims against a timestamp. */
|
|
84
|
+
export function isNostrWebTokenActive(event, now = Math.floor(Date.now() / 1000), clockSkew = 60) {
|
|
85
|
+
if (!isValidNostrWebToken(event))
|
|
86
|
+
return false;
|
|
87
|
+
const claims = parseNostrWebTokenClaims(event);
|
|
88
|
+
if (!claims)
|
|
89
|
+
return false;
|
|
90
|
+
if (claims.expiration !== undefined && now - clockSkew >= claims.expiration)
|
|
91
|
+
return false;
|
|
92
|
+
if (claims.notBefore !== undefined && now + clockSkew < claims.notBefore)
|
|
93
|
+
return false;
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
/** Checks if a token contains a matching audience claim. */
|
|
97
|
+
export function hasNostrWebTokenAudience(event, audience) {
|
|
98
|
+
return parseNostrWebTokenClaims(event)?.audiences.includes(audience) ?? false;
|
|
99
|
+
}
|
|
100
|
+
/** Encodes a Nostr Web Token event for HTTP Authorization transport. */
|
|
101
|
+
export function encodeNostrWebToken(event) {
|
|
102
|
+
return base64urlnopad.encode(utf8.decode(JSON.stringify(event)));
|
|
103
|
+
}
|
|
104
|
+
/** Decodes a Nostr Web Token from Base64URL transport encoding. */
|
|
105
|
+
export function decodeNostrWebToken(token) {
|
|
106
|
+
try {
|
|
107
|
+
const event = JSON.parse(utf8.encode(base64urlnopad.decode(token)));
|
|
108
|
+
return isEvent(event) ? event : undefined;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Creates an HTTP Authorization header value for a Nostr Web Token event. */
|
|
115
|
+
export function createNostrWebTokenAuthorizationHeader(event) {
|
|
116
|
+
return `Nostr ${encodeNostrWebToken(event)}`;
|
|
117
|
+
}
|
|
118
|
+
/** Parses an HTTP Authorization header value containing a Nostr Web Token. */
|
|
119
|
+
export function parseNostrWebTokenAuthorizationHeader(header) {
|
|
120
|
+
const match = header?.match(/^Nostr\s+(.+)$/i);
|
|
121
|
+
if (!match)
|
|
122
|
+
return undefined;
|
|
123
|
+
return decodeNostrWebToken(match[1]);
|
|
124
|
+
}
|
|
@@ -21,6 +21,7 @@ export * as List from "./list.js";
|
|
|
21
21
|
export * as LiveStream from "./live-stream.js";
|
|
22
22
|
export * as MediaAttachment from "./media-attachment.js";
|
|
23
23
|
export * as Note from "./note.js";
|
|
24
|
+
export * as NostrWebToken from "./nostr-web-token.js";
|
|
24
25
|
export * as PicturePost from "./picture-post.js";
|
|
25
26
|
export * as Poll from "./poll.js";
|
|
26
27
|
export * as PollResponse from "./poll-response.js";
|
package/dist/operations/index.js
CHANGED
|
@@ -21,6 +21,7 @@ export * as List from "./list.js";
|
|
|
21
21
|
export * as LiveStream from "./live-stream.js";
|
|
22
22
|
export * as MediaAttachment from "./media-attachment.js";
|
|
23
23
|
export * as Note from "./note.js";
|
|
24
|
+
export * as NostrWebToken from "./nostr-web-token.js";
|
|
24
25
|
export * as PicturePost from "./picture-post.js";
|
|
25
26
|
export * as Poll from "./poll.js";
|
|
26
27
|
export * as PollResponse from "./poll-response.js";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { EventOperation } from "applesauce-core/factories";
|
|
2
|
+
/** Sets or removes the issuer claim. */
|
|
3
|
+
export declare function setIssuer(value: string | null): EventOperation;
|
|
4
|
+
/** Sets or removes the subject claim. */
|
|
5
|
+
export declare function setSubject(value: string | null): EventOperation;
|
|
6
|
+
/** Sets or removes the issued-at claim. */
|
|
7
|
+
export declare function setIssuedAt(value: number | null): EventOperation;
|
|
8
|
+
/** Sets or removes the expiration claim. */
|
|
9
|
+
export declare function setExpiration(value: number | null): EventOperation;
|
|
10
|
+
/** Sets or removes the not-before claim. */
|
|
11
|
+
export declare function setNotBefore(value: number | null): EventOperation;
|
|
12
|
+
/** Adds an audience claim. */
|
|
13
|
+
export declare function addAudience(value: string, replace?: boolean): EventOperation;
|
|
14
|
+
/** Removes a matching audience claim. */
|
|
15
|
+
export declare function removeAudience(value: string): EventOperation;
|
|
16
|
+
/** Removes all audience claims. */
|
|
17
|
+
export declare function clearAudiences(): EventOperation;
|
|
18
|
+
/** Replaces all audience claims. */
|
|
19
|
+
export declare function setAudiences(values: string[]): EventOperation;
|
|
20
|
+
/** Sets or removes a singleton claim. */
|
|
21
|
+
export declare function setClaim(name: string, value: string | number | null): EventOperation;
|
|
22
|
+
/** Adds an application-defined claim. */
|
|
23
|
+
export declare function addClaim(name: string, value: string | number, replace?: boolean): EventOperation;
|
|
24
|
+
/** Removes matching values for a claim, or all tags with the claim name when value is omitted. */
|
|
25
|
+
export declare function removeClaim(name: string, value?: string | number): EventOperation;
|
|
26
|
+
/** Removes all tags with the claim name. */
|
|
27
|
+
export declare function clearClaim(name: string): EventOperation;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { eventPipe } from "applesauce-core/helpers";
|
|
2
|
+
import { addNameValueTag, removeSingletonTag, setSingletonTag } from "applesauce-core/operations/tag/common";
|
|
3
|
+
import { modifyPublicTags } from "applesauce-core/operations/tags";
|
|
4
|
+
function setNullableSingletonTag(name, value) {
|
|
5
|
+
return modifyPublicTags(value === null ? removeSingletonTag(name) : setSingletonTag([name, String(value)], true));
|
|
6
|
+
}
|
|
7
|
+
/** Sets or removes the issuer claim. */
|
|
8
|
+
export function setIssuer(value) {
|
|
9
|
+
return setNullableSingletonTag("iss", value);
|
|
10
|
+
}
|
|
11
|
+
/** Sets or removes the subject claim. */
|
|
12
|
+
export function setSubject(value) {
|
|
13
|
+
return setNullableSingletonTag("sub", value);
|
|
14
|
+
}
|
|
15
|
+
/** Sets or removes the issued-at claim. */
|
|
16
|
+
export function setIssuedAt(value) {
|
|
17
|
+
return setNullableSingletonTag("iat", value);
|
|
18
|
+
}
|
|
19
|
+
/** Sets or removes the expiration claim. */
|
|
20
|
+
export function setExpiration(value) {
|
|
21
|
+
return setNullableSingletonTag("exp", value);
|
|
22
|
+
}
|
|
23
|
+
/** Sets or removes the not-before claim. */
|
|
24
|
+
export function setNotBefore(value) {
|
|
25
|
+
return setNullableSingletonTag("nbf", value);
|
|
26
|
+
}
|
|
27
|
+
/** Adds an audience claim. */
|
|
28
|
+
export function addAudience(value, replace = true) {
|
|
29
|
+
return modifyPublicTags(addNameValueTag(["aud", value], replace));
|
|
30
|
+
}
|
|
31
|
+
/** Removes a matching audience claim. */
|
|
32
|
+
export function removeAudience(value) {
|
|
33
|
+
return modifyPublicTags((tags) => tags.filter((tag) => !(tag[0] === "aud" && tag[1] === value)));
|
|
34
|
+
}
|
|
35
|
+
/** Removes all audience claims. */
|
|
36
|
+
export function clearAudiences() {
|
|
37
|
+
return modifyPublicTags((tags) => tags.filter((tag) => tag[0] !== "aud"));
|
|
38
|
+
}
|
|
39
|
+
/** Replaces all audience claims. */
|
|
40
|
+
export function setAudiences(values) {
|
|
41
|
+
return eventPipe(clearAudiences(), ...values.map((value) => addAudience(value, false)));
|
|
42
|
+
}
|
|
43
|
+
/** Sets or removes a singleton claim. */
|
|
44
|
+
export function setClaim(name, value) {
|
|
45
|
+
return setNullableSingletonTag(name, value);
|
|
46
|
+
}
|
|
47
|
+
/** Adds an application-defined claim. */
|
|
48
|
+
export function addClaim(name, value, replace = true) {
|
|
49
|
+
return modifyPublicTags(addNameValueTag([name, String(value)], replace));
|
|
50
|
+
}
|
|
51
|
+
/** Removes matching values for a claim, or all tags with the claim name when value is omitted. */
|
|
52
|
+
export function removeClaim(name, value) {
|
|
53
|
+
return modifyPublicTags((tags) => tags.filter((tag) => (value === undefined ? tag[0] !== name : !(tag[0] === name && tag[1] === String(value)))));
|
|
54
|
+
}
|
|
55
|
+
/** Removes all tags with the claim name. */
|
|
56
|
+
export function clearClaim(name) {
|
|
57
|
+
return removeClaim(name);
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-common",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "Common NIP-specific helpers and models for applesauce",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -82,8 +82,8 @@
|
|
|
82
82
|
}
|
|
83
83
|
},
|
|
84
84
|
"dependencies": {
|
|
85
|
-
"@scure/base": "^
|
|
86
|
-
"applesauce-core": "^6.
|
|
85
|
+
"@scure/base": "^2.2.0",
|
|
86
|
+
"applesauce-core": "^6.2.0",
|
|
87
87
|
"hash-sum": "^2.0.0",
|
|
88
88
|
"light-bolt11-decoder": "^3.2.0",
|
|
89
89
|
"nanoid": "^5.0.9",
|