diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/mastodon-auth.el | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el index 17f19e3..aa83fae 100644 --- a/lisp/mastodon-auth.el +++ b/lisp/mastodon-auth.el @@ -30,6 +30,7 @@ ;;; Code: (require 'plstore) +(require 'auth-source) (autoload 'mastodon-client "mastodon-client") (autoload 'mastodon-http--api "mastodon-http") @@ -42,6 +43,15 @@ :prefix "mastodon-auth-" :group 'mastodon) +(defcustom mastodon-auth-source-file "" + "Filename to use to store user names and passwords. + +Leave empty to not permanently store any secrets. +Otherwise set to e.g. \"~/.authinfo.gpg\" to have encrypted storage, or +if you are happy with unencryped storage use e.g. \"~/authinfo\"." + :group 'mastodon-auth + :type 'string) + (defvar mastodon-auth--token-alist nil "Alist of User access tokens keyed by instance url.") @@ -50,6 +60,13 @@ (defun mastodon-auth--generate-token () "Make POST to generate auth token." + (if (or (null mastodon-auth-source-file) + (string= "" mastodon-auth-source-file)) + (mastodon-auth--generate-token-no-storing-credentials) + (mastodon-auth--generate-token-and-store))) + +(defun mastodon-auth--generate-token-no-storing-credentials () + "Make POST to generate auth token." (mastodon-http--post (concat mastodon-instance-url "/oauth/token") `(("client_id" . ,(plist-get (mastodon-client) :client_id)) @@ -61,6 +78,36 @@ nil :unauthenticated)) +(defun mastodon-auth--generate-token-and-store () + "Make POST to generate auth token. + +Reads and/or stores secres in `MASTODON-AUTH-SOURCE-FILE'." + (let* ((auth-sources (list mastodon-auth-source-file)) + (auth-source-creation-prompts + '((user . "Enter email for %h: ") + (secret . "Password: "))) + (credentials-plist (nth 0 (auth-source-search + :create t + :host mastodon-instance-url + :port 443 + :require '(:user :secret))))) + (prog1 + (mastodon-http--post + (concat mastodon-instance-url "/oauth/token") + `(("client_id" . ,(plist-get (mastodon-client) :client_id)) + ("client_secret" . ,(plist-get (mastodon-client) :client_secret)) + ("grant_type" . "password") + ("username" . ,(plist-get credentials-plist :user)) + ("password" . ,(let ((secret (plist-get credentials-plist :secret))) + (if (functionp secret) + (funcall secret) + secret))) + ("scope" . "read write follow")) + nil + :unauthenticated) + (when (functionp (plist-get credentials-plist :save-function)) + (funcall (plist-get credentials-plist :save-function)))))) + (defun mastodon-auth--get-token () "Make auth token request and return JSON response." (with-current-buffer (mastodon-auth--generate-token) |