Query the user for whether to store the credentials.
authorLars Magne Ingebrigtsen <larsi@quimbies.gnus.org>
Sun, 26 Sep 2010 12:45:59 +0000 (14:45 +0200)
committerLars Magne Ingebrigtsen <larsi@quimbies.gnus.org>
Sun, 26 Sep 2010 12:45:59 +0000 (14:45 +0200)
lisp/ChangeLog
lisp/auth-source.el
lisp/netrc.el

index bce9708..367efe3 100644 (file)
@@ -1,5 +1,10 @@
 2010-09-26  Lars Magne Ingebrigtsen  <larsi@gnus.org>
 
+       * auth-source.el (auth-source-create): Query the user for whether to
+       store the credentials.
+
+       * netrc.el (netrc-store-data): New function.
+
        * auth-source.el (auth-source-user-or-password): Use the existing auth
        sources, if any, for creation.
 
index c0464e2..b212f9f 100644 (file)
@@ -312,25 +312,41 @@ Return structure as specified by MODE."
     (setq result
          (mapcar
           (lambda (m)
-            (cond
-             ((equal "password" m)
-              (let ((passwd (read-passwd
-                             (format "Password for %s on %s: " prot host))))
-                (cond
-                 ;; Secret Service API.
-                 ((consp source)
-                  (apply
-                   'secrets-create-item
-                   (auth-get-source entry) name passwd spec))
-                 (t)) ;; netrc not implemented yes.
-                passwd))
-             ((equal "login" m)
-              (or user
-                  (read-string (format "User name for %s on %s: " prot host))))
-             (t
-              "unknownuser")))
+            (cons
+             m
+             (cond
+              ((equal "password" m)
+               (let ((passwd (read-passwd
+                              (format "Password for %s on %s: " prot host))))
+                 (cond
+                  ;; Secret Service API.
+                  ((consp source)
+                   (apply
+                    'secrets-create-item
+                    (auth-get-source entry) name passwd spec))
+                  (t)) ;; netrc not implemented yes.
+                 passwd))
+              ((equal "login" m)
+               (or user
+                   (read-string (format "User name for %s on %s: " prot host))))
+              (t
+               "unknownuser"))))
           (if (consp mode) mode (list mode))))
-    (if (consp mode) result (car result))))
+    ;; Allow the source to save the data.
+    (cond
+     ((consp source)
+      ;; Secret Service API -- not implemented.
+      )
+     (t
+      ;; netrc interface.
+      (when (y-or-n-p (format "Do you want to save this password in %s? "
+                             source))
+       (netrc-store-data source host prot
+                         (or user (cdr (assoc "login" result)))
+                         (cdr (assoc "password" result))))))
+    (if (consp mode)
+       (mapcar #'cdr result)
+      (cdar result))))
 
 (defun auth-source-delete (entry &rest spec)
   "Delete credentials according to SPEC in ENTRY."
index 2644659..d76b8cf 100644 (file)
@@ -220,6 +220,17 @@ MODE can be \"login\" or \"password\", suitable for passing to
                          (eq type (car (cddr service)))))))
     (cadr service)))
 
+(defun netrc-store-data (file host port user password)
+  (with-temp-buffer
+    (when (file-exists-p file)
+      (insert-file-contents file))
+    (goto-char (point-max))
+    (unless (bolp)
+      (insert "\n"))
+    (insert (format "machine %s login %s password %s port %s\n"
+                   host user password port))
+    (write-region (point-min) (point-max) file nil 'silent)))
+
 ;;;###autoload
 (defun netrc-credentials (machine &rest ports)
   "Return a user name/password pair.