From 93c0d8bf3b92a0757d4330fac5ad1786082f4b2e Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 2 Oct 2019 23:05:17 -0400 Subject: [PATCH] Resolve issues with Get, add Key for decryption of encrypted pastes automatically --- options.go | 1 + pastee.go | 23 +++++++++++++++++++++-- structs.go | 6 ++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/options.go b/options.go index 12f7f71..8ade96c 100644 --- a/options.go +++ b/options.go @@ -5,6 +5,7 @@ import "github.com/google/go-querystring/query" type PasteGetOptions struct { Urls bool `url:"urls,omitempty"` TimeFormat string `url:"time_format,omitempty"` + Key string `url:"-"` } func (o PasteGetOptions) encode() string { diff --git a/pastee.go b/pastee.go index 4d135f8..9684f5e 100644 --- a/pastee.go +++ b/pastee.go @@ -3,6 +3,7 @@ package pastee import ( "bytes" "encoding/json" + "errors" "io" "math/rand" "net/http" @@ -100,13 +101,31 @@ func (p *Pastee) Get(id string, opts ...PasteGetOptions) (*Paste, error) { defer res.Body.Close() - var response Paste + var response PasteGetResponse if err := json.NewDecoder(res.Body).Decode(&response); err != nil { return nil, err } - return &response, nil + if response.Success && response.Paste != nil { + if response.Paste.Encrypted && len(opts) > 0 && opts[0].Key != "" { + for _, section := range response.Paste.Sections { + b, err := cryptojs.Decrypt(section.Contents, opts[0].Key) + + if err != nil { + return nil, err + } + + section.Contents = string(b) + } + } + } else if len(response.Errors) > 0 { + return nil, errors.New("error: " + response.Errors[0].Message) + } else { + return nil, errors.New("unknown error") + } + + return response.Paste, nil } func (p *Pastee) Submit(paste *Paste) (*PasteResponse, error) { diff --git a/structs.go b/structs.go index b2f4a84..38e1d0d 100644 --- a/structs.go +++ b/structs.go @@ -43,6 +43,12 @@ type PasteResponse struct { Link string `json:"link"` } +type PasteGetResponse struct { + Success bool `json:"success"` + Errors []*Error `json:"errors"` + Paste *Paste `json:"paste"` +} + type authRequest struct { username string `json:"username"` password string `json:"password"`