Slide 24
Slide 24 text
FUNCTIONS AS A SERVICE
const {compose, withLog, handle} = require('linklet');
module.exports = compose(
withLog({json: true}),
withBasicAuth({validate: ({username, password}) => password === 'mypassword' ? {role: 'users'} : null})
)(handler);
async function handler(req) {
return {
user: req.user,
body: req.body
};
}
function withBasicAuth({validate = () => ({})}) {
return handler => async (req, res) => {
const authHeader = req.headers.authorization;
if (!authHeader.includes('Basic')) return handler(req, res);
const asString = Buffer.from(authHeader.replace('Basic ', ''), 'base64').toString('ascii');
const [username, password] = asString.split(':');
const validationResult = await validate({username, password});
if (!validationResult)
return handle(req, res, null, {
statusCode: 401,
status: 'Unauthorized',
message: 'Unauthorized'
});
req.user = {...validationResult, username};
return handler(req, res);
};
}
CUSTOM ENHANCEMENTS
LINKLET FAAS API
curl -s -u myusername:mypassword http://localhost:3000 | jq .