branch: externals/url-http-oauth commit e1408ed4062f8a3cda9f18d67aeea01e8fc4d4b6 Author: Thomas Fitzsimmons <fitz...@fitzsim.org> Commit: Thomas Fitzsimmons <fitz...@fitzsim.org>
Implement provider registration * url-http-oauth.el (url-http-oauth--registered-oauth-urls): Adjust doc string. (url-http-oauth-url-string): New function. (url-http-oauth-url-object): Likewise. (url-http-oauth-register-provider): Likewise. * url-http-oauth.el (url-oauth-auth): Fix byte compiler warning. --- url-http-oauth.el | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/url-http-oauth.el b/url-http-oauth.el index ba0916d30f..acbd40ffbb 100644 --- a/url-http-oauth.el +++ b/url-http-oauth.el @@ -32,15 +32,47 @@ (require 'url-http) (require 'url-util) +;; For evaluation during development: +;; (setq url-http-oauth--registered-oauth-urls nil) +;; (message "%S" url-http-oauth--registered-oauth-urls) (defvar url-http-oauth--registered-oauth-urls nil - "A hash table mapping URL strings to lists of OAuth 2.0 configuration items.") + "A hash table mapping URL strings to lists of OAuth 2.0 configuration.") + +(defun url-http-oauth-url-string (url) + "Ensure URL is a string." + (if (url-p url) (url-recreate-url url) url)) + +(defun url-http-oauth-url-object (url) + "Ensure URL is a parsed URL object." + (if (url-p url) url (url-generic-parse-url url))) + +;; Maybe if RFC 8414, "OAuth 2.0 Authorization Server Metadata", +;; catches on, authorize-url and access-token-url can be made +;; optional, and their values retrieved automatically. But from what +;; I can tell RFC 8414 is not consistently implemented yet. +(defun url-http-oauth-register-provider (url authorize-url access-token-url) + "Register URL as an OAuth 2.0 provider. +URL will be accessed by Emacs with a suitable \"Authorization\" +header containing \"Bearer <token>\". AUTHORIZE-URL and +ACCESS-TOKEN-URL will be used to acquire <token> and save it to +the user's `auth-source' file. URL and ACCESS-TOKEN-URL are +either URL structures or URL strings." + (unless url-http-oauth--registered-oauth-urls + (setq url-http-oauth--registered-oauth-urls + (make-hash-table :test #'equal))) + (let ((key (url-http-oauth-url-string url)) + (authorize (url-http-oauth-url-object authorize-url)) + (access-token (url-http-oauth-url-object access-token-url))) + (puthash key (list authorize access-token) + url-http-oauth--registered-oauth-urls))) ;;; Public function called by `url-get-authentication'. ;;;###autoload (defun url-oauth-auth (url &optional _prompt _overwrite _realm _args) "Return an OAuth 2.0 HTTP authorization header. URL is a structure representing a parsed URL." - nil) + ;; Do nothing for now. + (when url nil)) ;;; Register `url-oauth-auth' HTTP authentication method. ;;;###autoload