forked from fastify/fastify-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.d.ts
162 lines (139 loc) · 5.5 KB
/
jwt.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
DecoderOptions,
JwtHeader,
KeyFetcher,
SignerCallback,
SignerOptions,
VerifierCallback,
VerifierOptions
} from 'fast-jwt'
import * as fastify from 'fastify'
/**
* for declaration merging
* @example
* ```
* declare module 'fastify-jwt' {
* interface FastifyJWT {
* payload: { name: string; email: string }
* }
* }
* ```
* @example
* ```
* // With `formatUser`.
* declare module 'fastify-jwt' {
* interface FastifyJWT {
* payload: { Name: string; e_mail: string }
* user: { name: string; email: string }
* }
* }
* ```
*/
export interface FastifyJWT {
// payload: ...
// user: ...
}
export type SignPayloadType = FastifyJWT extends { payload: infer T }
? T extends string | object | Buffer
? T
: string | object | Buffer
: string | object | Buffer
export type UserType = FastifyJWT extends { user: infer T }
? T
: SignPayloadType
export type TokenOrHeader = JwtHeader | { header: JwtHeader; payload: any }
export type Secret = string | Buffer | KeyFetcher
| ((request: fastify.FastifyRequest, tokenOrHeader: TokenOrHeader, cb: (e: Error | null, secret: string | Buffer | undefined) => void) => void)
| ((request: fastify.FastifyRequest, tokenOrHeader: TokenOrHeader) => Promise<string | Buffer>)
export type VerifyPayloadType = object | string
export type DecodePayloadType = object | string
export interface DecodeCallback<Decoded extends DecodePayloadType> {
(err: Error, decoded: Decoded): void
}
export interface SignOptions extends Omit<SignerOptions, "expiresIn" | "notBefore"> {
expiresIn: number | string;
notBefore: number | string;
}
export interface VerifyOptions extends Omit<VerifierOptions, "maxAge"> {
maxAge: number | string;
}
export interface FastifyJWTOptions {
secret: Secret | { public: Secret; private: Secret }
decode?: Partial<DecoderOptions>
sign?: Partial<SignOptions>
verify?: Partial<VerifyOptions> & { extractToken?: (request: fastify.FastifyRequest) => string | void }
cookie?: {
cookieName: string,
signed: boolean
}
messages?: {
badRequestErrorMessage?: string
badCookieRequestErrorMessage?: string
noAuthorizationInHeaderMessage?: string
noAuthorizationInCookieMessage?: string
authorizationTokenExpiredMessage?: string
authorizationTokenInvalid?: ((err: Error) => string) | string
authorizationTokenUntrusted?: string
}
trusted?: (request: fastify.FastifyRequest, decodedToken: { [k: string]: any }) => boolean | Promise<boolean> | SignPayloadType | Promise<SignPayloadType>
formatUser?: (payload: SignPayloadType) => UserType,
jwtDecode?: boolean | string
namespace?: string
jwtVerify?: string
jwtSign?: string
}
export interface JWT {
options: {
decode: Partial<DecoderOptions>
sign: Partial<SignOptions>
verify: Partial<VerifyOptions>
}
cookie?: {
cookieName: string,
signed: boolean
}
sign(payload: SignPayloadType, options?: Partial<SignOptions>): string
sign(payload: SignPayloadType, callback: SignerCallback): void
sign(payload: SignPayloadType, options: Partial<SignOptions>, callback: SignerCallback): void
verify<Decoded extends VerifyPayloadType>(token: string, options?: Partial<VerifyOptions>): Decoded
verify<Decoded extends VerifyPayloadType>(token: string, callback: VerifierCallback): void
verify<Decoded extends VerifyPayloadType>(token: string, options: Partial<VerifyOptions>, callback: VerifierCallback): void
decode<Decoded extends DecodePayloadType>(token: string, options?: Partial<DecoderOptions>): null | Decoded
}
export type { JwtHeader } from 'fast-jwt'
export const fastifyJwt: fastify.FastifyPluginCallback<FastifyJWTOptions>
export default fastifyJwt
export interface FastifyJwtSignOptions {
sign?: Partial<SignOptions>
}
export interface FastifyJwtVerifyOptions {
decode: Partial<DecoderOptions>
verify: Partial<VerifyOptions>
}
export interface FastifyJwtDecodeOptions {
decode: Partial<DecoderOptions>
verify: Partial<VerifyOptions>
}
declare module 'fastify' {
interface FastifyInstance {
jwt: JWT
}
interface FastifyReply {
jwtSign(payload: SignPayloadType, options?: FastifyJwtSignOptions): Promise<string>
jwtSign(payload: SignPayloadType, callback: SignerCallback): void
jwtSign(payload: SignPayloadType, options: FastifyJwtSignOptions, callback: SignerCallback): void
jwtSign(payload: SignPayloadType, options?: Partial<SignOptions>): Promise<string>
jwtSign(payload: SignPayloadType, options: Partial<SignOptions>, callback: SignerCallback): void
}
interface FastifyRequest {
jwtVerify<Decoded extends VerifyPayloadType>(options?: FastifyJwtVerifyOptions): Promise<Decoded>
jwtVerify<Decoded extends VerifyPayloadType>(callback: VerifierCallback): void
jwtVerify<Decoded extends VerifyPayloadType>(options: FastifyJwtVerifyOptions, callback: VerifierCallback): void
jwtVerify<Decoded extends VerifyPayloadType>(options?: Partial<VerifyOptions>): Promise<Decoded>
jwtVerify<Decoded extends VerifyPayloadType>(options: Partial<VerifyOptions>, callback: VerifierCallback): void
jwtDecode<Decoded extends DecodePayloadType>(options?: FastifyJwtDecodeOptions): Promise<Decoded>
jwtDecode<Decoded extends DecodePayloadType>(callback: DecodeCallback<Decoded>): void
jwtDecode<Decoded extends DecodePayloadType>(options: FastifyJwtDecodeOptions, callback: DecodeCallback<Decoded>): void
user: UserType
}
}