/
pki.go
106 lines (83 loc) · 2.33 KB
/
pki.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2020 Self Group Ltd. All Rights Reserved.
package pki
import (
"encoding/json"
"fmt"
"net/http"
"github.com/joinself/self-go-sdk/pkg/transport"
"golang.org/x/crypto/ed25519"
)
// Transport the stateful connection used to send and receive messages
type Transport interface {
Get(path string) ([]byte, error)
Post(path, contentType string, payload []byte) ([]byte, error)
}
// Config messaging configuration for connecting to self messaging
type Config struct {
SelfID string
PrivateKey ed25519.PrivateKey
APIURL string
Transport Transport
}
// Client default implementation of a messaging client
type Client struct {
config Config
transport Transport
}
// New create a new messaging client
func New(config Config) (*Client, error) {
c := Client{
config: config,
}
if config.Transport == nil {
cfg := transport.RestConfig{
SelfID: config.SelfID,
PrivateKey: config.PrivateKey,
APIURL: config.APIURL,
Client: &http.Client{},
}
rs, err := transport.NewRest(cfg)
if err != nil {
return nil, err
}
config.Transport = rs
}
c.transport = config.Transport
return &c, nil
}
// GetHistory gets an identities public keys
func (c *Client) GetHistory(selfID string) ([]json.RawMessage, error) {
var path string
if len(selfID) > 11 {
path = fmt.Sprintf("/v1/apps/%s/history", selfID)
} else {
path = fmt.Sprintf("/v1/identities/%s/history", selfID)
}
var history []json.RawMessage
data, err := c.transport.Get(path)
if err != nil {
return nil, err
}
return history, json.Unmarshal(data, &history)
}
// GetDeviceKey gets an identities one time device key
func (c *Client) GetDeviceKey(selfID, deviceID string) ([]byte, error) {
var path string
if len(selfID) > 11 {
path = fmt.Sprintf("/v1/apps/%s/devices/%s/pre_keys", selfID, deviceID)
} else {
path = fmt.Sprintf("/v1/identities/%s/devices/%s/pre_keys", selfID, deviceID)
}
return c.transport.Get(path)
}
// SetDeviceKeys updates an identites device prekey bundle
func (c *Client) SetDeviceKeys(selfID, deviceID string, pkb []byte) error {
var path string
if len(selfID) > 11 {
path = fmt.Sprintf("/v1/apps/%s/devices/%s/pre_keys", selfID, deviceID)
} else {
path = fmt.Sprintf("/v1/identities/%s/devices/%s/pre_keys", selfID, deviceID)
}
_, err := c.transport.Post(path, "application/json", pkb)
return err
}