From 08b04a07aa399bc756f1a8a00ae17cf333be02d9 Mon Sep 17 00:00:00 2001 From: Xiyue Deng Date: Sun, 28 Jul 2024 03:41:20 -0700 Subject: [PATCH 5/6] Add debug messages and provide a switch variable for enabling This helps debugging whether the authorization and refresh requests were successful and inspecting the responses. * packages/oauth2/oauth2.el: add support for debug messages and a switch variable for enabling. --- oauth2.el | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/oauth2.el b/oauth2.el index 035971ac85..aca48f9ce1 100644 --- a/oauth2.el +++ b/oauth2.el @@ -40,6 +40,7 @@ (require 'plstore) (require 'json) (require 'url-http) +(require 'pp) (defvar url-http-data) (defvar url-http-method) @@ -53,6 +54,15 @@ :link '(url-link :tag "Savannah" "https://git.savannah.gnu.org/cgit/emacs/elpa.git/tree/?h=externals/oauth2") :link '(url-link :tag "ELPA" "https://elpa.gnu.org/packages/oauth2.html")) +(defcustom oauth2-debug nil + "Enable debug messages." + :type 'boolean) + +(defun oauth2--do-debug (&rest msg) + "Output debug messages when `oauth2-debug' is enabled." + (if oauth2-debug + (apply #'message msg))) + (defun oauth2-request-authorization (auth-url client-id &optional scope state redirect-uri) "Request OAuth authorization at AUTH-URL by launching `browse-url'. CLIENT-ID is the client id provided by the provider. @@ -79,6 +89,8 @@ It returns the code provided by the service." (defun oauth2-make-access-request (url data) "Make an access request to URL using DATA in POST." + (oauth2--do-debug "oauth2-make-access-request: url: %s" url) + (oauth2--do-debug "oauth2-make-access-request: data: %s" data) (let ((url-request-method "POST") (url-request-data data) (url-request-extra-headers @@ -86,6 +98,8 @@ It returns the code provided by the service." (with-current-buffer (url-retrieve-synchronously url) (let ((data (oauth2-request-access-parse))) (kill-buffer (current-buffer)) + (oauth2--do-debug "oauth2-make-access-request: response: %s" + (pp-to-string data)) data)))) (cl-defstruct oauth2-token @@ -158,10 +172,8 @@ TOKEN should be obtained with `oauth2-request-access'." auth-url client-id scope state redirect-uri) redirect-uri)) -(defcustom oauth2-token-file (concat user-emacs-directory "oauth2.plstore") - "File path where store OAuth tokens." - :group 'oauth2 - :type 'file) +(defvar oauth2-token-file (concat user-emacs-directory "oauth2.plstore") + "File path where store OAuth tokens.") (defun oauth2-compute-id (auth-url token-url scope client-id) "Compute an unique id based on URLs. -- 2.39.2