summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-28 16:51:56 -0600
committermo khan <mo@mokhan.ca>2025-04-28 16:51:56 -0600
commit059a87a80227426f854256139bbbc7309bdb6fa0 (patch)
treeebd83f6a0f57133c9a34fffaf8c20ed6d62d9250
parent37adeb5437d0f62f5dbc137bb98addcb9d891238 (diff)
feat: redirect to login page when session is established
-rw-r--r--app/controllers/sessions/controller.go6
-rw-r--r--app/controllers/sessions/controller_test.go19
-rw-r--r--app/controllers/sessions/service.go2
-rw-r--r--app/controllers/sessions/service_test.go9
-rw-r--r--pkg/pls/nonce.go11
-rw-r--r--pkg/pls/random.go12
-rw-r--r--pkg/pls/random_test.go17
7 files changed, 63 insertions, 13 deletions
diff --git a/app/controllers/sessions/controller.go b/app/controllers/sessions/controller.go
index 25c215e..e2f4b22 100644
--- a/app/controllers/sessions/controller.go
+++ b/app/controllers/sessions/controller.go
@@ -5,6 +5,7 @@ import (
"time"
"github.com/xlgmokha/x/pkg/log"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/middleware"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web/cookie"
)
@@ -26,6 +27,11 @@ func (c *Controller) MountTo(mux *http.ServeMux) {
}
func (c *Controller) New(w http.ResponseWriter, r *http.Request) {
+ if middleware.IsLoggedIn(r) {
+ http.Redirect(w, r, "/dashboard", http.StatusFound)
+ return
+ }
+
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/controller_test.go b/app/controllers/sessions/controller_test.go
index 64c9fc1..05f642b 100644
--- a/app/controllers/sessions/controller_test.go
+++ b/app/controllers/sessions/controller_test.go
@@ -12,6 +12,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xlgmokha/x/pkg/x"
+ xcfg "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web/cookie"
@@ -68,7 +70,22 @@ func TestSessions(t *testing.T) {
})
})
- t.Run("with an active authenicated session", func(t *testing.T) {})
+ t.Run("with an active authenicated session", func(t *testing.T) {
+ t.Run("redirects to the dashboard", func(t *testing.T) {
+ user := &domain.User{}
+ r, w := test.RequestResponse(
+ "GET",
+ "/session/new",
+ test.WithContextKeyValue(t.Context(), xcfg.CurrentUser, user),
+ )
+
+ mux.ServeHTTP(w, r)
+
+ require.Equal(t, http.StatusFound, w.Code)
+ assert.Equal(t, "/dashboard", w.Header().Get("Location"))
+ })
+ })
+
t.Run("with an expired authenicated session", func(t *testing.T) {})
})
diff --git a/app/controllers/sessions/service.go b/app/controllers/sessions/service.go
index 68ee26d..cbd00fe 100644
--- a/app/controllers/sessions/service.go
+++ b/app/controllers/sessions/service.go
@@ -22,7 +22,7 @@ func NewService(cfg *oidc.OpenID, http *http.Client) *Service {
}
func (svc *Service) GenerateRedirectURL() (string, string) {
- nonce := pls.GenerateNonce(32)
+ nonce := pls.GenerateRandomHex(32)
url := svc.cfg.Config.AuthCodeURL(
nonce,
oauth2.SetAuthURLParam("audience", svc.cfg.Config.ClientID),
diff --git a/app/controllers/sessions/service_test.go b/app/controllers/sessions/service_test.go
new file mode 100644
index 0000000..5f270f0
--- /dev/null
+++ b/app/controllers/sessions/service_test.go
@@ -0,0 +1,9 @@
+package sessions
+
+import "testing"
+
+func TestService(t *testing.T) {
+ t.Run("Exchange", func(t *testing.T) {
+
+ })
+}
diff --git a/pkg/pls/nonce.go b/pkg/pls/nonce.go
deleted file mode 100644
index aeab640..0000000
--- a/pkg/pls/nonce.go
+++ /dev/null
@@ -1,11 +0,0 @@
-package pls
-
-import (
- "crypto/rand"
-)
-
-func GenerateNonce(size int) string {
- nonceBytes := make([]byte, size)
- rand.Read(nonceBytes)
- return string(nonceBytes)
-}
diff --git a/pkg/pls/random.go b/pkg/pls/random.go
new file mode 100644
index 0000000..f7935f1
--- /dev/null
+++ b/pkg/pls/random.go
@@ -0,0 +1,12 @@
+package pls
+
+import (
+ "crypto/rand"
+ "encoding/hex"
+)
+
+func GenerateRandomHex(length int) string {
+ nonceBytes := make([]byte, length)
+ rand.Read(nonceBytes)
+ return hex.EncodeToString(nonceBytes)
+}
diff --git a/pkg/pls/random_test.go b/pkg/pls/random_test.go
new file mode 100644
index 0000000..56dfb2d
--- /dev/null
+++ b/pkg/pls/random_test.go
@@ -0,0 +1,17 @@
+package pls
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestGenerateRandomHex(t *testing.T) {
+ t.Run("returns a random nonce each time", func(t *testing.T) {
+ item := GenerateRandomHex(32)
+
+ require.NotEmpty(t, item)
+ assert.NotEqual(t, item, GenerateRandomHex(32))
+ })
+}