cbll
cbll

Reputation: 7219

Go parsing JWT: error verifying ID token: illegal base64 data at input byte 0

I'm adding a Firebase JWT to my Google App Engine Go-service in the request header of a GET request. Here's the JavaScript:

const response = await fetch(
                'https://some-app.appspot.com/_ah/data', {
                    method: 'get',
                    headers: {
                        'Authorization': 'Bearer ' + await Component.fetchJWT()
                    }
                });

On my Go service, which is hosted at the endpoint, the request is received. However, the error is thrown:

error verifying ID token: illegal base64 data at input byte 0

Here is how I process the JWT:

func (ma *myapp) SomeHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    reqToken := r.Header.Get("Authorization")
    splitToken := strings.Split(reqToken, "Bearer")
    reqToken = splitToken[1]
    fmt.Println(reqToken) // Prints the token correctly

    lib.VerifyIDToken(ma.fbapp, reqToken) // Error is thrown in this function

    enc := json.NewEncoder(w)
    err := enc.Encode(SomeData)

    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}

The token function is as such:

func VerifyIDToken(app *firebase.App, idToken string) *auth.Token {
    // [START verify_id_token]
    client, err := app.Auth(context.Background())
    if err != nil {
        log.Fatalf("error getting Auth client: %v\n", err)
    }

    token, err := client.VerifyIDToken(idToken)
    if err != nil {
        log.Fatalf("error verifying ID token: %v\n", err) // Error thrown here
    }

    log.Printf("Verified ID token: %v\n", token)
    // [END verify_id_token]

    return token
}

When printing out the token, it appears fine to me. However, is my method of retrieving it from the request header wrong? It could appear so?

Upvotes: 0

Views: 2515

Answers (1)

Sebastian Krysiak
Sebastian Krysiak

Reputation: 911

Remove the leading space in Authorization header split

strings.Split(reqToken, "Bearer ")

Upvotes: 6

Related Questions