Guest User

Untitled

a guest
Jan 30th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. const USERNAME = 'USERNAME'
  2. const PASSWORD = 'PASSWORD'
  3. const denyAccess = (res) => {
  4. res.statusCode = 401;
  5. res.setHeader('WWW-Authenticate', 'Basic realm="Authorization
  6. Required');
  7. res.end('Unauthorized');
  8. }
  9.  
  10. exports.authorizeAccess = functions.https.onRequest((req, res) => {
  11. if (typeof req.headers.authorization !== 'string') {
  12. denyAccess(res);
  13. return;
  14. }
  15.  
  16. const base64Auth = req.headers.authorization.split(' ')[1];
  17. if (typeof base64Auth !== 'string' ) {
  18. denyAccess(res);
  19. return;
  20. }
  21.  
  22. const [user, pass] = Buffer.from(base64Auth,
  23. 'base64').toString().split(':');
  24. if (user !== USERNAME || pass !== PASSWORD) {
  25. denyAccess(res);
  26. return;
  27. }
  28.  
  29. const urlObject = url.parse(req.url);
  30. urlObject.pathname =
  31. `/${PASSWORD}${urlObject.pathname}`;
  32. const location = url.format(urlObject);
  33.  
  34. res.writeHead(302, { location });
  35. res.end();
  36. });
  37.  
  38. .
  39. ├── index.html
  40. ├── js
  41. | ├── main.js
  42. | └── main.js.map
  43. └── styles.css
  44.  
  45. .
  46. └── PASSWORD
  47. ├── index.html
  48. ├── js
  49. | ├── main.js
  50. | └── main.js.map
  51. └── styles.css
  52.  
  53. {
  54. ...
  55. "rewrites": {
  56. "source": "/"
  57. "function": "authorizeAccess"
  58. }
  59. ...
  60. }
Add Comment
Please, Sign In to add comment