Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

82 Zeilen
2.3 KiB

  1. const cookie = require("cookie");
  2. const request = require("superagent");
  3. const { get_url } = require("../utils");
  4. const { get_conf } = require("../../node_utils");
  5. const conf = get_conf();
  6. function authenticate_with_xhiveframework(socket, next) {
  7. let namespace = socket.nsp.name;
  8. namespace = namespace.slice(1, namespace.length); // remove leading `/`
  9. if (namespace != get_site_name(socket)) {
  10. next(new Error("Invalid namespace"));
  11. }
  12. if (get_hostname(socket.request.headers.host) != get_hostname(socket.request.headers.origin)) {
  13. next(new Error("Invalid origin"));
  14. return;
  15. }
  16. if (!socket.request.headers.cookie) {
  17. next(new Error("No cookie transmitted."));
  18. return;
  19. }
  20. let cookies = cookie.parse(socket.request.headers.cookie || "");
  21. let authorization_header = socket.request.headers.authorization;
  22. if (!cookies.sid && !authorization_header) {
  23. next(new Error("No authentication method used. Use cookie or authorization header."));
  24. return;
  25. }
  26. let auth_req = request.get(get_url(socket, "/api/method/xhiveframework.realtime.get_user_info"));
  27. if (cookies.sid) {
  28. auth_req = auth_req.query({ sid: cookies.sid });
  29. } else {
  30. auth_req = auth_req.set("Authorization", authorization_header);
  31. }
  32. auth_req
  33. .type("form")
  34. .then((res) => {
  35. socket.user = res.body.message.user;
  36. socket.user_type = res.body.message.user_type;
  37. socket.sid = cookies.sid;
  38. socket.authorization_header = authorization_header;
  39. next();
  40. })
  41. .catch((e) => {
  42. next(new Error(`Unauthorized: ${e}`));
  43. });
  44. }
  45. function get_site_name(socket) {
  46. if (socket.site_name) {
  47. return socket.site_name;
  48. } else if (socket.request.headers["x-xhiveframework-site-name"]) {
  49. socket.site_name = get_hostname(socket.request.headers["x-xhiveframework-site-name"]);
  50. } else if (
  51. conf.default_site &&
  52. ["localhost", "127.0.0.1"].indexOf(get_hostname(socket.request.headers.host)) !== -1
  53. ) {
  54. socket.site_name = conf.default_site;
  55. } else if (socket.request.headers.origin) {
  56. socket.site_name = get_hostname(socket.request.headers.origin);
  57. } else {
  58. socket.site_name = get_hostname(socket.request.headers.host);
  59. }
  60. return socket.site_name;
  61. }
  62. function get_hostname(url) {
  63. if (!url) return undefined;
  64. if (url.indexOf("://") > -1) {
  65. url = url.split("/")[2];
  66. }
  67. return url.match(/:/g) ? url.slice(0, url.indexOf(":")) : url;
  68. }
  69. module.exports = authenticate_with_xhiveframework;