Browse Source

Add support for anonymous login.

JC Brand 10 years ago
parent
commit
4d3b4380dd
5 changed files with 81 additions and 44 deletions
  1. 45 21
      converse.js
  2. 5 5
      css/converse.css
  3. 3 0
      dev.html
  4. 14 12
      sass/converse.scss
  5. 14 6
      src/templates/login_panel.html

+ 45 - 21
converse.js

@@ -182,6 +182,10 @@
 
         // Constants
         // ---------
+        var MANUAL = "manual";
+        var ANONYMOUS  = "anonymous";
+        var PREBIND = "prebind";
+
         var UNENCRYPTED = 0;
         var UNVERIFIED= 1;
         var VERIFIED= 2;
@@ -256,7 +260,8 @@
             message_carbons: false,
             no_trimming: false, // Set to true for phantomjs tests (where browser apparently has no width)
             play_sounds: false,
-            prebind: false,
+            authentication: 'manual', // Available values are "manual", "prebind", "anonymous".
+            prebind: false, // XXX: Deprecated, use "authentication" instead.
             prebind_url: null,
             providers_link: 'https://xmpp.net/directory.php', // Link to XMPP providers shown on registration page
             rid: undefined,
@@ -284,6 +289,9 @@
         // Allow only whitelisted configuration attributes to be overwritten
         _.extend(this, _.pick(settings, Object.keys(this.default_settings)));
 
+        // BBB
+        if (this.prebind === true) { this.authentication = PREBIND; }
+
         if (settings.visible_toolbar_buttons) {
             _.extend(
                 this.visible_toolbar_buttons,
@@ -431,7 +439,7 @@
 
         this.reconnect = function () {
             converse.giveFeedback(__('Reconnecting'), 'error');
-            if (!converse.prebind) {
+            if (converse.authentication !== "prebind") {
                 this.connection.connect(
                     this.connection.jid,
                     this.connection.pass,
@@ -690,7 +698,7 @@
         this.OTR = Backbone.Model.extend({
             // A model for managing OTR settings.
             getSessionPassphrase: function () {
-                if (converse.prebind) {
+                if (converse.authentication === 'prebind') {
                     var key = b64_sha1(converse.connection.jid),
                         pass = window.sessionStorage[key];
                     if (typeof pass === 'undefined') {
@@ -5149,8 +5157,13 @@
             initialize: function (cfg) {
                 cfg.$parent.html(this.$el.html(
                     converse.templates.login_panel({
+                        'MANUAL': MANUAL,
+                        'ANONYMOUS': ANONYMOUS,
+                        'PREBIND': PREBIND,
+                        'authentication': converse.authentication,
                         'label_username': __('XMPP Username:'),
                         'label_password': __('Password:'),
+                        'label_anon_login': __('Click here to log in anonymously'),
                         'label_login': __('Log In')
                     })
                 ));
@@ -5168,8 +5181,16 @@
 
             authenticate: function (ev) {
                 if (ev && ev.preventDefault) { ev.preventDefault(); }
-                var $form = $(ev.target),
-                    $jid_input = $form.find('input[name=jid]'),
+                var $form = $(ev.target);
+                if (converse.authentication === ANONYMOUS) {
+                    if (!converse.jid) {
+                        throw("Config Error: you need to provide the server's domain via the " +
+                              "'jid' option when using anonymous authentication.");
+                    }
+                    this.connect($form, converse.jid, null);
+                    return;
+                }
+                var $jid_input = $form.find('input[name=jid]'),
                     jid = $jid_input.val(),
                     $pw_input = $form.find('input[name=password]'),
                     password = $pw_input.val(),
@@ -5198,12 +5219,15 @@
             },
 
             connect: function ($form, jid, password) {
+                var resource;
                 if ($form) {
                     $form.find('input[type=submit]').hide().after('<span class="spinner login-submit"/>');
                 }
-                var resource = Strophe.getResourceFromJid(jid);
-                if (!resource) {
-                    jid += '/converse.js-' + Math.floor(Math.random()*139749825).toString();
+                if (jid) {
+                    resource = Strophe.getResourceFromJid(jid);
+                    if (!resource) {
+                        jid += '/converse.js-' + Math.floor(Math.random()*139749825).toString();
+                    }
                 }
                 converse.connection.connect(jid, password, converse.onConnect);
             },
@@ -5321,33 +5345,25 @@
                 this.onConnected();
             } else {
                 if (!this.bosh_service_url && ! this.websocket_url) {
-                    throw("Error: you must supply a value for the bosh_service_url or websocket_url");
+                    throw("Config Error: you must supply a value for the bosh_service_url or websocket_url");
                 }
                 if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
                     this.connection = new Strophe.Connection(this.websocket_url);
                 } else if (this.bosh_service_url) {
                     this.connection = new Strophe.Connection(this.bosh_service_url);
                 } else {
-                    throw("Error: this browser does not support websockets and no bosh_service_url specified.");
+                    throw("Config Error: this browser does not support websockets and no bosh_service_url specified.");
                 }
                 this.setUpXMLLogging();
 
-                if (this.prebind) {
-                    if (this.jid && this.sid && this.rid) {
-                        this.connection.attach(this.jid, this.sid, this.rid, this.onConnect);
-                    }
-                    if (!this.keepalive) {
-                        throw("If you use prebind and don't use keepalive, "+
-                              "then you MUST supply JID, RID and SID values");
-                    }
-                }
                 if (this.keepalive) {
                     rid = this.session.get('rid');
                     sid = this.session.get('sid');
                     jid = this.session.get('jid');
-                    if (this.prebind) {
+                    if (this.authentication === "prebind") {
                         if (!this.jid) {
-                            throw("When using 'keepalive' with 'prebind, you must supply the JID of the current user.");
+                            throw("Config Eror: When using 'keepalive' with authentication='prebind', " +
+                                  "you must supply the JID of the current user.");
                         }
                         if (rid && sid && jid && Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(this.jid)) {
                             this.session.save({rid: rid}); // The RID needs to be increased with each request.
@@ -5365,6 +5381,14 @@
                             this.connection.attach(jid, sid, rid, this.onConnect);
                         }
                     }
+                } else if (this.authentication == "prebind") {
+                    // prebind is used without keepalive
+                    if (this.jid && this.sid && this.rid) {
+                        this.connection.attach(this.jid, this.sid, this.rid, this.onConnect);
+                    } else {
+                        throw("Config Error: If you use authentication='prebind' and don't use keepalive, "+
+                              "then you MUST supply JID, RID and SID values");
+                    }
                 }
             }
         };

+ 5 - 5
css/converse.css

@@ -977,15 +977,15 @@
     font-weight: bold;
     height: auto;
     margin: 4px; }
-  #conversejs #converse-register .login-submit,
-  #conversejs #converse-login .login-submit,
-  #conversejs #converse-register .submit,
-  #conversejs #converse-login .submit {
+  #conversejs #converse-register .login-submit, #conversejs #converse-register .submit, #conversejs #converse-login .login-submit, #conversejs #converse-login .submit {
     height: 30px;
     padding: 0px;
     font-size: 14px; }
-  #conversejs #converse-login .submit {
+  #conversejs #converse-register .submit, #conversejs #converse-login .submit {
     margin: 1em 0; }
+  #conversejs #converse-register .login-anon, #conversejs #converse-login .login-anon {
+    height: auto;
+    white-space: normal; }
   #conversejs form.add-chatroom {
     background: none;
     padding: 0.5em; }

+ 3 - 0
dev.html

@@ -52,6 +52,9 @@
 <script>
     require(['converse'], function (converse) {
         converse.initialize({
+            allow_registration: false,
+            authentication: "anonymous",
+            jid: "opkode.im",
             bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
             i18n: locales['en'], // Refer to ./locale/locales.js to see which locales are supported
             keepalive: true,

+ 14 - 12
sass/converse.scss

@@ -1156,19 +1156,21 @@
     margin: 4px;
   }
 
-  #converse-register .login-submit,
-  #converse-login .login-submit,
-  #converse-register .submit,
-  #converse-login .submit {
-    height: 30px;
-    padding: 0px;
-    font-size: $font-size;
-  }
-
-  #converse-login .submit {
-    margin: 1em 0;
+  #converse-register, #converse-login {
+    .login-submit, .submit {
+      height: 30px;
+      padding: 0px;
+      font-size: $font-size;
+    }
+    .submit {
+      margin: 1em 0;
+    }
+    .login-anon {
+      height: auto;
+      white-space: normal;
+    }
   }
-
+  
   form.add-chatroom {
     background: none;
     padding: 0.5em;

+ 14 - 6
src/templates/login_panel.html

@@ -1,8 +1,16 @@
 <form id="converse-login" method="post">
-    <label>{{label_username}}</label>
-    <input type="email" name="jid" placeholder="user@server">
-    <label>{{label_password}}</label>
-    <input type="password" name="password" placeholder="password">
-    <input class="submit" type="submit" value="{{label_login}}">
-    <span class="conn-feedback"></span>
+    {[ if (authentication == MANUAL) { ]}
+        <label>{{label_username}}</label>
+        <input type="email" name="jid" placeholder="user@server">
+        <label>{{label_password}}</label>
+        <input type="password" name="password" placeholder="password">
+        <input class="submit" type="submit" value="{{label_login}}">
+        <span class="conn-feedback"></span>
+    {[ } ]}
+    {[ if (authentication == ANONYMOUS) { ]}
+        <input type="submit" class="submit login-anon" value="{{label_anon_login}}"/>
+    {[ } ]}
+    {[ if (authentication == PREBIND) { ]}
+        <p>Disconnected.</p>
+    {[ } ]}
 </form>