     :or {token-name "Token"}}]
   (reify
     IAuthentication
-    (parse [_ context]
+    (-parse [_ context]
       (parse-authorization-header context token-name))
-    (authenticate [_ context token]
+    (-authenticate [_ context token]
       (p/promise (fn [resolve]
                    (try+
                     (resolve (jws/unsign token secret options))

    :or {token-name "Token"}}]
  (reify
    IAuthentication
    (parse [_ context]
      (parse-authorization-header context token-name))
    (authenticate [_ context token]
      (p/promise (fn [resolve]
                   (try+
                    (resolve (jwe/decrypt token secret options))

  []
  (reify
    IAuthentication
    (-parse [_ context]
      (:identity @(:session context)))
    (-authenticate [_ context data]
      (p/resolved data))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Handlers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn- do-auth
  "Perform an asynchronous recursive loop over all
  provided backends and tries to authenticate with
  all them in order."
  [context callback [backend & backends]]
  (if backend
    (let [token (-parse backend context)]
      (if (nil? token)
        (do-auth context callback backends)
        (-> (-authenticate backend context token)
            (p/then (fn [ue]
                      (if (nil? ue)
                        (do-auth context callback backends)
                        (callback {:identity ue}))))
            (p/catch (fn [e] (callback {}))))))
    (callback {})))
