From b0bc1f0944525a5278714d0f1559022b5611949a Mon Sep 17 00:00:00 2001 From: Jeff Ploughman Date: Mon, 23 Jul 2018 10:55:11 -0400 Subject: [PATCH] Don't write temp file --- .gitignore | 3 ++- data/claim.json | 4 ---- path_trustee.go | 12 +++++++++++- util.go | 20 ++++++-------------- 4 files changed, 19 insertions(+), 20 deletions(-) delete mode 100644 data/claim.json diff --git a/.gitignore b/.gitignore index 4262400..3d9ff08 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ **/*.sh **/data **/data/*.json -**/test \ No newline at end of file +**/test +**/releases/* \ No newline at end of file diff --git a/data/claim.json b/data/claim.json deleted file mode 100644 index 3b83d95..0000000 --- a/data/claim.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "service": "bank-account-service", - "delegate": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJncm91cCI6WyJwYXktbWFzdGVyLWdyb3VwIl0sInN1YiI6ImN5cGhlcmhhdCJ9.kNrzfOxcALrGXQPbYkSnoBJ_LjiRfPmQXjCOv5l2uUeuM3H6GCdqnQY5kvnZtJuj4ztR3Us_uDI5cxhDJ45OQEHz4zRipYJX28rKHfO04rK1ieP95KNRxlQ1YCnufWmHHmPJgh3aK-a9zGdy2ZaXlmpVbDEOxNyUm7gNuJ1AFkYqN0S_LnNx_alU5zzoxTkKGMpTLVGzPqVKrhXRuEZwK1duKlAS4YIvq4BzYJm7lMyAafdxEkeqPb1VptQEvJzyIU2xkZMBBlhbxj6qZUEiiKloPzgAs6z1pLYDCpJL6SZ50ozyDM3tqocqY6Qqaxl3Rk0WARC17z7UFIuiOERMfUafvKC5v8aA7Wzr_3BoM91qNI3IyqFl-GEYToDZ4TD922hvNaVpdKciKIJMUUZjNXvXD9xhGWWUqwvHMPkYYJDnC5uRDdlgzgXVIGD0ABPk3a6ULLMw9PxF_RpjQzUkqVfywsUvaUOj0jPx1SVeS3CQdjFcPLwYQuub5H3HzjGUWSFLetktGrbdG_YnW6lFAz-wMzI_BYOSBtwiq9IhrxDL0x2E6PYnU1k5C0-DmYV3yDb_cMNul0KZLq4e0tC6i8YeteAlqCfoWOc3WgWPuqVulBsPGIkbmuRNYOWEpxlseWaX41On_BSskfL7NK02YHHFIZH91njGSDHo_Md0h6Y" -} \ No newline at end of file diff --git a/path_trustee.go b/path_trustee.go index aa70484..9918df0 100644 --- a/path_trustee.go +++ b/path_trustee.go @@ -15,6 +15,7 @@ package main import ( + "bytes" "context" "encoding/json" "fmt" @@ -25,6 +26,7 @@ import ( "time" jwt "github.com/dgrijalva/jwt-go" + "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" @@ -364,7 +366,7 @@ func (b *backend) pathCreateJWT(ctx context.Context, req *logical.Request, data } claims["exp"] = fmt.Sprintf("%d", timeStart.Add(timeExpiry).Unix()) - key, err := b.getTrusteePrivateKey(prunedPath, *trustee) + key, err := b.getTrusteePrivateKey(*trustee) if err != nil { return nil, err } @@ -450,3 +452,11 @@ func (b *backend) verifyClaim(ctx context.Context, rawToken string) (jwt.MapClai } return nil, fmt.Errorf("Error verifying token") } + +// PrettyPrint prints an indented JSON payload. This is used for development debugging. +func PrettyPrint(v interface{}) string { + jsonString, _ := json.Marshal(v) + var out bytes.Buffer + json.Indent(&out, jsonString, "", " ") + return out.String() +} diff --git a/util.go b/util.go index 144ade4..364f920 100644 --- a/util.go +++ b/util.go @@ -157,6 +157,7 @@ func (b *backend) readJSONKeystore(keystorePath string) ([]byte, error) { var jsonKeystore []byte file, err := os.Open(keystorePath) defer file.Close() + defer b.removeTemporaryKeystore(keystorePath) stat, err := file.Stat() if err != nil { return nil, err @@ -174,22 +175,13 @@ func (b *backend) readJSONKeystore(keystorePath string) ([]byte, error) { } -func (b *backend) getTrusteePrivateKey(path string, trustee Trustee) (*keystore.Key, error) { - tmpDir, err := b.createTemporaryKeystoreDirectory() - if err != nil { - return nil, err - } +func (b *backend) getTrusteePrivateKey(trustee Trustee) (*keystore.Key, error) { + key, _ := keystore.DecryptKey(trustee.JSONKeystore, trustee.Passphrase) - keystorePath, err := b.writeTemporaryKeystoreFile(tmpDir, trustee.KeystoreName, trustee.JSONKeystore) - if err != nil { - return nil, err - } - key, err := b.readKeyFromJSONKeystore(keystorePath, trustee.Passphrase) - if err != nil { - return nil, err + if key != nil && key.PrivateKey != nil { + return key, nil } - err = b.removeTemporaryKeystore(tmpDir) - return key, err + return nil, fmt.Errorf("failed to read key from keystore") } func (b *backend) exportKeystore(path string, trustee *Trustee) (string, error) {