drupalsso.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * The SSO constructor.
  3. *
  4. * Notice that the returned SSO instance has asynchronous behavior: its state
  5. * component will only be initialized once the server callback has returned,
  6. * which will almost always be some milliseconds after the instance itself is
  7. * returned: check sso.state.online to ensure the connection attempts is done:
  8. * - undefined -> not yet
  9. * -> false -> failed, values are defaults,
  10. * -> true -> succeeded,valuers are those provided by the server.
  11. *
  12. * @returns {DrupalSSO}
  13. * @constructor
  14. */
  15. DrupalSSO = function () {
  16. // Called without `new`: call with it.
  17. if (!(this instanceof DrupalSSO)) {
  18. return new DrupalSSO();
  19. }
  20. // Work around "this" interpretation in local scope methods.
  21. var that = this;
  22. this.settings = {
  23. client: {}
  24. };
  25. this.state = {
  26. anonymousName: 'anome',
  27. cookieName: 'SESS___4___8__12__16__20__24__28__32',
  28. // Online is only set once the initialization has completed.
  29. online: undefined
  30. };
  31. var user = {
  32. uid: 0,
  33. name: 'undefined name',
  34. roles: ['anonymous user']
  35. };
  36. var userDep = new Tracker.Dependency();
  37. this.getUserId = function () {
  38. userDep.depend();
  39. return user.uid;
  40. };
  41. this.getUserName = function () {
  42. userDep.depend();
  43. return user.name;
  44. };
  45. this.getUserRoles = function () {
  46. userDep.depend();
  47. return user.roles;
  48. };
  49. /**
  50. * Update the user information from the Drupal server based on the cookies.
  51. *
  52. * @param {string} cookies
  53. */
  54. this.updateUser = function (cookies) {
  55. Meteor.call('drupal-sso.whoami', cookies, function (err, res) {
  56. if (err) {
  57. throw new Meteor.Error('whoami', err);
  58. }
  59. _.extend(user, res);
  60. userDep.changed();
  61. });
  62. };
  63. /**
  64. * Parse a cookie blob for value of the relevant session cookie.
  65. *
  66. * @param {string} cookieBlob
  67. * @returns {undefined}
  68. */
  69. this.getSessionCookie = function (cookieBlob) {
  70. cookieBlob = '; ' + cookieBlob;
  71. var cookieName = that.state.cookieName;
  72. var cookieValue;
  73. var cookies = cookieBlob.split('; ' + cookieName + "=");
  74. if (cookies.length == 2) {
  75. cookieValue = cookies.pop().split(';').shift();
  76. }
  77. return cookieValue;
  78. };
  79. /**
  80. * Initialize the server-only part of the SSO settings.
  81. *
  82. * This method needs to be invoked on the server-side SSO instance: it cannot
  83. * be invoked during construction, because the instance is not yet ready.
  84. *
  85. * @param {Object} settings
  86. */
  87. this.initServerState = function (settings) {
  88. that.settings['drupal-sso'] = settings['drupal-sso'];
  89. };
  90. // Constructor body:
  91. // - Merge public settings to instance.
  92. _.extend(that.settings.client, Meteor.settings.public);
  93. // - Initialize server-dependent state.
  94. Meteor.call('drupal-sso.initState', function (err, res) {
  95. if (err) {
  96. throw new Meteor.Error('init-state', err);
  97. }
  98. _.extend(that.state, res);
  99. if (Meteor.isClient) {
  100. that.updateUser(document.cookie);
  101. }
  102. });
  103. };