Resolve issues with Get, add Key for decryption of encrypted pastes automatically

This commit is contained in:
Tyler 2019-10-02 23:05:17 -04:00
parent b157d3be7e
commit 93c0d8bf3b
3 changed files with 28 additions and 2 deletions

View File

@ -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 {

View File

@ -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) {

View File

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