"error" : "invalid_token: Could not parse auth token." When generating JWT for Firebase in Clojure

StackOverflow https://stackoverflow.com/questions/15164159

  •  16-03-2022
  •  | 
  •  

質問

Trying to write write a wrapper around the Firebase REST API (see https://github.com/cloudfuji/taika for the full source), and the auth token seems to be failing. The functions are simple wrappers around the Firebase-provided Java library options (https://github.com/firebase/firebase-token-generator-java)

The code is simple:

(ns taika.auth
  (:require [clojure.string :as string]
            [clj-http.client :as client]
            [cheshire.core :as json])
  (:import [com.firebase.firebase-token-generator.security.token]
           [org.json.JSONOBject]))

(defn token-generator [secret-key]
  (com.firebase.security.token.TokenGenerator. secret-key))

(defn create-token [token-generator auth-data & [admin?]]
  (let [token-options (doto (com.firebase.security.token.TokenOptions.)
                        (.setAdmin (or admin? false)))]
    (.createToken token-generator (org.json.JSONObject. auth-data) token-options)))

Generating the token, the looks reasonable (example secret-key, of course):

(let [tg (token-generator "abc123")
                  auth-data {:email "example@example.com" :api_key "my-api-key"}
                  admin? false]
              (create-token tg auth-data admin?))

 => "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ2IjowLCJpYXQiOjEzNjIxNjEzMDJ9.8701406fad76b2dff83bf38a18d91a95ed7787f255e7dd534a77e7daa0c58c59"

But when using the token in REST API requests, it fails with:

{ "error" : "invalid_token: Could not parse auth token." }

The ruby library doesn't seem to have the same problem.

Again, the full source source is at https://github.com/cloudfuji/taika/blob/master/src/taika/auth.clj

役に立ちましたか?

解決

This error was caused by a bug in the Java Token Generator library. It has been fixed now. Pull down the changes and give it another shot.

https://github.com/firebase/firebase-token-generator-java

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top