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"`