2014-09-15 25 views
5

Qualcuno potrebbe dirmi perché il seguente esempio (da https://github.com/dgrijalva/jwt-go) non funziona?go e parsing token con jwt-go

token, err := jwt.Parse(myToken, func(token *jwt.Token) ([]byte, error) { 
    return myLookupKey(token.Header["kid"]) 
}) 

if err == nil && token.Valid { 
    deliverGoodness("!") 
} else { 
    deliverUtterRejection(":(") 
} 

ottengo un errore che dice "non può usare func letterale (tipo func (* jwt.Token) ([] Byte, errore)) come tipo jwt.Keyfunc in argomento per jwt.Parse"

Ho provato ad usare il codice da un paio di diversi esempi di jwt-go ma ho sempre finito con lo stesso errore.

risposta

6

La funzione Parse aspetta

type Keyfunc func(*Token) (interface{}, error) 

È necessario tornare interface{}, non byte[] nella funzione letterale.
(magari usando un byte.Buffer per avvolgere il byte[], che si può quindi leggere come in "Convert arbitrary Golang interface to byte array")

Gert Cuykens punti nei commenti a issue 36: commit e1571c8 dovrebbe hanno aggiornato l'esempio.
Other examples like this gist anche essere aggiornato.

+0

Bene, io sarò ... Ecco, ora tutto sembra funzionare come previsto. La cosa più divertente è che nel Github di jwt-go usano [] byte nell'esempio. Grazie! – QlliOlli

+0

Si noti che la modifica all'interfaccia {} è avvenuta abbastanza di recente: https://github.com/dgrijalva/jwt-go/commit/23cb3af02c1259a5058fcb1402424442ec9b03ab Potrebbe voler inviare un ping all'autore per correggere la loro documentazione. – dyoo

+0

https://github.com/dgrijalva/jwt-go/issues/36 –