/
github_keys.go
93 lines (76 loc) · 1.7 KB
/
github_keys.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
package sshkey
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/oursky/pageship/internal/cache"
"golang.org/x/crypto/ssh"
"golang.org/x/time/rate"
)
type GitHubKeys struct {
ctx context.Context
l *rate.Limiter
client *http.Client
cache *cache.Cache[map[string]struct{}]
}
func NewGitHubKeys(ctx context.Context) (*GitHubKeys, error) {
keys := &GitHubKeys{
ctx: ctx,
l: rate.NewLimiter(rate.Every(time.Minute), 10),
client: &http.Client{},
}
cache, err := cache.NewCache(100, time.Minute, keys.doLoad)
if err != nil {
return nil, err
}
keys.cache = cache
return keys, nil
}
func (g *GitHubKeys) PublicKey(username string) (map[string]struct{}, error) {
pkeys, err := g.cache.Load(strings.ToLower(username))
if err != nil {
return nil, err
}
return pkeys, nil
}
func (g *GitHubKeys) doLoad(username string) (map[string]struct{}, error) {
ctx, cancel := context.WithTimeout(g.ctx, time.Second*10)
defer cancel()
g.l.Wait(ctx)
u := url.URL{
Scheme: "https",
Host: "github.com",
Path: "/" + url.PathEscape(username) + ".keys",
}
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return nil, err
}
resp, err := g.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP status: %s", resp.Status)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
keys := make(map[string]struct{})
for {
pkey, _, _, rest, err := ssh.ParseAuthorizedKey(data)
if err != nil {
// Assume EOF
break
}
keys[string(pkey.Marshal())] = struct{}{}
data = rest
}
return keys, nil
}