hydroxide-push/protonmail/keys.go

82 lines
1.5 KiB
Go
Raw Normal View History

2017-08-24 11:51:15 +03:00
package protonmail
import (
"errors"
"net/http"
"net/url"
"strings"
"golang.org/x/crypto/openpgp"
)
2017-09-21 21:02:54 +03:00
type PrivateKey struct {
2017-09-13 12:43:12 +03:00
ID string
Version int
PublicKey string
PrivateKey string
2017-08-24 11:51:15 +03:00
Fingerprint string
2017-09-13 12:43:12 +03:00
Activation interface{} // TODO
2017-08-24 11:51:15 +03:00
}
2017-09-21 21:02:54 +03:00
func (priv *PrivateKey) Entity() (*openpgp.Entity, error) {
keyRing, err := openpgp.ReadArmoredKeyRing(strings.NewReader(priv.PrivateKey))
if err != nil {
return nil, err
}
if len(keyRing) == 0 {
return nil, errors.New("private key is empty")
}
return keyRing[0], nil
}
type RecipientType int
const (
RecipientInternal RecipientType = 1
RecipientExternal = 2
)
type PublicKeyResp struct {
RecipientType RecipientType
MIMEType string
Keys []*PublicKey
}
type PublicKey struct {
Send int
PublicKey string
}
func (pub *PublicKey) Entity() (*openpgp.Entity, error) {
keyRing, err := openpgp.ReadArmoredKeyRing(strings.NewReader(pub.PublicKey))
if err != nil {
return nil, err
}
if len(keyRing) == 0 {
return nil, errors.New("public key is empty")
}
return keyRing[0], nil
}
2017-09-21 21:03:55 +03:00
// GetPublicKeys retrieves public keys for a user.
func (c *Client) GetPublicKeys(email string) (*PublicKeyResp, error) {
v := url.Values{}
v.Set("Email", email)
// TODO: Fingerprint
req, err := c.newRequest(http.MethodGet, "/keys?"+v.Encode(), nil)
if err != nil {
return nil, err
}
var respData struct {
resp
*PublicKeyResp
}
if err := c.doJSON(req, &respData); err != nil {
return nil, err
}
return respData.PublicKeyResp, nil
}