Use a version number AMO will like. 0_1_0_RELEASE
authorShawn Wilsher <me@shawnwilsher.com>
Fri, 23 Oct 2009 15:45:06 -0700
changeset 9 63baf8c88bfdf3a92da4944ea8f6e9b67bff5845
parent 8 b1ee8302dabe16db4a998a6378987ed75cc833a6
child 10 3fc9c3a0d2c9429516c3bca98ecc5e9482d5ab8e
push id6
push usersdwilsh@shawnwilsher.com
push dateSat, 24 Oct 2009 00:21:49 +0000
Use a version number AMO will like. Make saving passwords work.
content/reply.js
install.rdf
--- a/content/reply.js
+++ b/content/reply.js
@@ -1,21 +1,21 @@
 ////////////////////////////////////////////////////////////////////////////////
 //// Constants
 
+// DEBUG constants
 /*
+const kDestinationURI = "https://api-dev.bugzilla.mozilla.org/stage/0.1/";
+const kBugzillaURI = "https://bugzilla-stage-tip.mozilla.org/";
+*/
+
 const kDestinationURI = "https://api-dev.bugzilla.mozilla.org/0.1/";
-*/
-const kDestinationURI = "https://api-dev.bugzilla.mozilla.org/stage/0.1/";
+const kBugzillaURI = "https://bugzilla.mozilla.org/";
+
 const kFingerprint = "75:B2:EF:89:81:51:8F:99:13:DC:BF:44:47:0E:A9:8D";
-
-/*
-const kBugzillaURI = "https://bugzilla.mozilla.org/";
-*/
-const kBugzillaURI = "https://bugzilla-stage-tip.mozilla.org/";
 const kBugzillaRealm = "bugzilla-helper";
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Dialog Controller
 
 var ReplyDialog = {
   initialize: function RD_initialize()
   {
@@ -28,31 +28,36 @@ var ReplyDialog = {
     });
     comment.value = lines.join("\n") + "\n";
     comment.focus();
   },
 
   onAccept: function RD_onAccept()
   {
     // TODO handle offline!
-    let [username, password] = this.getLoginInformation();
+    let [username, password, store] = this.getLoginInformation();
     const auth = "username=" + username + "&password=" + password;
     let bugID = window.arguments[0];
     const URI = kDestinationURI + "bug/" + bugID + "/comment?" + auth;
     let req = new UntrustedHttpsRequest(kFingerprint);
     // XXX make async
     req.open("POST", URI, false);
     req.overrideMimeType("application/json");
     req.setRequestHeader("Accept", "application/json");
     req.send(ReplyDialog.createJSON());
-    if (req.status == 201) // created
+    if (req.status == 201) {
+      // HTTP/201 Created
+      if (store)
+        this.storePassword(username, password);
       return true;
+    }
 
     // TODO error handling!
-    alert(req.status + "\n" + req.responseText);
+    alert("Sorry, but an error occured!\n" + req.status + "\n" +
+          req.responseText);
     return false;
   },
 
   createJSON: function RD_createJSON()
   {
     let comment = document.getElementById("comment").value;
     let private = document.getElementById("private").checked;
     let data = {
@@ -61,47 +66,64 @@ var ReplyDialog = {
     };
     return JSON.stringify(data);
   },
 
   getLoginInformation: function RD_getLoginInformation()
   {
     // NOTE this isn't my actually password.  I changed it on the staging server
     // to be something weak.
+    let shouldStore = false;
     let [username, password] = this.getStoredPassword();
-    if (!username || !password)
+    if (!username || !password) {
+      shouldStore = true;
       [username, password] = this.promptForPassword();
+    }
 
-    return [username, password];
+    return [username, password, shouldStore];
   },
 
   getStoredPassword: function RD_getStoredPassword()
   {
     let lm = Components.classes["@mozilla.org/login-manager;1"]
                        .getService(Components.interfaces.nsILoginManager);
     let logins = lm.findLogins({}, kBugzillaURI, null, kBugzillaRealm);
     if (!logins.length)
-      return null;
+      return [null, null];
     return [logins[0].username, logins[0].password];
   },
 
   promptForPassword: function RD_promptForPassword()
   {
     let ps = Components.classes["@mozilla.org/network/default-prompt;1"]
                        .getService(Components.interfaces.nsIAuthPrompt);
     let username = { value: null };
     let password = { value: null };
     let result = ps.promptUsernameAndPassword(
-      "title",
-      "message",
+      "Bugzilla Authentication",
+      "We'll need your bugzilla username and password to carry on.",
       kBugzillaRealm,
       Components.interfaces.nsIAuthPrompt.SAVE_PASSWORD_PERMANENTLY,
       username,
       password
     );
     if (!result)
-      return null;
+      return [null, null];
 
     return [username.value, password.value];
   },
+
+  storePassword: function RD_storePassword(aUsername,
+                                           aPassword)
+  {
+    // Create the login.
+    let l = Components.classes["@mozilla.org/login-manager/loginInfo;1"]
+                      .createInstance(Components.interfaces.nsILoginInfo);
+    l.init(kBugzillaURI, null, kBugzillaRealm, aUsername, aPassword, "", "");
+
+    // And store it.
+    let lm = Components.classes["@mozilla.org/login-manager;1"]
+                       .getService(Components.interfaces.nsILoginManager);
+    lm.addLogin(l);
+  },
 };
 
 window.addEventListener("DOMContentLoaded", ReplyDialog.initialize, false);
--- a/install.rdf
+++ b/install.rdf
@@ -9,13 +9,13 @@
     <em:version>0.1</em:version>
     <em:type>2</em:type>
     <em:creator>Shawn Wilsher</em:creator>
 
     <em:targetApplication>
       <Description>
         <em:id>{3550f703-e582-4d05-9a08-453d09bdfdc6}</em:id>
         <em:minVersion>3.0b1</em:minVersion>
-        <em:maxVersion>3.0.0.*</em:maxVersion>
+        <em:maxVersion>3.0pre</em:maxVersion>
       </Description>
     </em:targetApplication>
   </Description>
 </RDF>