Resolve issues with Get, add Key for decryption of encrypted pastes automatically
This commit is contained in:
parent
b157d3be7e
commit
93c0d8bf3b
|
@ -5,6 +5,7 @@ import "github.com/google/go-querystring/query"
|
||||||
type PasteGetOptions struct {
|
type PasteGetOptions struct {
|
||||||
Urls bool `url:"urls,omitempty"`
|
Urls bool `url:"urls,omitempty"`
|
||||||
TimeFormat string `url:"time_format,omitempty"`
|
TimeFormat string `url:"time_format,omitempty"`
|
||||||
|
Key string `url:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o PasteGetOptions) encode() string {
|
func (o PasteGetOptions) encode() string {
|
||||||
|
|
23
pastee.go
23
pastee.go
|
@ -3,6 +3,7 @@ package pastee
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -100,13 +101,31 @@ func (p *Pastee) Get(id string, opts ...PasteGetOptions) (*Paste, error) {
|
||||||
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
var response Paste
|
var response PasteGetResponse
|
||||||
|
|
||||||
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
|
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
|
||||||
return nil, err
|
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) {
|
func (p *Pastee) Submit(paste *Paste) (*PasteResponse, error) {
|
||||||
|
|
|
@ -43,6 +43,12 @@ type PasteResponse struct {
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PasteGetResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Errors []*Error `json:"errors"`
|
||||||
|
Paste *Paste `json:"paste"`
|
||||||
|
}
|
||||||
|
|
||||||
type authRequest struct {
|
type authRequest struct {
|
||||||
username string `json:"username"`
|
username string `json:"username"`
|
||||||
password string `json:"password"`
|
password string `json:"password"`
|
||||||
|
|
Loading…
Reference in New Issue