Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const AWS = require('aws-sdk');
- exports.handler = (event, context, callback) => {
- const request = event.Records[0].cf.request;
- const headers = request.headers;
- const body = 'Unauthorized';
- const response = {
- status: '401',
- statusDescription: 'Unauthorized',
- body: body,
- headers: {
- 'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]
- },
- };
- if(typeof headers.authorization != 'undefined'){
- var decoded = new Buffer(headers.authorization[0].value.split(" ")[1], "base64").toString();
- console.log("decoded from auth header without basic: " + decoded);
- var arr = decoded.split(":");
- var user = arr[0];
- var pass = arr[1];
- console.log("user: " + user);
- console.log("pass: " + pass);
- const cognito = new AWS.CognitoIdentityServiceProvider( 'us-east-1');
- var params = {
- AuthFlow: 'ADMIN_NO_SRP_AUTH',
- ClientId: [myclientid],
- UserPoolId: [myuserpoolid],
- AuthParameters: {
- USERNAME: user,
- PASSWORD: pass,
- },
- };
- cognito.adminInitiateAuth(params, function(err, data) {
- if(err){
- console.log("failed to authenticate");
- console.log(err, err.stack);
- callback(null, response);
- } else{
- console.log("successfully authenticated");
- console.log(data);
- callback(null, request);
- }
- });
- }
- else{
- callback(null, response);
- }
- };
Add Comment
Please, Sign In to add comment