From caae7e4ce42d652b75338c40a303b2601057d1e1 Mon Sep 17 00:00:00 2001
From: Samat K Jain <samat@samat.org>
Date: Mon, 15 Aug 2011 06:16:48 -0600
Subject: [PATCH] Support BrowserID session API (bug #1249746)

---
 browserid.info   |    2 ++
 browserid.js     |   50 +++++++++++++++++++++++++++++++++-----------------
 browserid.module |   11 ++++++++++-
 3 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/browserid.info b/browserid.info
index 7bba6c7..c881f91 100644
--- a/browserid.info
+++ b/browserid.info
@@ -5,3 +5,5 @@ configure = admin/config/browserid
 
 files[] = browserid.module
 files[] = browserid.install
+
+scripts[] = browserid.js
diff --git a/browserid.js b/browserid.js
index 840210d..9ff1bb3 100644
--- a/browserid.js
+++ b/browserid.js
@@ -1,25 +1,41 @@
 (function ($) {
 
 Drupal.behaviors.browserid = {
+  performLogin: function(context) {
+    navigator.id.getVerifiedEmail(function(assertion) {
+      if (assertion) {
+        $.post(Drupal.settings.basePath +'index.php?q=browserid/verify&assertion=' + assertion + '&audience=' + window.location.host, function (data) {
+          if (data.reload) {
+            window.location.reload();
+          }
+          else if (data.destination) {
+            window.location.href = data.destination;
+          }
+          else {
+            alert(Drupal.t('An unknown error occurred while attempting to validate your BrowserID login. Sending you to the conventional login page.'));
+            document.location.href = Drupal.settings.basePath + 'user/login';
+          }
+        });
+      }
+    });
+  },
   attach: function (context) {
     $(context).find('.browserid-button').show();
-    $(context).find('.browserid-button').click(function() {
-      navigator.id.getVerifiedEmail(function(assertion) {
-        if (assertion) {
-          $.post(Drupal.settings.basePath +'index.php?q=browserid/verify&assertion=' + assertion + '&audience=' + window.location.host, function (data) {
-            if (data.reload) {
-              window.location.reload();
-            }
-            else if (data.destination) {
-              window.location.href = data.destination;
-            }
-            else {
-              alert(Drupal.t('An unknown error occurred while attempting to validate your BrowserID login.'));
-            }
-          });
-        }
-      });
-    });
+    $(context).find('.browserid-button').click(Drupal.behaviors.browserid.performLogin);
+
+    if (navigator.id) {
+      if (Drupal.settings.browserid)
+        navigator.id.sessions = [{ email: Drupal.settings.browserid.email }];
+      else
+        navigator.id.sessions = [];
+  
+      document.addEventListener('login', function(e) {
+        Drupal.behaviors.browserid.performLogin();
+      }, false);
+      document.addEventListener('logout', function(e) {
+        document.location.href = Drupal.settings.basePath + 'user/logout';
+      }, false);
+    }
   }
 }
 
diff --git a/browserid.module b/browserid.module
index f1d9a48..9fcd9a8 100644
--- a/browserid.module
+++ b/browserid.module
@@ -5,6 +5,16 @@
  *   Adds Mozilla BrowserID support to Drupal.
  */
 
+function browserid_init() {
+  // Send e-mail address for display in BrowserID browser chrome
+  if (user_is_logged_in()) {
+    $browserid_settings = array(
+      'email' => $GLOBALS['user']->mail
+    );
+    drupal_add_js(array('browserid' => $browserid_settings), 'setting');
+  }
+}
+
 /**
  * Implements hook_menu().
  */
@@ -87,7 +97,6 @@ function browserid_block_view($delta = '') {
 function browserid_login_button($type = NULL, $all = FALSE) {
   $path = drupal_get_path('module', 'browserid');
   drupal_add_js('https://browserid.org/include.js', 'external');
-  drupal_add_js($path . '/browserid.js');
   if (empty($type)) {
     $type = variable_get('browserid_button', 'blue');
   }
-- 
1.7.4.1

