@navios/jwt 0.1.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.
@@ -0,0 +1,125 @@
1
+ import type { Secret as JwtSecret } from 'jsonwebtoken'
2
+
3
+ import { z } from 'zod'
4
+
5
+ export enum RequestType {
6
+ Sign = 'Sign',
7
+ Verify = 'Verify',
8
+ }
9
+
10
+ export const AlgorithmType = z.enum([
11
+ 'HS256',
12
+ 'HS384',
13
+ 'HS512',
14
+ 'RS256',
15
+ 'RS384',
16
+ 'RS512',
17
+ 'ES256',
18
+ 'ES384',
19
+ 'ES512',
20
+ 'PS256',
21
+ 'PS384',
22
+ 'PS512',
23
+ 'none',
24
+ ])
25
+
26
+ export const JwtHeaderSchema = z.object({
27
+ alg: AlgorithmType.or(z.string()),
28
+ typ: z.string().optional(),
29
+ cty: z.string().optional(),
30
+ crit: z.string().array().optional(),
31
+ kid: z.string().optional(),
32
+ jku: z.string().optional(),
33
+ x5u: z.union([z.string(), z.array(z.string())]).optional(),
34
+ 'x5t#S256': z.string().optional(),
35
+ x5t: z.string().optional(),
36
+ x5c: z.union([z.string(), z.array(z.string())]).optional(),
37
+ })
38
+
39
+ export type JwtHeader = z.infer<typeof JwtHeaderSchema>
40
+
41
+ export const SignOptionsSchema = z.object({
42
+ algorithm: AlgorithmType.optional(),
43
+ keyid: z.string().optional(),
44
+ expiresIn: z.union([z.string(), z.number()]).optional(),
45
+ notBefore: z.union([z.string(), z.number()]).optional(),
46
+ audience: z.union([z.string(), z.string().array()]).optional(),
47
+ subject: z.string().optional(),
48
+ issuer: z.string().optional(),
49
+ jwtid: z.string().optional(),
50
+ mutatePayload: z.boolean().optional(),
51
+ noTimestamp: z.boolean().optional(),
52
+ header: JwtHeaderSchema.optional(),
53
+ encoding: z.string().optional(),
54
+ allowInsecureKeySizes: z.boolean().optional(),
55
+ allowInvalidAsymmetricKeyTypes: z.boolean().optional(),
56
+ })
57
+
58
+ export type SignOptions = z.infer<typeof SignOptionsSchema>
59
+
60
+ export const VerifyOptionsSchema = z.object({
61
+ algorithms: AlgorithmType.array().optional(),
62
+ audience: z
63
+ .union([z.string(), z.instanceof(RegExp), z.string().array()])
64
+ .optional(),
65
+ clockTimestamp: z.number().optional(),
66
+ clockTolerance: z.number().optional(),
67
+ complete: z.boolean().optional(),
68
+ issuer: z.union([z.string(), z.string().array()]).optional(),
69
+ ignoreExpiration: z.boolean().optional(),
70
+ ignoreNotBefore: z.boolean().optional(),
71
+ jwtid: z.string().optional(),
72
+ nonce: z.string().optional(),
73
+ subject: z.string().optional(),
74
+ maxAge: z.union([z.string(), z.number()]).optional(),
75
+ allowInvalidAsymmetricKeyTypes: z.boolean().optional(),
76
+ })
77
+
78
+ export type VerifyOptions = z.infer<typeof VerifyOptionsSchema>
79
+
80
+ export const SecretSchema = z.union([
81
+ z.string(),
82
+ z.instanceof(Buffer),
83
+ z
84
+ .object({
85
+ type: z.string(),
86
+ })
87
+ .passthrough(),
88
+ z.object({
89
+ key: z.union([z.string(), z.instanceof(Buffer)]),
90
+ passphrase: z.string(),
91
+ }),
92
+ ])
93
+
94
+ export type Secret = z.infer<typeof SecretSchema>
95
+
96
+ export const JwtServiceOptionsSchema = z.object({
97
+ signOptions: SignOptionsSchema.optional(),
98
+ secret: z.string().optional(),
99
+ publicKey: z.union([z.string(), z.instanceof(Buffer)]).optional(),
100
+ privateKey: SecretSchema.optional(),
101
+ verifyOptions: VerifyOptionsSchema.optional(),
102
+ secretOrKeyProvider: z
103
+ .function()
104
+ .args(
105
+ z.nativeEnum(RequestType),
106
+ z.any(),
107
+ z.union([SignOptionsSchema, VerifyOptionsSchema]).optional(),
108
+ )
109
+ .returns(z.union([SecretSchema, z.promise(SecretSchema)]))
110
+ .optional(),
111
+ })
112
+
113
+ export type JwtServiceOptions = z.infer<typeof JwtServiceOptionsSchema>
114
+
115
+ export interface JwtSignOptions extends SignOptions {
116
+ secret?: string | Buffer
117
+ privateKey?: Secret
118
+ }
119
+
120
+ export interface JwtVerifyOptions extends VerifyOptions {
121
+ secret?: string | Buffer
122
+ publicKey?: string | Buffer
123
+ }
124
+
125
+ export type GetSecretKeyResult = string | Buffer | JwtSecret