From 2a6568d3d9a7b121a4ee760eea0dfb2f91f79fb1 Mon Sep 17 00:00:00 2001 From: Ian Eure Date: Sun, 3 May 2020 10:46:53 -0700 Subject: Rewrite `mastodon-auth--access-token` so it handles errors. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, `mastodon-auth--access-token` unconditionally returns the value of the `:access_token` key from the response of `(mastodon-auth--get-token)`. This causes problems when there was an error getting the token, for example, if you enter the wrong password. If a token couldn’t be retrieved, the JSON looks like: (:error "invalid_grant" :error_description "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.") Since there is no `:access_token` key, `mastodon-auth--access-token` returns `nil`, which results in a broken header in the next request: Authorization: Bearer Which causes the whole thing to freeze Emacs until you mash `C-g`. This commit rewrites the function to handle that case; to explicitly signal an error for *any* response that isn’t expected; to use `if-let`, which allows the temporary `token` variable to be eliminated; uses `pcase` to determine what kind of response was received; and adds ERT tests for all these cases. --- lisp/mastodon-auth.el | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'lisp/mastodon-auth.el') diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el index 231bb70..cfe89b5 100644 --- a/lisp/mastodon-auth.el +++ b/lisp/mastodon-auth.el @@ -124,13 +124,22 @@ Reads and/or stores secres in `MASTODON-AUTH-SOURCE-FILE'." "Return the access token to use with the current `mastodon-instance-url'. Generate token and set if none known yet." - (let ((token - (cdr (assoc mastodon-instance-url mastodon-auth--token-alist)))) - (unless token - (let ((json (mastodon-auth--get-token))) - (setq token (plist-get json :access_token)) - (push (cons mastodon-instance-url token) mastodon-auth--token-alist))) - token)) + (if-let ((token (cdr (assoc mastodon-instance-url mastodon-auth--token-alist)))) + token + + (mastodon-auth--handle-token-response (mastodon-auth--get-token)))) + +(defun mastodon-auth--handle-token-response (response) + (pcase response + ((and (let token (plist-get response :access_token)) + (guard token)) + (cdar (push (cons mastodon-instance-url token) + mastodon-auth--token-alist))) + + (`(:error ,class :error_description ,error) + (error "mastodon-auth--access-token: %s: %s" class error)) + + (_ (error "Unknown response from mastodon-auth--get-token!")))) (defun mastodon-auth--get-account-name () "Request user credentials and return an account name." -- cgit v1.2.3