summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-17 14:08:18 -0600
committermo khan <mo@mokhan.ca>2025-04-17 14:08:18 -0600
commit62c24d360745ef8778a322c276a9ad577dfc7713 (patch)
treea93eb5de35fb283bfa014bd3c23094017a2e72c4
parent81d3482b6937617ba3fd778a42d2fbbbf0b8e2b7 (diff)
refactor: move context keys to key package
-rw-r--r--app/controllers/dashboard/controller.go7
-rw-r--r--app/controllers/dashboard/controller_test.go3
-rw-r--r--pkg/key/init.go10
-rw-r--r--pkg/oidc/oidc.go3
-rw-r--r--pkg/web/middleware/unpack_token.go3
-rw-r--r--pkg/web/middleware/unpack_token_test.go7
6 files changed, 20 insertions, 13 deletions
diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go
index d805027..b27ebe1 100644
--- a/app/controllers/dashboard/controller.go
+++ b/app/controllers/dashboard/controller.go
@@ -4,13 +4,10 @@ import (
"html/template"
"net/http"
- "github.com/xlgmokha/x/pkg/context"
"github.com/xlgmokha/x/pkg/x"
- "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
)
-var CurrentUserKey context.Key[*domain.User] = context.Key[*domain.User]("current_user")
-
type Controller struct {
}
@@ -23,7 +20,7 @@ func (c *Controller) MountTo(mux *http.ServeMux) {
}
func (c *Controller) Show(w http.ResponseWriter, r *http.Request) {
- currentUser := CurrentUserKey.From(r.Context())
+ currentUser := key.CurrentUserKey.From(r.Context())
if x.IsZero(currentUser) {
http.Redirect(w, r, "/", http.StatusFound)
diff --git a/app/controllers/dashboard/controller_test.go b/app/controllers/dashboard/controller_test.go
index ff2482f..b8199c4 100644
--- a/app/controllers/dashboard/controller_test.go
+++ b/app/controllers/dashboard/controller_test.go
@@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test"
)
@@ -29,7 +30,7 @@ func TestController(t *testing.T) {
t.Run("when authenticated", func(t *testing.T) {
t.Run("renders a dashboard page", func(t *testing.T) {
- ctx := CurrentUserKey.With(t.Context(), &domain.User{})
+ ctx := key.CurrentUserKey.With(t.Context(), &domain.User{})
r, w := test.RequestResponse("GET", "/dashboard", test.WithContext(ctx))
mux.ServeHTTP(w, r)
diff --git a/pkg/key/init.go b/pkg/key/init.go
new file mode 100644
index 0000000..01d8a3d
--- /dev/null
+++ b/pkg/key/init.go
@@ -0,0 +1,10 @@
+package key
+
+import (
+ "github.com/xlgmokha/x/pkg/context"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc"
+)
+
+var CurrentUserKey context.Key[*domain.User] = context.Key[*domain.User]("current_user")
+var IDTokenKey context.Key[*oidc.IDToken] = context.Key[*oidc.IDToken]("id_token")
diff --git a/pkg/oidc/oidc.go b/pkg/oidc/oidc.go
index 200db69..b82570b 100644
--- a/pkg/oidc/oidc.go
+++ b/pkg/oidc/oidc.go
@@ -4,7 +4,6 @@ import (
"context"
"github.com/coreos/go-oidc/v3/oidc"
- xcontext "github.com/xlgmokha/x/pkg/context"
"golang.org/x/oauth2"
)
@@ -14,8 +13,6 @@ type OpenID struct {
OIDCConfig *oidc.Config
}
-var IDTokenKey xcontext.Key[*IDToken] = xcontext.Key[*IDToken]("id_token")
-
func New(ctx context.Context, issuer string, clientID, clientSecret, callbackURL string) (*OpenID, error) {
provider, err := oidc.NewProvider(ctx, issuer)
if err != nil {
diff --git a/pkg/web/middleware/unpack_token.go b/pkg/web/middleware/unpack_token.go
index 949934b..914f405 100644
--- a/pkg/web/middleware/unpack_token.go
+++ b/pkg/web/middleware/unpack_token.go
@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/xlgmokha/x/pkg/log"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc"
)
@@ -36,7 +37,7 @@ func UnpackToken(cfg *oidc.OpenID) func(http.Handler) http.Handler {
log.WithFields(r.Context(), log.Fields{"id_token": idToken})
next.ServeHTTP(
w,
- r.WithContext(oidc.IDTokenKey.With(r.Context(), idToken)),
+ r.WithContext(key.IDTokenKey.With(r.Context(), idToken)),
)
})
}
diff --git a/pkg/web/middleware/unpack_token_test.go b/pkg/web/middleware/unpack_token_test.go
index 814aa79..1405d6d 100644
--- a/pkg/web/middleware/unpack_token_test.go
+++ b/pkg/web/middleware/unpack_token_test.go
@@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/xlgmokha/x/pkg/log"
"github.com/xlgmokha/x/pkg/x"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
"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"
@@ -46,7 +47,7 @@ func TestUnpackToken(t *testing.T) {
encoded := x.Must(tokens.ToBase64String())
server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- token := oidc.IDTokenKey.From(r.Context())
+ token := key.IDTokenKey.From(r.Context())
require.NotNil(t, token)
assert.Equal(t, user.Subject, token.Subject)
@@ -67,7 +68,7 @@ func TestUnpackToken(t *testing.T) {
t.Run("when an invalid session cookie is provided", func(t *testing.T) {
t.Run("forwards the request", func(t *testing.T) {
server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- require.Nil(t, oidc.IDTokenKey.From(r.Context()))
+ require.Nil(t, key.IDTokenKey.From(r.Context()))
w.WriteHeader(http.StatusTeapot)
}))
@@ -86,7 +87,7 @@ func TestUnpackToken(t *testing.T) {
t.Run("when no cookies are provided", func(t *testing.T) {
t.Run("forwards the request", func(t *testing.T) {
server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- require.Nil(t, oidc.IDTokenKey.From(r.Context()))
+ require.Nil(t, key.IDTokenKey.From(r.Context()))
w.WriteHeader(http.StatusTeapot)
}))