drupalsso.js 3.3 KB

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