From 80be4727a30483e5fe5fcc85e42cdd2785b3607c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Pila=C5=99?= Date: Thu, 15 Oct 2015 08:38:32 +0200 Subject: [PATCH] Added parsing of ECDSA keys in the example app --- cmd/jwt/app.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/jwt/app.go b/cmd/jwt/app.go index 62cb9a4..75db9d2 100644 --- a/cmd/jwt/app.go +++ b/cmd/jwt/app.go @@ -116,7 +116,14 @@ func verifyToken() error { // Parse the token. Load the key from command line option 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 @@ -161,7 +168,8 @@ func signToken() error { } // get the key - keyData, err := loadData(*flagKey) + var key interface{} + key, err = loadData(*flagKey) if err != nil { return fmt.Errorf("Couldn't read key: %v", err) } @@ -176,7 +184,18 @@ func signToken() error { token := jwt.New(alg) 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) } else { return fmt.Errorf("Error signing token: %v", err) @@ -184,3 +203,7 @@ func signToken() error { return nil } + +func isEs() bool { + return (*flagAlg)[0:2] == "ES" +}