From 5e63e08113ddeb3195b8ac76f111c2a5c4d24041 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 28 Apr 2025 16:29:52 -0600 Subject: refactor: extract a session service class --- app/controllers/sessions/controller.go | 7 ++++--- app/controllers/sessions/service.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 app/controllers/sessions/service.go (limited to 'app') diff --git a/app/controllers/sessions/controller.go b/app/controllers/sessions/controller.go index 7549dc7..6c61ec0 100644 --- a/app/controllers/sessions/controller.go +++ b/app/controllers/sessions/controller.go @@ -7,7 +7,6 @@ import ( "github.com/xlgmokha/x/pkg/log" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web/cookie" "golang.org/x/oauth2" ) @@ -15,12 +14,15 @@ import ( type Controller struct { cfg *oidc.OpenID http *http.Client + + svc *Service } func New(cfg *oidc.OpenID, http *http.Client) *Controller { return &Controller{ cfg: cfg, http: http, + svc: NewService(cfg, http), } } @@ -31,8 +33,7 @@ func (c *Controller) MountTo(mux *http.ServeMux) { } func (c *Controller) New(w http.ResponseWriter, r *http.Request) { - nonce := pls.GenerateNonce(32) - url := c.cfg.Config.AuthCodeURL(nonce, oauth2.SetAuthURLParam("audience", c.cfg.Config.ClientID)) + url, nonce := c.svc.GenerateRedirectURL() http.SetCookie(w, cookie.New("oauth_state", nonce, time.Now().Add(10*time.Minute))) http.Redirect(w, r, url, http.StatusFound) } diff --git a/app/controllers/sessions/service.go b/app/controllers/sessions/service.go new file mode 100644 index 0000000..e9a37c6 --- /dev/null +++ b/app/controllers/sessions/service.go @@ -0,0 +1,30 @@ +package sessions + +import ( + "net/http" + + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" + "golang.org/x/oauth2" +) + +type Service struct { + cfg *oidc.OpenID + http *http.Client +} + +func NewService(cfg *oidc.OpenID, http *http.Client) *Service { + return &Service{ + cfg: cfg, + http: http, + } +} + +func (svc *Service) GenerateRedirectURL() (string, string) { + nonce := pls.GenerateNonce(32) + url := svc.cfg.Config.AuthCodeURL( + nonce, + oauth2.SetAuthURLParam("audience", svc.cfg.Config.ClientID), + ) + return url, nonce +} -- cgit v1.2.3