unjwt 0.5.39 → 0.6.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/README.md +156 -13
- package/dist/_chunks/_crypto.mjs +1014 -0
- package/dist/_chunks/error.d.mts +67 -0
- package/dist/_chunks/index.d.mts +631 -219
- package/dist/_chunks/jwk.d.mts +220 -87
- package/dist/_chunks/jws.mjs +729 -682
- package/dist/_chunks/utils.mjs +74 -69
- package/dist/adapters/h3v1/index.d.mts +282 -0
- package/dist/{_chunks/h3v1.mjs → adapters/h3v1/index.mjs} +204 -91
- package/dist/adapters/h3v2/index.d.mts +213 -0
- package/dist/adapters/{h3v2.mjs → h3v2/index.mjs} +215 -113
- package/dist/core/_crypto/index.d.mts +71 -0
- package/dist/core/_crypto/index.mjs +2 -0
- package/dist/core/jwe.d.mts +40 -0
- package/dist/{_chunks → core}/jwe.mjs +61 -32
- package/dist/core/jwk.d.mts +231 -0
- package/dist/core/jwk.mjs +686 -0
- package/dist/core/jws.d.mts +31 -0
- package/dist/core/jws.mjs +104 -0
- package/dist/{_chunks/index2.d.mts → core/utils/index.d.mts} +13 -12
- package/dist/core/utils/index.mjs +2 -0
- package/dist/index.d.mts +7 -6
- package/dist/index.mjs +5 -5
- package/package.json +50 -41
- package/dist/_chunks/chunk.mjs +0 -13
- package/dist/_chunks/jwe.d.mts +0 -43
- package/dist/_chunks/jwk.mjs +0 -1592
- package/dist/_chunks/jws.d.mts +0 -46
- package/dist/adapters/h3.d.mts +0 -3
- package/dist/adapters/h3.mjs +0 -3
- package/dist/adapters/h3v1.d.mts +0 -3
- package/dist/adapters/h3v1.mjs +0 -3
- package/dist/adapters/h3v2.d.mts +0 -184
- package/dist/jwe.d.mts +0 -3
- package/dist/jwe.mjs +0 -4
- package/dist/jwk.d.mts +0 -3
- package/dist/jwk.mjs +0 -3
- package/dist/jws.d.mts +0 -3
- package/dist/jws.mjs +0 -4
- package/dist/utils.d.mts +0 -3
- package/dist/utils.mjs +0 -2
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//#region src/core/error.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* All known error codes thrown by unjwt.
|
|
4
|
+
*
|
|
5
|
+
* Code naming follows the JOSE spec hierarchy:
|
|
6
|
+
* ERR_JWT_* — RFC 7519 JSON Web Token claim validation
|
|
7
|
+
* ERR_JWS_* — RFC 7515 JSON Web Signature
|
|
8
|
+
* ERR_JWE_* — RFC 7516 JSON Web Encryption
|
|
9
|
+
* ERR_JWK_* — RFC 7517 JSON Web Key
|
|
10
|
+
*/
|
|
11
|
+
type JWTErrorCode = "ERR_JWT_EXPIRED" | "ERR_JWT_NBF" | "ERR_JWT_CLAIM_INVALID" | "ERR_JWT_CLAIM_MISSING" | "ERR_JWS_INVALID" | "ERR_JWS_SIGNATURE_INVALID" | "ERR_JWS_ALG_NOT_ALLOWED" | "ERR_JWE_INVALID" | "ERR_JWE_DECRYPTION_FAILED" | "ERR_JWE_ALG_NOT_ALLOWED" | "ERR_JWK_INVALID" | "ERR_JWK_KEY_NOT_FOUND" | (string & {});
|
|
12
|
+
/**
|
|
13
|
+
* Maps known error codes to their structured `cause` shapes.
|
|
14
|
+
* Only codes that carry decoded context beyond the message appear here.
|
|
15
|
+
* Extend this interface when new structured causes are introduced.
|
|
16
|
+
*/
|
|
17
|
+
interface JWTErrorCauseMap {
|
|
18
|
+
/** Decoded claims available at the point the lifetime check failed. */
|
|
19
|
+
ERR_JWT_EXPIRED: {
|
|
20
|
+
jti?: string;
|
|
21
|
+
iat?: number;
|
|
22
|
+
exp?: number;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Base error class for all unjwt errors. Every error thrown by the library is
|
|
27
|
+
* an instance of `JWTError`, so a single `instanceof` check is sufficient to
|
|
28
|
+
* distinguish library errors from other thrown values. Use the `code` property
|
|
29
|
+
* (or the `isJWTError` type guard) to narrow to a specific error condition.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* import { JWTError, isJWTError } from "unjwt/jws";
|
|
33
|
+
*
|
|
34
|
+
* try {
|
|
35
|
+
* await verify(token, key);
|
|
36
|
+
* } catch (err) {
|
|
37
|
+
* if (isJWTError(err, "ERR_JWT_EXPIRED")) {
|
|
38
|
+
* console.log(err.cause.jti); // string | undefined
|
|
39
|
+
* }
|
|
40
|
+
* }
|
|
41
|
+
*/
|
|
42
|
+
declare class JWTError<TCode extends JWTErrorCode = JWTErrorCode> extends Error {
|
|
43
|
+
readonly code: TCode;
|
|
44
|
+
override readonly cause?: unknown;
|
|
45
|
+
constructor(message: string, code: TCode, cause?: unknown);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Type guard that narrows `error` to `JWTError`.
|
|
49
|
+
*
|
|
50
|
+
* When called with a code that is a key of `JWTErrorCauseMap`, the return
|
|
51
|
+
* type is additionally narrowed to include the structured `cause` shape for
|
|
52
|
+
* that code. When called with any other `JWTErrorCode`, `code` is narrowed
|
|
53
|
+
* but `cause` remains `unknown`. When called without a code, narrows only to
|
|
54
|
+
* `JWTError`.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* if (isJWTError(err, "ERR_JWT_EXPIRED")) {
|
|
58
|
+
* err.cause.jti // string | undefined ✓
|
|
59
|
+
* }
|
|
60
|
+
*/
|
|
61
|
+
declare function isJWTError<T extends keyof JWTErrorCauseMap>(error: unknown, code: T): error is JWTError<T> & {
|
|
62
|
+
cause: JWTErrorCauseMap[T];
|
|
63
|
+
};
|
|
64
|
+
declare function isJWTError<T extends JWTErrorCode>(error: unknown, code: T): error is JWTError<T>;
|
|
65
|
+
declare function isJWTError(error: unknown): error is JWTError;
|
|
66
|
+
//#endregion
|
|
67
|
+
export { isJWTError as i, JWTErrorCauseMap as n, JWTErrorCode as r, JWTError as t };
|