From f02aedfb94fc9a94cc59a0750f0ce3cd56aa25c8 Mon Sep 17 00:00:00 2001 From: lwnmengjing Date: Fri, 1 Dec 2023 18:39:49 +0800 Subject: [PATCH] :sparkles: feat: github oauth2 --- pkg/config/oauth2.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/pkg/config/oauth2.go b/pkg/config/oauth2.go index 0e9a0ca..c300ff7 100644 --- a/pkg/config/oauth2.go +++ b/pkg/config/oauth2.go @@ -9,7 +9,6 @@ package config import ( "context" - "fmt" "github.com/coreos/go-oidc/v3/oidc" "golang.org/x/oauth2" @@ -18,12 +17,24 @@ import ( // OAuth2 holds the configuration for the OAuth2 provider. type OAuth2 struct { Issuer string `yaml:"issuer" json:"issuer"` + Endpoint Endpoint `yaml:"endpoint" json:"endpoint"` ClientID string `yaml:"clientID" json:"clientID"` ClientSecret string `yaml:"clientSecret" json:"clientSecret"` Scopes []string `yaml:"scopes" json:"scopes"` RedirectURL string `yaml:"redirectURL" json:"redirectURL"` } +type Endpoint struct { + AuthURL string `yaml:"authURL" json:"authURL"` + DeviceAuthURL string `yaml:"deviceAuthURL" json:"deviceAuthURL"` + TokenURL string `yaml:"tokenURL" json:"tokenURL"` + + // AuthStyle optionally specifies how the endpoint wants the + // client ID & client secret sent. The zero value means to + // auto-detect. + AuthStyle int `yaml:"authStyle" json:"authStyle"` +} + // GetIssuer returns the OAuth2 issuer. func (e *OAuth2) GetIssuer() string { return e.Issuer @@ -51,16 +62,23 @@ func (e *OAuth2) GetRedirectURL() string { // GetOAuth2Config returns an oauth2.Config. func (e *OAuth2) GetOAuth2Config(c context.Context) (*oauth2.Config, error) { - fmt.Println(e.Scopes) - provider, err := oidc.NewProvider(c, e.Issuer) - if err != nil { - return nil, err - } - return &oauth2.Config{ + conf := &oauth2.Config{ ClientID: e.ClientID, ClientSecret: e.ClientSecret, Scopes: e.Scopes, - Endpoint: provider.Endpoint(), RedirectURL: e.RedirectURL, - }, nil + } + if e.Issuer != "" { + provider, err := oidc.NewProvider(c, e.Issuer) + if err != nil { + return nil, err + } + conf.Endpoint = provider.Endpoint() + return conf, nil + } + conf.Endpoint.AuthURL = e.Endpoint.AuthURL + conf.Endpoint.TokenURL = e.Endpoint.TokenURL + conf.Endpoint.AuthStyle = oauth2.AuthStyle(e.Endpoint.AuthStyle) + conf.Endpoint.DeviceAuthURL = e.Endpoint.DeviceAuthURL + return conf, nil }