-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignedAccess.d.ts
221 lines (211 loc) · 9.32 KB
/
SignedAccess.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { KeyObject, webcrypto } from 'crypto'
declare module '@jadsonlucena/signedaccess' {
export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array
export type Key = string | ArrayBuffer | Buffer | TypedArray | DataView | KeyObject | webcrypto.CryptoKey
/**
* @classdesc Sign and verify URLs and cookies to add a layer of protection to publicly accessible routes
*/
export default class SignedAccess {
/**
* Create a Signed Access
* @param {Key} key - One of the supported {@link https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options key types}
* @param {Object} [options]
* @param {string} [options.algorithm=sha512] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptogethashes hash algorithms}
* @param {number} [options.ttl=86400] - {@link https://wikipedia.org/wiki/Time_to_live Time to Live} in seconds (Natural number)
*
* @throws {TypeError} Invalid key
* @throws {TypeError} Invalid algorithm
* @throws {TypeError|SyntaxError} Invalid ttl
* @throws {AggregateError} Invalid arguments
*/
constructor(
key: Key,
{
algorithm,
ttl
}: {
algorithm?: string,
ttl?: number
}
)
/**
* @default 'sha512'
*
* @throws {TypeError} Invalid algorithm
*
* @see https://nodejs.org/api/crypto.html#cryptogethashes
*/
set algorithm(param: string)
get algorithm(): string
/**
* @throws {TypeError} Invalid key
*
* @see https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options
*/
set key(param: Key)
get key(): Key
/**
* Time to Live in seconds (Natural number)
* @default 86400
*
* @throws {TypeError|SyntaxError} Invalid ttl
*
* @see https://wikipedia.org/wiki/Time_to_live
*/
set ttl(param: number)
get ttl(): number
/**
* @param {string} prefix - A prefix encodes a scheme (either http:// or https://), {@link Fully_qualified_domain_name FQDN}, and an optional {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname path}. Ending the path with a / is optional but recommended. The prefix shouldn't include query parameters or fragments such as ? or #
* @param {Object} [options]
* @param {string} [options.accessControlAllowMethods=*] - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods Access control allow methods}
* @param {string} [options.algorithm=sha512] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptogethashes hash algorithms}
* @param {Key} [options.key] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options key types}
* @param {string} [options.nonce] - Use random {@link https://openid.net/specs/openid-connect-core-1_0.html#NonceNotes Number Once}
* @param {string} [options.remoteAddress] - {@link https://developer.mozilla.org/en-US/docs/Glossary/IP_Address Client IP}
* @param {number} [options.ttl=86400] - {@link https://wikipedia.org/wiki/Time_to_live Time to Live} in seconds (Natural number)
*
* @throws {TypeError} Invalid prefix
* @throws {TypeError|SyntaxError} Invalid accessControlAllowMethods
* @throws {TypeError} Invalid algorithm
* @throws {TypeError} Invalid key
* @throws {TypeError} Invalid nonce
* @throws {TypeError|SyntaxError} Invalid remoteAddress
* @throws {TypeError|SyntaxError} Invalid ttl
* @throws {AggregateError} Invalid arguments
*
* @return {string} Signed cookie
*/
signCookie(
prefix: string,
{
accessControlAllowMethods,
algorithm,
key,
nonce,
remoteAddress,
ttl
}: {
accessControlAllowMethods?: string,
algorithm?: string,
key?: Key,
nonce?: string,
remoteAddress?: string,
ttl?: number
}
): string
/**
* @param {string} url - Requisition {@link https://nodejs.org/api/url.html#url-strings-and-url-objects URL}
* @param {string} cookie - Signed {@link https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Cookies cookie}
* @param {Object} [options]
* @param {string} [options.algorithm=sha512] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptogethashes hash algorithms}
* @param {Key} [options.key] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options key types}
* @param {string} [options.method] - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods HTTP request methods}
* @param {string} [options.remoteAddress] - {@link https://developer.mozilla.org/en-US/docs/Glossary/IP_Address Client IP}
*
* @throws {TypeError} Invalid URL
* @throws {TypeError} Invalid cookie
* @throws {TypeError} Invalid algorithm
* @throws {TypeError} Invalid key
* @throws {TypeError} Invalid method
* @throws {TypeError|SyntaxError} Invalid remoteAddress
* @throws {AggregateError} Invalid arguments
* @throws {Error} method required
* @throws {Error} remoteAddress required
* @throws {AggregateError} Invalid cookie
*
* @return {boolean}
*/
verifyCookie(
url: string,
cookie: string,
{
algorithm,
key,
method,
remoteAddress
}: {
algorithm?: string,
key?: Key,
method?: string,
remoteAddress?: string
}
): boolean
/**
* @param {string} url - {@link https://nodejs.org/api/url.html#url-strings-and-url-objects URL} to be signed
* @param {Object} [options]
* @param {string} [options.accessControlAllowMethods=*] - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods Access control allow methods}
* @param {string} [options.algorithm=sha512] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptogethashes hash algorithms}
* @param {Key} [options.key] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options key types}
* @param {string} [options.nonce] - Use random {@link https://openid.net/specs/openid-connect-core-1_0.html#NonceNotes Number Once}
* @param {string} [options.pathname] - Starts with / followed by the {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname URL path}, shouldn't include query parameters or fragments such as ? or #
* @param {string} [options.remoteAddress] - {@link https://developer.mozilla.org/en-US/docs/Glossary/IP_Address Client IP}
* @param {number} [options.ttl=86400] - {@link https://wikipedia.org/wiki/Time_to_live Time to Live} in seconds (Natural number)
*
* @throws {TypeError} Invalid URL
* @throws {TypeError} Invalid algorithm
* @throws {TypeError|SyntaxError} Invalid accessControlAllowMethods
* @throws {TypeError} Invalid key
* @throws {TypeError} Invalid nonce
* @throws {TypeError|SyntaxError} Invalid pathname
* @throws {TypeError|SyntaxError} Invalid remoteAddress
* @throws {TypeError|SyntaxError} Invalid ttl
* @throws {AggregateError} Invalid arguments
*
* @return {string} Signed URL
*/
signURL(
url: string,
{
accessControlAllowMethods,
algorithm,
key,
nonce,
pathname,
remoteAddress,
ttl
}: {
accessControlAllowMethods?: string,
algorithm?: string,
key?: Key,
nonce?: string,
pathname?: string,
remoteAddress?: string,
ttl?: number
}
): string
/**
* @param {string} url - Signed {@link https://nodejs.org/api/url.html#url-strings-and-url-objects URL}
* @param {Object} [options]
* @param {string} [options.algorithm=sha512] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptogethashes hash algorithms}
* @param {Key} [options.key] - One of the supported {@link https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options key types}
* @param {string} [options.method] - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods HTTP request methods}
* @param {string} [options.remoteAddress] - {@link https://developer.mozilla.org/en-US/docs/Glossary/IP_Address Client IP}
*
* @throws {TypeError} Invalid URL
* @throws {TypeError} Invalid algorithm
* @throws {TypeError} Invalid key
* @throws {TypeError} Invalid method
* @throws {TypeError|SyntaxError} Invalid remoteAddress
* @throws {AggregateError} Invalid arguments
* @throws {Error} method required
* @throws {Error} remoteAddress required
* @throws {AggregateError} Invalid URL
*
* @return {boolean}
*/
verifyURL(
url: string,
{
algorithm,
key,
method,
remoteAddress
}: {
algorithm?: string,
key?: Key,
method?: string,
remoteAddress?: string
}
): boolean
}
}