How does the emacs url package handle authentication?

I have not seen a really good example on the Internet. How to add authentication to the following request:

(defun login-show-posts () (interactive) (let ((url-request-method "GET") (url-request-extra-headers '(("Content-Type" . "application/xml")))) (url-retrieve "http://localhost:3000/essay/1.xml" (lambda (status) (switch-to-buffer (current-buffer)) )))) 

if, for example, the user and password are admin: admin?

+4
source share
2 answers

I got the impression that url.el was designed primarily for interactive operations, i.e. you make a call without authorization, the server responds 403 with “authorization required” (correct code?), and url.el will ask for a user for a username and password.

You can see my code at http://github.com/hdurer/fluiddb.el , where I am trying to do something programmatically.

Basically, I create the HTTP author’s header myself (base64 encodes a correctly formatted string and adds the correct header to url-request-extra-headers ). Then, in the second step, I need to add the url-http-handle-authentication tip so that it doesn’t ask the user that the credentials transferred are not acceptable.

This is very similar to raping url.el , but it works for me, and this is the only way to make it work.


So your code would look something like this:

  (defvar xyz-user-name "admin") (defvar xyz-password "admin") (defvar xyz-block-authorisation nil "Flag whether to block url.el usual interactive authorisation procedure") (defadvice url-http-handle-authentication (around xyz-fix) (unless xyz-block-authorisation ad-do-it)) (ad-activate 'url-http-handle-authentication) (defun login-show-posts () (interactive) (let ((xyz-block-authorisation t) (url-request-method "GET") (url-request-extra-headers `(("Content-Type" . "application/xml") ("Authorization" . ,(concat "Basic " (base64-encode-string (concat xyz-user-name ":" xyz-password))))))) (url-retrieve "http://localhost:3000/essay/1.xml" (lambda (status) (switch-to-buffer (current-buffer)) )))) 
+7
source

With twit.el, there was a little farting to get it working. Authentication information is stored inside alist. This list is bound to a character in the url-basic-auth-storage variable.

 ELISP> (require 'url) ELISP> (pp url-basic-auth-storage) url-http-real-basic-auth-storage ELISP> (pp (symbol-value url-basic-auth-storage)) (("twitter.com:443" ("Twitter API" . "@uTh5tr!n6==")) ("twitter.com:80" ("Twitter API" . "AnotherAuthString=="))) ELISP> 

Perhaps you could do something like this:

 (let ((my-temporary-auth '(("host.com:80" ("Auth Realm" . "@uTH5r!n6=="))))) (url-basic-auth-storage 'my-temporary-auth)) (do-gunk)) 

This will lead to the fact that user authentication information will be one (if any). Looking back, perhaps this was a smarter way to go with twit.el.

+3
source

All Articles