This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
69 lines (56 loc) · 1.37 KB
/
auth.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
package gitamite
import (
"bytes"
"encoding/json"
"fmt"
"golang.org/x/crypto/openpgp"
"io/ioutil"
)
type AuthRequest struct {
Signature []byte
Data interface{}
}
// TODO make sure to memoize
func ReadKeyringFile(path string) (openpgp.EntityList, error) {
f, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
keyring, err := openpgp.ReadKeyRing(bytes.NewReader(f))
if err != nil {
return nil, err
}
return keyring, err
}
func (r AuthRequest) VerifyRequest() error {
// TODO: move this to models.go
p, err := GetConfigValue("pubkeyring_path")
if err != nil {
return fmt.Errorf("failed to read public keyring")
}
keyring, _ := ReadKeyringFile(p)
blob, _ := json.Marshal(r.Data) // FIXME: unmarshaling then marshaling again
if _, err := openpgp.CheckArmoredDetachedSignature(keyring,
bytes.NewReader(blob),
bytes.NewReader(r.Signature)); err != nil {
return err
}
return nil
}
func CreateAuthRequest(data interface{}) (AuthRequest, error) {
p, err := GetConfigValue("privkeyring_file")
if err != nil {
return AuthRequest{}, nil
}
keyring, _ := ReadKeyringFile(p)
r := AuthRequest{}
r.Data = data
sig := bytes.NewBufferString("")
blob, _ := json.Marshal(data)
err = openpgp.ArmoredDetachSign(sig, keyring[0], bytes.NewReader(blob), nil)
if err != nil {
return AuthRequest{}, err
}
r.Signature = sig.Bytes()
return r, nil
}