summaryrefslogtreecommitdiff
path: root/app/controllers/sessions
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-28 16:29:52 -0600
committermo khan <mo@mokhan.ca>2025-04-28 16:29:52 -0600
commit5e63e08113ddeb3195b8ac76f111c2a5c4d24041 (patch)
treef83bda1f05e6ca26f71dc84e4d553d4974ec04d8 /app/controllers/sessions
parent85ff7971afe2d48880259d6bbd899297a9bbbe31 (diff)
refactor: extract a session service class
Diffstat (limited to 'app/controllers/sessions')
-rw-r--r--app/controllers/sessions/controller.go7
-rw-r--r--app/controllers/sessions/service.go30
2 files changed, 34 insertions, 3 deletions
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
+}