-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
152 lines (123 loc) · 3.25 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/google/go-github/v49/github"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/leveldbcache"
"golang.org/x/oauth2"
)
func main() {
var (
explicitAccessToken string
accessTokenPath string
username string
requiredOrgName string
requiredTeamNames string
credentialCacheDir string
)
flag.StringVar(&explicitAccessToken, "a", "", "GitHub access token")
flag.StringVar(&accessTokenPath, "apath", "", "GitHub access token path")
flag.StringVar(&username, "u", "", "GitHub username")
flag.StringVar(&requiredOrgName, "o", "", "GitHub organization to require membership in")
flag.StringVar(&requiredTeamNames, "t", "", "GitHub teams to require membership in (comma separated)")
flag.StringVar(&credentialCacheDir, "cpath", "", "Credential cache directory")
flag.Parse()
switch {
case len(username) == 0:
log.Fatal("username is required")
case len(requiredOrgName) == 0:
log.Fatal("org name is required")
}
ctx := context.Background()
authedTransport := &oauth2.Transport{
Source: oauth2.StaticTokenSource(getAccessToken(accessTokenPath, explicitAccessToken)),
}
client := github.NewClient(&http.Client{
Transport: maybeCached(authedTransport, credentialCacheDir),
})
if len(requiredTeamNames) > 0 {
isInRequiredTeams := false
teamNames := strings.Split(requiredTeamNames, ",")
for _, teamName := range teamNames {
user_team_membership, resp, err := client.Teams.GetTeamMembershipBySlug(ctx, requiredOrgName, teamName, username)
if err != nil {
if resp.StatusCode == 404 {
continue
}
log.Fatal(err)
}
if user_team_membership != nil {
isInRequiredTeams = true
break
}
}
if !isInRequiredTeams {
os.Exit(0)
}
} else {
user_orgs, _, err := client.Organizations.List(ctx, username, nil)
if err != nil {
log.Fatal(err)
}
isInRequiredOrg := false
for _, org := range user_orgs {
orgName := *(org.Login)
if orgName == requiredOrgName {
isInRequiredOrg = true
break
}
}
if !isInRequiredOrg {
os.Exit(0)
}
}
keys, _, err := client.Users.ListKeys(ctx, username, nil)
if err != nil {
log.Fatal(err)
}
for _, key := range keys {
material := *(key.Key)
fmt.Printf("%v\n", material)
}
}
func getAccessToken(tokenPath string, explicitToken string) *oauth2.Token {
if len(tokenPath) == 0 {
if len(explicitToken) == 0 {
log.Fatal("access token is required")
}
return &oauth2.Token{AccessToken: explicitToken}
}
if len(explicitToken) > 0 {
log.Fatal("cannot set both -a and -apath")
}
tokenBuf, err := ioutil.ReadFile(tokenPath)
if err != nil {
log.Fatal(err)
}
tokenStr := strings.TrimSuffix(string(tokenBuf), "\n")
return &oauth2.Token{AccessToken: tokenStr}
}
func maybeCached(backingTransport http.RoundTripper, cacheDir string) http.RoundTripper {
if len(cacheDir) == 0 {
return backingTransport
}
if err := os.MkdirAll(cacheDir, 0700); err != nil {
return backingTransport
}
ldbCache, err := leveldbcache.New(path.Join(cacheDir, "credentials.db"))
if err != nil {
return backingTransport
}
return &httpcache.Transport{
Transport: backingTransport,
Cache: ldbCache,
}
}