Commits

Hugh Giddens  committed 96ec1f9 Merge

Merge basic authentication branch.

It's slow but session auth code as excessively stupid.

  • Participants
  • Parent commits 8024261, e3b824c

Comments (0)

Files changed (1)

File jira-rest.el

 (defun jira-rest-instance-password (&optional instance)
   (fourth jira-rest-instance))
 
-(defun jira-rest-session-endpoint ()
-  (concat (jira-rest-instance-url) "/rest/auth/latest/session"))
-
 (defun jira-rest-project-endpoint ()
   (concat (jira-rest-instance-url) "/rest/api/latest/project"))
 
           id-or-key
           (if user-to-delete (concat "?username=" user-to-delete) "")))
 
-(defun jira-rest-is-logged-in-p ()
-  "Determines whether a response was served to a authenticated user. "
-  (save-excursion
-    (save-restriction
-      (mail-narrow-to-head)
-      (string= (mail-fetch-field "X-Seraph-LoginReason" t) "OK"))))
-
-(defun jira-rest-should-retry-after-login-p ()
-  (and (jira-rest-instance-username)
-       (jira-rest-instance-password)
-       (not (jira-rest-is-logged-in-p))))
+(defun jira-rest-auth-token ()
+  (concat "Basic "
+          (base64-encode-string (concat (jira-rest-instance-username)
+                                        ":"
+                                        (jira-rest-instance-password)))))
 
 (defmacro with-jira-rest-response (&rest body)
   (let ((buffer (gensym)))
 response body.
 
 Wraps `url-retrieve', which should be seen for more details."
-  ;; We do this asinine request, re-auth if failed, retry stuff because the
-  ;; authenticated sessions end with no warning from our end. We're using
-  ;; session-based authentication instead of HTTP basic authentication (as
-  ;; the documentation says we should be) because doing it this way is
-  ;; dramatically faster (~ 6x against OnDemand).
-  (labels ((got-first-attempt (status instance url-parameters callback args)
-             (with-jira-rest-response
-              (let ((jira-rest-instance instance))
-                (destructuring-bind (&optional error-symbol . data) (plist-get status :error)
-                  (when (and error-symbol (not (= (second data) 401)))
-                    (signal error-symbol data))
-                  (if (or error-symbol (jira-rest-should-retry-after-login-p))
-                      (let ((url-request-method "POST")
-                            (url-request-extra-headers '(("Content-Type" . "application/json")))
-                            (url-request-data (json-encode `((username . ,(jira-rest-instance-username))
-                                                             (password . ,(jira-rest-instance-password))))))
-                        (jira-rest-retrieve (jira-rest-session-endpoint)
-                                            'got-login-response
-                                            (list url-parameters callback args)))
-                    (apply callback (jira-rest-parse-response status) args))))))
-           (got-login-response (status url-parameters callback args)
-             (with-jira-rest-response
-              (if (and (null (plist-get status :error))
-                       (jira-rest-is-logged-in-p))
-                  (destructuring-bind (url url-request-method url-request-extra-headers url-request-data)
-                      url-parameters
-                    (jira-rest-retrieve url 'got-second-attempt (list callback args)))
-                (error "Unable to log in: %S" status))))
-           (got-second-attempt (status callback args)
-             (with-jira-rest-response
-              (destructuring-bind (&optional error-symbol . data) (plist-get status :error)
-                (when error-symbol
-                  (signal error-symbol data)))
-              (apply callback (jira-rest-parse-response status) args))))
-    (destructuring-bind (url url-request-method url-request-extra-headers url-request-data)
-        url-parameters
-      (jira-rest-retrieve url 'got-first-attempt (list jira-rest-instance url-parameters callback args)))))
+  (destructuring-bind (url url-request-method url-request-extra-headers url-request-data)
+      url-parameters
+    (push (cons "Authorization" (jira-rest-auth-token)) url-request-extra-headers)
+    (jira-rest-retrieve url
+                        (lambda (status callback args)
+                          (with-jira-rest-response
+                           (destructuring-bind (&optional error-symbol . data) (plist-get status :error)
+                             (when error-symbol
+                               (signal error-symbol data)))
+                           (apply callback (jira-rest-parse-response status) args)))
+                        (list callback args))))
 
 (defun jira-rest-get (url callback &optional args)
   (jira-rest-request (list url nil nil nil) callback args))