-
Notifications
You must be signed in to change notification settings - Fork 126
/
github_app_oauth.go
91 lines (73 loc) · 1.87 KB
/
github_app_oauth.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
package github
import (
"fmt"
"net/http"
"os"
"strconv"
"github.com/bradleyfalzon/ghinstallation/v2"
"golang.org/x/oauth2"
"github.com/hatchet-dev/hatchet/internal/auth/oauth"
githubsdk "github.com/google/go-github/v57/github"
)
const (
GithubAuthURL string = "https://github.com/login/oauth/authorize"
GithubTokenURL string = "https://github.com/login/oauth/access_token" // #nosec G101
)
type GithubAppConf struct {
oauth2.Config
appName string
webhookSecret string
webhookURL string
secret []byte
appID int64
}
func NewGithubAppConf(
cfg *oauth.Config,
appName, appSecretPath, appWebhookSecret, appWebhookURL, appID string) (*GithubAppConf, error) {
intAppID, err := strconv.ParseInt(appID, 10, 64)
if err != nil {
return nil, err
}
appSecret, err := os.ReadFile(appSecretPath)
if err != nil {
return nil, fmt.Errorf("could not read github app secret: %s", err)
}
return &GithubAppConf{
appName: appName,
webhookSecret: appWebhookSecret,
webhookURL: appWebhookURL,
secret: appSecret,
appID: intAppID,
Config: oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: GithubAuthURL,
TokenURL: GithubTokenURL,
},
RedirectURL: cfg.BaseURL + "/api/v1/users/github-app/callback",
Scopes: cfg.Scopes,
},
}, nil
}
func (g *GithubAppConf) GetGithubClient(installationID int64) (*githubsdk.Client, error) {
itr, err := ghinstallation.New(
http.DefaultTransport,
g.appID,
installationID,
g.secret,
)
if err != nil {
return nil, err
}
return githubsdk.NewClient(&http.Client{Transport: itr}), nil
}
func (g *GithubAppConf) GetWebhookSecret() string {
return g.webhookSecret
}
func (g *GithubAppConf) GetWebhookURL() string {
return g.webhookURL
}
func (g *GithubAppConf) GetAppName() string {
return g.appName
}