go/pastee.go

195 lines
3.7 KiB
Go
Raw Normal View History

2018-01-27 23:16:24 +00:00
package pastee
2017-06-08 05:40:35 +00:00
import (
"net/http"
"io"
"encoding/json"
"bytes"
2018-01-27 23:01:14 +00:00
"strconv"
"time"
"net/url"
"math/rand"
"git.meow.tf/tyler/go-pastee/cryptojs"
2017-06-08 05:40:35 +00:00
)
func New(key string) *Pastee {
2018-01-27 23:01:14 +00:00
return &Pastee{ApiKey: key, Base: "https://api.paste.ee/v1", Client: &http.Client{Timeout: time.Minute}}
}
func (p *Pastee) Authenticate(username, password string) (*AuthResponse, error) {
body, err := json.Marshal(&authRequest{username, password})
if err != nil {
return nil, err
}
req, err := p.newRequest("POST", "users/authenticate", bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
res, err := p.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var response AuthResponse
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
}
func (p *Pastee) List() (*PasteListResponse, error) {
q := &url.Values{}
q.Set("perpage", "25")
q.Set("page", "1")
req, err := p.newRequest("GET", "pastes?" + q.Encode(), nil)
if err != nil {
return nil, err
}
res, err := p.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var response PasteListResponse
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
}
func (p *Pastee) Get(id string) (*Paste, error) {
req, err := p.newRequest("GET", "pastes/" + id, nil)
if err != nil {
return nil, err
}
res, err := p.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var response Paste
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
2017-06-08 05:40:35 +00:00
}
func (p *Pastee) Submit(paste *Paste) (*PasteResponse, error) {
2018-01-27 23:01:14 +00:00
var key string
if paste.Encrypted {
key = RandStringBytesMaskImprSrc(32)
2018-01-27 23:21:57 +00:00
var err error
2018-01-27 23:01:14 +00:00
for _, section := range paste.Sections {
2018-01-27 23:21:57 +00:00
section.Contents, err = cryptojs.Encrypt(section.Contents, key)
if err != nil {
return nil, err
}
2018-01-27 23:01:14 +00:00
}
}
2017-06-08 05:40:35 +00:00
body, err := json.Marshal(paste)
if err != nil {
return nil, err
}
req, err := p.newRequest("POST", "pastes", bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
2018-01-27 23:01:14 +00:00
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
2017-06-08 05:40:35 +00:00
res, err := p.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var response PasteResponse
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
2018-01-27 23:01:14 +00:00
if key != "" {
response.Key = key
}
2017-06-08 05:40:35 +00:00
return &response, nil
}
func (p *Pastee) newRequest(method, path string, body io.Reader) (*http.Request, error) {
2018-01-27 23:01:14 +00:00
req, err := http.NewRequest(method, p.Base + "/" + path, body)
2017-06-08 05:40:35 +00:00
if err != nil {
return nil, err
}
req.Header.Set("X-Auth-Token", p.ApiKey)
return req, nil
2018-01-27 23:01:14 +00:00
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var src = rand.NewSource(time.Now().UnixNano())
func RandStringBytesMaskImprSrc(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
2017-06-08 05:40:35 +00:00
}