-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (62 loc) · 2.71 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const auth = require('@malijs/metadata-auth')
const create = require('grpc-create-error')
/**
* Generic Mali metadata field authorization middleware
* If the call has metadata with "authorization" string property with <code>"{field} {value}"</code> then specified function is called.
* @module @malijs/metadata-field-auth
*
* @param {String} field Field within the authorization metadata value to look for.
* @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 with signature <code>(key, ctx, next)</code>
*
* @example
* const fieldAuth = require('@malijs/metadata-field-auth')
*
* app.use(fieldAuth('secret', async (key, ctx, next) => {
* console.log(key)
* await next()
* })
*/
module.exports = function (field, options, fn) {
if (!field || typeof field !== 'string') {
throw new Error('field must be a string')
}
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 auth(options, (authorization, ctx, next) => {
if (!authorization) throw errFn()
const parts = authorization.split(' ')
if (parts.length !== 2) throw errFn()
const scheme = parts[0]
const credentials = parts[1]
let key
const rstr = String.raw`^${field}$`
if (new RegExp(rstr, 'i').test(scheme)) {
key = credentials
}
if (!key) throw errFn()
return fn(key, ctx, next)
})
}
function errorGenerator (message) {
return new Error(message || 'Not Authorized')
}