forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager_download.go
48 lines (38 loc) · 859 Bytes
/
manager_download.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
package proton
import (
"context"
"io"
"github.com/ProtonMail/gopenpgp/v2/crypto"
)
func (m *Manager) DownloadAndVerify(ctx context.Context, kr *crypto.KeyRing, url, sig string) ([]byte, error) {
fb, err := m.fetchFile(ctx, url)
if err != nil {
return nil, err
}
sb, err := m.fetchFile(ctx, sig)
if err != nil {
return nil, err
}
if err := kr.VerifyDetached(
crypto.NewPlainMessage(fb),
crypto.NewPGPSignature(sb),
crypto.GetUnixTime(),
); err != nil {
return nil, err
}
return fb, nil
}
func (m *Manager) fetchFile(ctx context.Context, url string) ([]byte, error) {
res, err := m.r(ctx).SetDoNotParseResponse(true).Get(url)
if err != nil {
return nil, err
}
b, err := io.ReadAll(res.RawBody())
if err != nil {
return nil, err
}
if err := res.RawBody().Close(); err != nil {
return nil, err
}
return b, nil
}