oauth-guard/config.go
Lukas Werner 7bbaf7ceb2
it works!
2025-07-02 00:27:22 -07:00

57 lines
1.4 KiB
Go

package main
import (
"fmt"
"github.com/BurntSushi/toml"
"golang.org/x/oauth2"
"golang.org/x/oauth2/endpoints"
)
type Upstream struct {
Addr string `toml:"addr"`
Program string `toml:"program"`
Args []string `toml:"args"`
}
type OAuthProvider struct {
Kind string `toml:"kind"`
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
RedirectURL string `toml:"redirect_url"`
}
type Config struct {
ListenURL string `toml:"listen_url"`
GuardedPaths []string `toml:"guarded_paths"`
AllowedUsers []string `toml:"allowed_users"`
Upstream Upstream `toml:"upstream"`
OAuthProvider OAuthProvider `toml:"provider"`
}
func LoadConfig() (Config, oauth2.Config, error) {
config := Config{}
oa2 := oauth2.Config{}
_, err := toml.DecodeFile("config.toml", &config)
if err != nil {
return config, oa2, fmt.Errorf("unable to parse 'config.toml' tompl decoding error: %w", err)
}
oa2.ClientID = config.OAuthProvider.ClientID
oa2.ClientSecret = config.OAuthProvider.ClientSecret
oa2.Endpoint = oauth2.Endpoint{}
oa2.RedirectURL = config.OAuthProvider.RedirectURL
oa2.Scopes = []string{}
switch config.OAuthProvider.Kind {
case "github":
oa2.Endpoint = endpoints.GitHub
oa2.Scopes = []string{"read:user"}
case "google":
oa2.Endpoint = endpoints.Google
oa2.Scopes = []string{"https://www.googleapis.com/auth/userinfo.email"}
}
return config, oa2, err
}