-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
github-oidc-auth-app.go
82 lines (65 loc) · 2.02 KB
/
github-oidc-auth-app.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
package main
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
port := os.Getenv("PORT")
private_key_base64 := os.Getenv("PRIVATE_KEY")
private_key, err := base64.StdEncoding.DecodeString(private_key_base64)
if err != nil {
log.Fatal("error decoding private key", err)
}
app_id, err := strconv.ParseInt(os.Getenv("APP_ID"), 10, 36)
if err != nil {
log.Fatal("Wrong format for APP_ID")
}
var webhook_secret, configFile string
if webhook_secret = os.Getenv("WEBHOOK_SECRET"); webhook_secret == "" {
log.Fatal("WEBHOOK_SECRET is not set")
}
configRepo := os.Getenv("CONFIG_REPO")
if configRepo == "" {
log.Println("CONFIG_REPO is not set, using default value 'oidc_entitlements'")
configRepo = "oidc_entitlements"
} else {
log.Printf("CONFIG_REPO set to '%s'", configRepo)
}
configFile = os.Getenv("CONFIG_FILE")
if configFile != "" {
log.Printf("CONFIG_FILE set to '%s'", configFile)
}
appTransport, err := ghinstallation.NewAppsTransport(http.DefaultTransport, app_id, private_key)
if err != nil {
log.Fatal("Failed to initialize GitHub App transport:", err)
}
wellKnownURL := "https://token.actions.githubusercontent.com/.well-known/jwks"
gitUrl := "https://github.com"
if ghesUrl := os.Getenv("GHES_URL"); ghesUrl != "" {
appTransport.BaseURL = fmt.Sprintf("%s/api/v3", ghesUrl)
wellKnownURL = fmt.Sprintf("%s/_services/token/.well-known/jwks", ghesUrl)
gitUrl = ghesUrl
}
appContext := NewAppContext(time.Now(), appTransport, webhook_secret, configRepo, configFile, wellKnownURL, gitUrl)
fmt.Println("loading config cache")
err = appContext.loadConfigs()
if err != nil {
log.Println("error while loading config cache", err)
}
fmt.Printf("starting up on port %s\n", port)
server := http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: appContext,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
}
server.ListenAndServe()
}