From 62c24d360745ef8778a322c276a9ad577dfc7713 Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 17 Apr 2025 14:08:18 -0600 Subject: refactor: move context keys to key package --- app/controllers/dashboard/controller.go | 7 ++----- app/controllers/dashboard/controller_test.go | 3 ++- pkg/key/init.go | 10 ++++++++++ pkg/oidc/oidc.go | 3 --- pkg/web/middleware/unpack_token.go | 3 ++- pkg/web/middleware/unpack_token_test.go | 7 ++++--- 6 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 pkg/key/init.go 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) })) -- cgit v1.2.3