From 31e3cfab2415187c6851e8571a5d3c87c04e3b1a Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Tue, 23 Jul 2024 19:23:51 +0200 Subject: [PATCH] code refactoring --- server/openid/config.go | 32 ++++++++++++++++++++++++++++++++ server/openid/handler.go | 25 ------------------------- 2 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 server/openid/config.go diff --git a/server/openid/config.go b/server/openid/config.go new file mode 100644 index 0000000..fd90fb2 --- /dev/null +++ b/server/openid/config.go @@ -0,0 +1,32 @@ +package openid + +import ( + "context" + + "github.com/coreos/go-oidc" + "github.com/marcopeocchi/yt-dlp-web-ui/server/config" + "golang.org/x/oauth2" +) + +func Configure() { + if !config.Instance().UseOpenId { + return + } + + provider, err := oidc.NewProvider(context.Background(), config.Instance().OpenIdProviderURL) + if err != nil { + panic(err) + } + + oauth2Config = oauth2.Config{ + ClientID: config.Instance().OpenIdClientId, + ClientSecret: config.Instance().OpenIdClientSecret, + RedirectURL: config.Instance().OpenIdRedirectURL, + Endpoint: provider.Endpoint(), + Scopes: []string{oidc.ScopeOpenID, "profile", "email"}, + } + + verifier = provider.Verifier(&oidc.Config{ + ClientID: config.Instance().OpenIdClientId, + }) +} diff --git a/server/openid/handler.go b/server/openid/handler.go index 6e32d9f..f51c791 100644 --- a/server/openid/handler.go +++ b/server/openid/handler.go @@ -1,7 +1,6 @@ package openid import ( - "context" "encoding/json" "errors" "fmt" @@ -10,7 +9,6 @@ import ( "github.com/coreos/go-oidc" "github.com/google/uuid" - "github.com/marcopeocchi/yt-dlp-web-ui/server/config" "golang.org/x/oauth2" ) @@ -25,29 +23,6 @@ var ( verifier *oidc.IDTokenVerifier ) -func Configure() { - if !config.Instance().UseOpenId { - return - } - - provider, err := oidc.NewProvider(context.Background(), config.Instance().OpenIdProviderURL) - if err != nil { - panic(err) - } - - oauth2Config = oauth2.Config{ - ClientID: config.Instance().OpenIdClientId, - ClientSecret: config.Instance().OpenIdClientSecret, - RedirectURL: config.Instance().OpenIdRedirectURL, - Endpoint: provider.Endpoint(), - Scopes: []string{oidc.ScopeOpenID, "profile", "email"}, - } - - verifier = provider.Verifier(&oidc.Config{ - ClientID: config.Instance().OpenIdClientId, - }) -} - func Login(w http.ResponseWriter, r *http.Request) { var ( state = uuid.NewString()