...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package github
17
18 import (
19 "net/url"
20 "os"
21
22 "github.com/google/go-containerregistry/pkg/authn"
23 )
24
25 const ghcrHostname = "ghcr.io"
26
27
28
29
30
31 var Keychain authn.Keychain = githubKeychain{}
32
33 type githubKeychain struct{}
34
35 func (githubKeychain) Resolve(r authn.Resource) (authn.Authenticator, error) {
36 serverURL, err := url.Parse("https://" + r.String())
37 if err != nil {
38 return authn.Anonymous, nil
39 }
40 if serverURL.Hostname() == ghcrHostname {
41 username := os.Getenv("GITHUB_ACTOR")
42 if username == "" {
43 username = "unset"
44 }
45 if tok := os.Getenv("GITHUB_TOKEN"); tok != "" {
46 return githubAuthenticator{username, tok}, nil
47 }
48 }
49 return authn.Anonymous, nil
50 }
51
52 type githubAuthenticator struct{ username, password string }
53
54 func (g githubAuthenticator) Authorization() (*authn.AuthConfig, error) {
55 return &authn.AuthConfig{
56 Username: g.username,
57 Password: g.password,
58 }, nil
59 }
60
View as plain text