aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-auth.el
blob: 86665f3aa55befaffb4ea9386b13bf5754d6c446 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(defun mastodon--read-access-token-file ()
  (let* ((plstore (plstore-open mastodon-token-file))
         (plist (cdr (plstore-get plstore "mastodon"))))
    (if (plist-get plist :access_token)
        plist)))

(defun mastodon--access-token-p ()
  (if (mastodon--get-client-token)
      t))

(defun mastodon--get-client-token ()
  (plist-get mastodon--client-plist :access_token))

(defun mastodon--read-or-get-access-token ()
  (if (mastodon--access-token-p)
      (mastodon--get-client-token)
    (progn
      (mastodon--client-id-and-secret)
      (sleep-for 1) ;; FIXME
      (mastodon--access-token)
      (sleep-for 2) ;; FIXME
      (mastodon--store-access-token-file)
      (mastodon--get-client-token))))

(defun mastodon--client-id-and-secret ()
  "Adds client_id and client_secret to `mastodon--client-plist'."
  (mastodon--http-post (mastodon--api-for "apps")
                       (lambda (status) (let ((client-data (mastodon--json-hash-table status)))
                                          (setq mastodon--client-plist `(:client_id ,(gethash "client_id" client-data) :client_secret ,(gethash "client_secret" client-data)))))
                       '(("client_name" . "mastodon.el")
                         ("redirect_uris" . "urn:ietf:wg:oauth:2.0:oob")
                         ("scopes" . "read write follow"))))

(defun mastodon--store-access-token-file ()
  (let ((plstore (plstore-open mastodon-token-file)))
    (plstore-put plstore "mastodon" nil
                 `(:client_id
                   ,(plist-get mastodon--client-plist :client_id)
                   :client_secret
                   ,(plist-get mastodon--client-plist :client_secret)
                   :access_token
                   ,(plist-get mastodon--client-plist :access_token)))
    (plstore-save plstore)))

(defun mastodon--access-token ()
  "Adds access_token to `mastodon--client-plist'."
  (mastodon--http-post (concat mastodon-instance-url "/oauth/token")
                       (lambda (status) (let ((token-data (mastodon--json-hash-table status)))
                                          (setq mastodon--client-plist (plist-put mastodon--client-plist :access_token (gethash "access_token" token-data)))))
                       `(("client_id" . ,(plist-get mastodon--client-plist :client_id))
                         ("client_secret" . ,(plist-get mastodon--client-plist :client_secret))
                         ("grant_type" . "password")
                         ("username" . ,(read-string "Email: "))
                         ("password" . ,(read-passwd "Password: ")))))