UNPKG

886 BJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const base64 = require('js-base64').Base64;
3
4const _instance = Joi.string();
5const _email = Joi.string().email();
6const _password = Joi.string().min(1);
7const _token = Joi.string().min(1);
8
9module.exports = (options = {}) => {
10 const { error } = Joi.object({
11 instance: _instance,
12 email: _email.required(),
13 password: _password,
14 token: _token
15 })
16 .xor('password', 'token')
17 .validate(options);
18 if (error) throw new Error(error.details[0].message);
19
20 // Allow for either 'password' or 'token' for authentication
21 const { email, password, token } = options;
22 const auth = token ? `${email}/token:${token}` : `${email}:${password}`;
23 const encoded = base64.encode(auth);
24
25 // default to 'application/json', override when needed
26 return {
27 'Content-Type': 'application/json',
28 Authorization: `Basic ${encoded}`
29 };
30};