Added parsing of ECDSA keys in the example app

This commit is contained in:
Štěpán Pilař 2015-10-15 08:38:32 +02:00
parent f62f64ea22
commit 80be4727a3
1 changed files with 26 additions and 3 deletions

View File

@ -116,7 +116,14 @@ func verifyToken() error {
// Parse the token. Load the key from command line option // Parse the token. Load the key from command line option
token, err := jwt.Parse(string(tokData), func(t *jwt.Token) (interface{}, error) { token, err := jwt.Parse(string(tokData), func(t *jwt.Token) (interface{}, error) {
return loadData(*flagKey) data, err := loadData(*flagKey)
if err != nil {
return nil, err
}
if isEs() {
return jwt.ParseECPublicKeyFromPEM(data)
}
return data, nil
}) })
// Print some debug data // Print some debug data
@ -161,7 +168,8 @@ func signToken() error {
} }
// get the key // get the key
keyData, err := loadData(*flagKey) var key interface{}
key, err = loadData(*flagKey)
if err != nil { if err != nil {
return fmt.Errorf("Couldn't read key: %v", err) return fmt.Errorf("Couldn't read key: %v", err)
} }
@ -176,7 +184,18 @@ func signToken() error {
token := jwt.New(alg) token := jwt.New(alg)
token.Claims = claims token.Claims = claims
if out, err := token.SignedString(keyData); err == nil { if isEs() {
if k, ok := key.([]byte); !ok {
return fmt.Errorf("Couldn't convert key data to key")
} else {
key, err = jwt.ParseECPrivateKeyFromPEM(k)
if err != nil {
return err
}
}
}
if out, err := token.SignedString(key); err == nil {
fmt.Println(out) fmt.Println(out)
} else { } else {
return fmt.Errorf("Error signing token: %v", err) return fmt.Errorf("Error signing token: %v", err)
@ -184,3 +203,7 @@ func signToken() error {
return nil return nil
} }
func isEs() bool {
return (*flagAlg)[0:2] == "ES"
}