Guest User

Untitled

a guest
May 17th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. 'use strict';
  2. const AWS = require('aws-sdk');
  3.  
  4. exports.handler = (event, context, callback) => {
  5. const request = event.Records[0].cf.request;
  6. const headers = request.headers;
  7.  
  8. const body = 'Unauthorized';
  9. const response = {
  10. status: '401',
  11. statusDescription: 'Unauthorized',
  12. body: body,
  13. headers: {
  14. 'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]
  15. },
  16. };
  17.  
  18. if(typeof headers.authorization != 'undefined'){
  19. var decoded = new Buffer(headers.authorization[0].value.split(" ")[1], "base64").toString();
  20. console.log("decoded from auth header without basic: " + decoded);
  21. var arr = decoded.split(":");
  22. var user = arr[0];
  23. var pass = arr[1];
  24. console.log("user: " + user);
  25. console.log("pass: " + pass);
  26.  
  27. const cognito = new AWS.CognitoIdentityServiceProvider( 'us-east-1');
  28. var params = {
  29. AuthFlow: 'ADMIN_NO_SRP_AUTH',
  30. ClientId: [myclientid],
  31. UserPoolId: [myuserpoolid],
  32. AuthParameters: {
  33. USERNAME: user,
  34. PASSWORD: pass,
  35. },
  36. };
  37. cognito.adminInitiateAuth(params, function(err, data) {
  38. if(err){
  39. console.log("failed to authenticate");
  40. console.log(err, err.stack);
  41. callback(null, response);
  42. } else{
  43. console.log("successfully authenticated");
  44. console.log(data);
  45. callback(null, request);
  46. }
  47. });
  48. }
  49.  
  50. else{
  51. callback(null, response);
  52. }
  53. };
Add Comment
Please, Sign In to add comment