1 | #### withDefaults([optns])
|
2 |
|
3 | merge provided options with defaults
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | ##### Parameters
|
9 |
|
10 | | Name | Type | Description | |
|
11 | | ---- | ---- | ----------- | -------- |
|
12 | | optns | `Object` | provided options | *Optional* |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | ##### Examples
|
18 |
|
19 | ```javascript
|
20 |
|
21 | const { withDefaults } = require('@lykmapipo/jwt-common');
|
22 | withDefaults({ secret: 'xo67Rw' }) // => { secret: 'xo67Rw', ...}
|
23 | ```
|
24 |
|
25 |
|
26 | ##### Returns
|
27 |
|
28 |
|
29 | - `Object` merged options with environment variables
|
30 |
|
31 |
|
32 |
|
33 | #### encode(payload[, opts], cb)
|
34 |
|
35 | encode given payload as jwt.
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | ##### Parameters
|
41 |
|
42 | | Name | Type | Description | |
|
43 | | ---- | ---- | ----------- | -------- |
|
44 | | payload | `Object` | data to encode. | |
|
45 | | opts | `Object` | jwt sign or encoding options. | *Optional* |
|
46 | | cb | `Function` | callback to invoke on success or failure. | |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | ##### Examples
|
52 |
|
53 | ```javascript
|
54 |
|
55 | const { encode } = require('@lykmapipo/jwt-common');
|
56 |
|
57 | const payload = { _id: 'xo5', permissions: ['user:read'] };
|
58 |
|
59 | // encode with default options
|
60 | encode(payload, (error, jwt) => { ... });
|
61 |
|
62 | // encode with merged options
|
63 | encode(payload, { secret: 'xo67Rw' }, (error, jwt) => { ... });
|
64 | ```
|
65 |
|
66 |
|
67 | ##### Returns
|
68 |
|
69 |
|
70 | - `String` `Error` jwt token if success or error.
|
71 |
|
72 |
|
73 |
|
74 | #### decode(token[, opts], cb)
|
75 |
|
76 | decode and verify given jwt.
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | ##### Parameters
|
82 |
|
83 | | Name | Type | Description | |
|
84 | | ---- | ---- | ----------- | -------- |
|
85 | | token | `String` | jwt token to decode. | |
|
86 | | opts | `Object` | jwt verify or decoding options. | *Optional* |
|
87 | | cb | `Function` | callback to invoke on success or failure. | |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | ##### Examples
|
93 |
|
94 | ```javascript
|
95 |
|
96 | const { decode } = require('@lykmapipo/jwt-common');
|
97 |
|
98 | const payload = { _id: 'xo5', permissions: ['user:read'] };
|
99 |
|
100 | // decode with default options
|
101 | decode(token, (error, payload) => { ... });
|
102 |
|
103 | // decode with provided options
|
104 | decode(token, { secret: 'xo67Rw' }, (error, payload) => { ... });
|
105 | ```
|
106 |
|
107 |
|
108 | ##### Returns
|
109 |
|
110 |
|
111 | - `Payload` `Error` payload if success or error.
|
112 |
|
113 |
|
114 |
|
115 | #### decodeJwtToUser([opts]) *private method*
|
116 |
|
117 | return a function used to decode jwt to user.
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | ##### Parameters
|
123 |
|
124 | | Name | Type | Description | |
|
125 | | ---- | ---- | ----------- | -------- |
|
126 | | opts | `Object` | decoding options. | *Optional* |
|
127 | | opts.user | `Functon` | | *Optional* |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | ##### Returns
|
133 |
|
134 |
|
135 | - `Function` jwt to user decoder
|
136 |
|
137 |
|
138 |
|
139 | #### parseJwtFromHttpHeaders(done)
|
140 |
|
141 | parse request headers to get jwt.
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | ##### Parameters
|
147 |
|
148 | | Name | Type | Description | |
|
149 | | ---- | ---- | ----------- | -------- |
|
150 | | done | `Function` | callback to invoke on success or failure. | |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | ##### Examples
|
156 |
|
157 | ```javascript
|
158 |
|
159 | const { parseJwtFromHttpHeaders } = require('@lykmapipo/jwt-common');
|
160 | parseJwtFromHttpHeaders(request, (error, jwt) => { ... });
|
161 | ```
|
162 |
|
163 |
|
164 | ##### Returns
|
165 |
|
166 |
|
167 | - `Payload` `Error` jwt if success or error.
|
168 |
|
169 |
|
170 |
|
171 | #### parseJwtFromHttpQueryParams(done)
|
172 |
|
173 | parse request headers to get jwt.
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | ##### Parameters
|
179 |
|
180 | | Name | Type | Description | |
|
181 | | ---- | ---- | ----------- | -------- |
|
182 | | done | `Function` | callback to invoke on success or failure. | |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | ##### Examples
|
188 |
|
189 | ```javascript
|
190 |
|
191 | const { parseJwtFromHttpQueryParams } = require('@lykmapipo/jwt-common');
|
192 | parseJwtFromHttpQueryParams(request, (error, jwt) => { ... });
|
193 | ```
|
194 |
|
195 |
|
196 | ##### Returns
|
197 |
|
198 |
|
199 | - `Payload` `Error` jwt if success or error.
|
200 |
|
201 |
|
202 |
|
203 | #### parseJwtFromHttpRequest(done)
|
204 |
|
205 | parse request headers to get jwt.
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | ##### Parameters
|
211 |
|
212 | | Name | Type | Description | |
|
213 | | ---- | ---- | ----------- | -------- |
|
214 | | done | `Function` | callback to invoke on success or failure. | |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | ##### Examples
|
220 |
|
221 | ```javascript
|
222 |
|
223 | const { parseJwtFromHttpRequest } = require('@lykmapipo/jwt-common');
|
224 | parseJwtFromHttpRequest(request, (error, jwt) => { ... });
|
225 | ```
|
226 |
|
227 |
|
228 | ##### Returns
|
229 |
|
230 |
|
231 | - `Payload` `Error` jwt if success or error.
|
232 |
|
233 |
|
234 |
|
235 | #### jwtAuth([opts])
|
236 |
|
237 | create middlware to authorize request using jwt
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | ##### Parameters
|
243 |
|
244 | | Name | Type | Description | |
|
245 | | ---- | ---- | ----------- | -------- |
|
246 | | opts | `Object` | jwt verify or decoding options. | *Optional* |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | ##### Examples
|
252 |
|
253 | ```javascript
|
254 |
|
255 | const { jwtAuth } = require('@lykmapipo/jwt-common');
|
256 |
|
257 | app.get('/users', jwtAuth({ secret: 'xo67Rw' }), (req, res, next) => { ... });
|
258 | ```
|
259 |
|
260 |
|
261 | ##### Returns
|
262 |
|
263 |
|
264 | - `Function` express compactoble middleware.
|
265 |
|
266 |
|
267 |
|
268 | #### jwtPermit(requiredScopes)
|
269 |
|
270 | create middlware to check request for jwt permissions(or scopes).
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | ##### Parameters
|
276 |
|
277 | | Name | Type | Description | |
|
278 | | ---- | ---- | ----------- | -------- |
|
279 | | requiredScopes | `Array.<String>` `String` | required scopes or permissions. | |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | ##### Examples
|
285 |
|
286 | ```javascript
|
287 |
|
288 | const { jwtPermit } = require('@lykmapipo/jwt-common');
|
289 |
|
290 | app.get('/users', jwtPermit('user:read'), (req, res, next) => { ... });
|
291 | ```
|
292 |
|
293 |
|
294 | ##### Returns
|
295 |
|
296 |
|
297 | - `Function` express compactoble middleware.
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | *Documentation generated with [doxdox](https://github.com/neogeek/doxdox).*
|