-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (130 loc) · 4.06 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/golang-jwt/jwt"
"github.com/google/go-github/v40/github"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
)
var rootCmd = &cobra.Command{
Use: "gh-token",
Short: "Create and revoke GitHub Installation tokens",
Example: heredoc.Doc(`
# create a token with an installation ID
$ gh token create --app-id 123 app-private-key-path path/to/pem --installation-id 123
# create a token without installation ID
$ gh token create --app-id 123 app-private-key-path path/to/pem --org org-name
# revoke a token
$ gh token revoke --token ghs_123
`),
}
var createTokenCmd = &cobra.Command{
Use: "create",
RunE: func(cmd *cobra.Command, args []string) error {
appID, err := cmd.Flags().GetInt64("app-id")
if err != nil {
return err
}
appPrivKeyPath, err := cmd.Flags().GetString("app-private-key-path")
if err != nil {
return err
}
installationID, err := cmd.Flags().GetInt64("installation-id")
if err != nil {
return err
}
appToken, err := createAppToken(appID, appPrivKeyPath)
if err != nil {
return err
}
ctx := context.Background()
appClient := createClient(ctx, appToken)
if installationID == 0 {
org, err := cmd.Flags().GetString("org")
if err != nil {
return err
}
installationID, err = findOrgInstallationID(ctx, appClient, org)
}
installationToken, err := createInstallationToken(ctx, appClient, installationID)
if err != nil {
return err
}
fmt.Println(*installationToken.Token)
return nil
},
}
var revokeTokenCmd = &cobra.Command{
Use: "revoke",
RunE: func(cmd *cobra.Command, args []string) error {
token, err := cmd.Flags().GetString("token")
if err != nil {
return err
}
return revokeInstallationToken(context.Background(), token)
},
}
func main() {
rootCmd.AddCommand(createTokenCmd)
rootCmd.AddCommand(revokeTokenCmd)
// create command
createTokenCmd.Flags().Int64P("app-id", "a", 0, "App ID")
createTokenCmd.Flags().Int64P("installation-id", "i", 0, "Installation ID")
createTokenCmd.Flags().StringP("org", "o", "", "Organization name")
createTokenCmd.Flags().StringP("app-private-key-path", "p", "", "Path to the App Private Key")
createTokenCmd.MarkFlagRequired("app-id")
createTokenCmd.MarkFlagRequired("app-private-key-path")
// revoke command
revokeTokenCmd.Flags().StringP("token", "t", "", "Installation token to revoke")
revokeTokenCmd.MarkFlagRequired("token")
rootCmd.Execute()
}
// Create an authenticated GitHub client
func createClient(ctx context.Context, token string) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
}
// Create a JWT token to authenticate as a GitHub App
func createAppToken(appId int64, privKeyPath string) (string, error) {
privKeyContents, err := os.ReadFile(privKeyPath)
if err != nil {
return "", err
}
privKey, err := jwt.ParseRSAPrivateKeyFromPEM(privKeyContents)
if err != nil {
return "", err
}
now := time.Now()
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
"iat": now.Unix(),
"exp": now.Add(10 * time.Minute).Unix(),
"iss": appId,
})
return token.SignedString(privKey)
}
// Create a token for a GitHub App installation
func createInstallationToken(ctx context.Context, client *github.Client, id int64) (*github.InstallationToken, error) {
installationToken, _, err := client.Apps.CreateInstallationToken(ctx, id, &github.InstallationTokenOptions{})
return installationToken, err
}
// Revoke an existing GitHub App installation token
func revokeInstallationToken(ctx context.Context, token string) error {
client := createClient(ctx, token)
_, err := client.Apps.RevokeInstallationToken(ctx)
return err
}
// Find a GitHub App installation for an organization
func findOrgInstallationID(ctx context.Context, client *github.Client, org string) (int64, error) {
installation, _, err := client.Apps.FindOrganizationInstallation(ctx, org)
if err != nil {
return 0, err
}
return *installation.ID, nil
}