-
Notifications
You must be signed in to change notification settings - Fork 32
/
keypairs.go
80 lines (68 loc) · 2.07 KB
/
keypairs.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
package registry
import (
"context"
"log"
"net/url"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/envelope"
"github.com/manifoldco/torus-cli/identity"
)
// ClaimedKeyPair contains a public/private keypair, and all the Claims made
// against it (system and user signatures).
type ClaimedKeyPair struct {
apitypes.PublicKeySegment
PrivateKey *envelope.Signed `json:"private_key"`
}
// KeyPairs represents the `/keypairs` registry endpoint, used for accessing
// users' signing and encryption keypairs.
type KeyPairs struct {
client *Client
}
// Post creates a new keypair on the registry.
//
// The keypair includes the user's public key, private key, and a self-signed
// claim on the public key.
//
// keys may be either signing or encryption keys.
func (k *KeyPairs) Post(ctx context.Context, pubKey, privKey,
claim *envelope.Signed) (*envelope.Signed, *envelope.Signed, []envelope.Signed, error) {
req, err := k.client.NewRequest("POST", "/keypairs", nil,
ClaimedKeyPair{
PublicKeySegment: apitypes.PublicKeySegment{
PublicKey: pubKey,
Claims: []envelope.Signed{*claim},
},
PrivateKey: privKey,
})
if err != nil {
log.Printf("Error building http request: %s", err)
return nil, nil, nil, err
}
resp := ClaimedKeyPair{}
_, err = k.client.Do(ctx, req, &resp)
if err != nil {
log.Printf("Failed to create signing keypair: %s", err)
return nil, nil, nil, err
}
return resp.PublicKey, resp.PrivateKey, resp.Claims, nil
}
// List returns all KeyPairs for the logged in user in the given, or all orgs
// if orgID is nil.
func (k *KeyPairs) List(ctx context.Context, orgID *identity.ID) ([]ClaimedKeyPair, error) {
query := &url.Values{}
if orgID != nil {
query.Set("org_id", orgID.String())
}
req, err := k.client.NewRequest("GET", "/keypairs", query, nil)
if err != nil {
log.Printf("Error building http request: %s", err)
return nil, err
}
resp := []ClaimedKeyPair{}
_, err = k.client.Do(ctx, req, &resp)
if err != nil {
log.Printf("Failed to retrieve keypairs: %s", err)
return nil, err
}
return resp, nil
}