Browse Source

Implemented whoami, works from client.

Frederic G. MARAND 8 years ago
parent
commit
71f6b78273
2 changed files with 65 additions and 4 deletions
  1. 31 1
      lib/drupalsso.js
  2. 34 3
      server/sso.js

+ 31 - 1
lib/drupalsso.js

@@ -55,6 +55,22 @@ DrupalSSO = function () {
     return user.roles;
   };
 
+  /**
+   * Update the user information from the Drupal server based on the cookies.
+   *
+   * @param {string} cookies
+   */
+  this.updateUser = function (cookies) {
+    Meteor.call('drupal-sso.whoami', cookies, function (err, res) {
+      if (err) {
+        throw new Meteor.Error('whoami', err);
+      }
+
+      _.extend(user, res);
+      userDep.changed();
+    });
+  };
+
   /**
    * Parse a cookie blob for value of the relevant session cookie.
    *
@@ -75,9 +91,23 @@ DrupalSSO = function () {
     return cookieValue;
   };
 
-  // Constructor body.
+  /**
+   * Initialize the server-only part of the SSO settings.
+   *
+   * This method needs to be invoked on the server-side SSO instance: it cannot
+   * be invoked during construction, because the instance is not yet ready.
+   *
+   * @param {Object} settings
+   */
+  this.initServerState = function (settings) {
+    that.settings['drupal-sso'] = settings['drupal-sso'];
+  };
+
+  // Constructor body:
+  // - Merge public settings to instance.
   _.extend(that.settings.client, Meteor.settings.public);
 
+  // - Initialize server-dependent state.
   Meteor.call('drupal-sso.initState', function (err, res) {
     if (err) {
       throw new Meteor.Error('init-state', err);

+ 34 - 3
server/sso.js

@@ -1,6 +1,8 @@
 // Cause an error on startup if the Drupal server is unreachable.
 Meteor.startup(function () {
   sso = new DrupalSSO();
+  sso.initServerState(Meteor.settings);
+
   if (!sso.state.online) {
     throw new Meteor.Error('startup', "Could not reach the Drupal server.");
   }
@@ -43,10 +45,39 @@ Meteor.methods({
       };
       Meteor._debug("Error: ", err);
     }
-    Meteor._debug("initState returning", info);
     return info;
   },
 
-  "drupal-sso.whoami": function () {
-  },
+  /**
+   *
+   * @param {string} cookieBlob
+   * @returns {*}
+   */
+  "drupal-sso.whoami": function (cookieBlob) {
+    // sso is a package global, initialized in server/sso.js Meteor.startup().
+    var cookieName = sso.state.cookieName;
+    var cookieValue = sso.getSessionCookie(cookieBlob);
+    var url = sso.settings['drupal-sso'].site + "/meteor/whoami";
+
+    var options = {
+      headers: {
+        'cookie': cookieName + '=' + cookieValue
+      }
+    };
+    Meteor._debug('Checking ' + cookieName + "=" + cookieValue + ' on ' + url);
+    try {
+      var ret = HTTP.get(url, options);
+      info = JSON.parse(ret.content);
+    }
+    catch (err) {
+      info = {
+        'uid': 0,
+        'name': 'Unresolved',
+        'roles': []
+      };
+      Meteor._debug("Error: ", err);
+    }
+
+    return info;
+  }
 });