-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (51 loc) · 2.22 KB
/
index.js
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
const create = require('grpc-create-error')
/**
* Mali authorization metadata middleware.
* If the call has metadata with "authorization" string property the specified function is called
* @module @malijs/metadata-auth
*
* @param {Options} options
* @param {String|Object|Function} options.error optional Error creation options.
* If <code>String</code> the message for Error to throw in case
* authorization is not present.
* If <code>Object</code> the error options with <code>message</code>,
* <code>code</code>, and <code>metadata</code> properties. See <code>create-grpc-error</code>
* module.
* If <code>Function</code> a function with signature <code>(ctx)</code>
* called to create an error. Must return an <code>Error</code> instanse.
* Default: <code>"Not Authorized"</code>
* @param {Function} fn The middleware function to execute
*
* @example
* const auth = require('@malijs/metadata-auth')
*
* app.use(auth(async (authValue, ctx, next) => {
* console.log(authValue)
* await next()
* })
*/
module.exports = function (options, fn) {
if (typeof options === 'function') {
fn = options
options = {}
}
let errFn = errorGenerator
if (typeof options.error === 'string') {
errFn = () => errorGenerator(options.error)
} else if (typeof options.error === 'object') {
errFn = () => create(options.error.message || 'Not Authorized', options.error.code, options.error.metadata)
} else if (typeof options.error === 'function') {
errFn = options.error
}
return function auth (ctx, next) {
if (!ctx.metadata) throw errFn()
const keys = Object.keys(ctx.metadata)
if (!keys.length) throw errFn()
const key = keys.find(k => k === 'authorization')
if (!key) throw errFn()
return fn(ctx.metadata[key], ctx, next)
}
}
function errorGenerator (message) {
return new Error(message || 'Not Authorized')
}