Merge pull request #93 from EnerfisTeam/master

Added parsing of ECDSA keys in the example app
This commit is contained in:
Dave Grijalva 2015-11-02 11:20:56 -08:00
commit 52c68d1293
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"
}