diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-25 15:36:04 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-25 15:36:04 -0600 |
| commit | 786c4d2cd0860032e2624cadbbf54891d4269ae2 (patch) | |
| tree | 3ad2aa0e298d97b431982183224c1e8c6a7ed817 /app | |
| parent | 8836d0744aed44435e35af78d7533ff09db0c62e (diff) | |
refactor: move key pacakge to cfg
Diffstat (limited to 'app')
| -rw-r--r-- | app/cfg/cfg.go | 10 | ||||
| -rw-r--r-- | app/controllers/dashboard/controller.go | 4 | ||||
| -rw-r--r-- | app/controllers/dashboard/controller_test.go | 4 | ||||
| -rw-r--r-- | app/controllers/sparkles/controller_test.go | 4 | ||||
| -rw-r--r-- | app/controllers/sparkles/init.go | 4 | ||||
| -rw-r--r-- | app/middleware/id_token.go | 4 | ||||
| -rw-r--r-- | app/middleware/id_token_test.go | 8 | ||||
| -rw-r--r-- | app/middleware/require_user.go | 4 | ||||
| -rw-r--r-- | app/middleware/require_user_test.go | 4 | ||||
| -rw-r--r-- | app/middleware/user.go | 6 | ||||
| -rw-r--r-- | app/middleware/user_test.go | 12 |
11 files changed, 37 insertions, 27 deletions
diff --git a/app/cfg/cfg.go b/app/cfg/cfg.go new file mode 100644 index 0000000..2dbadc4 --- /dev/null +++ b/app/cfg/cfg.go @@ -0,0 +1,10 @@ +package cfg + +import ( + "github.com/xlgmokha/x/pkg/context" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" +) + +var CurrentUser context.Key[*domain.User] = context.Key[*domain.User]("current_user") +var IDToken context.Key[*oidc.IDToken] = context.Key[*oidc.IDToken]("id_token") diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go index a1d1bbf..ef5b18d 100644 --- a/app/controllers/dashboard/controller.go +++ b/app/controllers/dashboard/controller.go @@ -4,9 +4,9 @@ import ( "net/http" "github.com/xlgmokha/x/pkg/log" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/middleware" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/views" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key" ) type Controller struct { @@ -23,7 +23,7 @@ func (c *Controller) MountTo(mux *http.ServeMux) { } func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { - currentUser := key.CurrentUser.From(r.Context()) + currentUser := cfg.CurrentUser.From(r.Context()) w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "text/html") diff --git a/app/controllers/dashboard/controller_test.go b/app/controllers/dashboard/controller_test.go index 20e16ce..ced3fd5 100644 --- a/app/controllers/dashboard/controller_test.go +++ b/app/controllers/dashboard/controller_test.go @@ -5,8 +5,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + "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/key" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" ) @@ -30,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 := key.CurrentUser.With(t.Context(), &domain.User{}) + ctx := cfg.CurrentUser.With(t.Context(), &domain.User{}) r, w := test.RequestResponse("GET", "/dashboard", test.WithContext(ctx)) mux.ServeHTTP(w, r) diff --git a/app/controllers/sparkles/controller_test.go b/app/controllers/sparkles/controller_test.go index 21f4ec7..c158f32 100644 --- a/app/controllers/sparkles/controller_test.go +++ b/app/controllers/sparkles/controller_test.go @@ -7,9 +7,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/xlgmokha/x/pkg/serde" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/db" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/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" ) @@ -58,7 +58,7 @@ func TestSparkles(t *testing.T) { "/sparkles", test.WithAcceptHeader(serde.JSON), test.WithContentType(sparkle, serde.JSON), - test.WithContextKeyValue(t.Context(), key.CurrentUser, currentUser), + test.WithContextKeyValue(t.Context(), cfg.CurrentUser, currentUser), ) mux.ServeHTTP(response, request) diff --git a/app/controllers/sparkles/init.go b/app/controllers/sparkles/init.go index 2586c9a..1118a09 100644 --- a/app/controllers/sparkles/init.go +++ b/app/controllers/sparkles/init.go @@ -6,8 +6,8 @@ import ( "github.com/xlgmokha/x/pkg/log" "github.com/xlgmokha/x/pkg/mapper" "github.com/xlgmokha/x/pkg/serde" + "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/key" ) func init() { @@ -16,7 +16,7 @@ func init() { if err != nil { log.WithFields(r.Context(), log.Fields{"error": err}) } - sparkle.Author = key.CurrentUser.From(r.Context()) + sparkle.Author = cfg.CurrentUser.From(r.Context()) return sparkle }) } diff --git a/app/middleware/id_token.go b/app/middleware/id_token.go index a32c77b..da39f43 100644 --- a/app/middleware/id_token.go +++ b/app/middleware/id_token.go @@ -5,7 +5,7 @@ import ( "github.com/xlgmokha/x/pkg/log" "github.com/xlgmokha/x/pkg/x" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key" + xcfg "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" ) @@ -43,7 +43,7 @@ func IDToken(cfg *oidc.OpenID) func(http.Handler) http.Handler { log.WithFields(r.Context(), log.Fields{"id_token": idToken}) next.ServeHTTP( w, - r.WithContext(key.IDToken.With(r.Context(), idToken)), + r.WithContext(xcfg.IDToken.With(r.Context(), idToken)), ) return } diff --git a/app/middleware/id_token_test.go b/app/middleware/id_token_test.go index 4f26cdf..607c028 100644 --- a/app/middleware/id_token_test.go +++ b/app/middleware/id_token_test.go @@ -12,7 +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" + xcfg "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "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" @@ -47,7 +47,7 @@ func TestIDToken(t *testing.T) { encoded := x.Must(tokens.ToBase64String()) server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - token := key.IDToken.From(r.Context()) + token := xcfg.IDToken.From(r.Context()) require.NotNil(t, token) assert.Equal(t, user.Subject, token.Subject) @@ -68,7 +68,7 @@ func TestIDToken(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, key.IDToken.From(r.Context())) + require.Nil(t, xcfg.IDToken.From(r.Context())) w.WriteHeader(http.StatusTeapot) })) @@ -87,7 +87,7 @@ func TestIDToken(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, key.IDToken.From(r.Context())) + require.Nil(t, xcfg.IDToken.From(r.Context())) w.WriteHeader(http.StatusTeapot) })) diff --git a/app/middleware/require_user.go b/app/middleware/require_user.go index e81d5b5..8df4fd7 100644 --- a/app/middleware/require_user.go +++ b/app/middleware/require_user.go @@ -4,13 +4,13 @@ import ( "net/http" "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/app/cfg" ) func RequireUser(code int, url string) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user := key.CurrentUser.From(r.Context()) + user := cfg.CurrentUser.From(r.Context()) if x.IsZero(user) { http.Redirect(w, r, url, code) return diff --git a/app/middleware/require_user_test.go b/app/middleware/require_user_test.go index 68b9911..17c0276 100644 --- a/app/middleware/require_user_test.go +++ b/app/middleware/require_user_test.go @@ -6,8 +6,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "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/key" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" ) @@ -30,7 +30,7 @@ func TestRequireUser(t *testing.T) { t.Run("when a user is logged in", func(t *testing.T) { t.Run("forwards the request", func(t *testing.T) { - r, w := test.RequestResponse("GET", "/example", test.WithContextKeyValue(t.Context(), key.CurrentUser, &domain.User{})) + r, w := test.RequestResponse("GET", "/example", test.WithContextKeyValue(t.Context(), cfg.CurrentUser, &domain.User{})) server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) diff --git a/app/middleware/user.go b/app/middleware/user.go index 194ded6..e2f1ce3 100644 --- a/app/middleware/user.go +++ b/app/middleware/user.go @@ -6,15 +6,15 @@ import ( "github.com/xlgmokha/x/pkg/log" "github.com/xlgmokha/x/pkg/mapper" "github.com/xlgmokha/x/pkg/x" + "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/key" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" ) func User(db domain.Repository[*domain.User]) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - idToken := key.IDToken.From(r.Context()) + idToken := cfg.IDToken.From(r.Context()) if x.IsZero(idToken) { next.ServeHTTP(w, r) return @@ -30,7 +30,7 @@ func User(db domain.Repository[*domain.User]) func(http.Handler) http.Handler { } } - next.ServeHTTP(w, r.WithContext(key.CurrentUser.With(r.Context(), user))) + next.ServeHTTP(w, r.WithContext(cfg.CurrentUser.With(r.Context(), user))) }) } } diff --git a/app/middleware/user_test.go b/app/middleware/user_test.go index e6c74d8..3e2425c 100644 --- a/app/middleware/user_test.go +++ b/app/middleware/user_test.go @@ -6,9 +6,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/db" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" - "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/pls" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" @@ -24,14 +24,14 @@ func TestUser(t *testing.T) { t.Run("when ID Token is provided", func(t *testing.T) { t.Run("when user is known", func(t *testing.T) { server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user := key.CurrentUser.From(r.Context()) + user := cfg.CurrentUser.From(r.Context()) require.NotNil(t, user) assert.Equal(t, knownUser.ID, user.ID) w.WriteHeader(http.StatusTeapot) })) - ctx := key.IDToken.With(t.Context(), &oidc.IDToken{Subject: knownUser.ID.String()}) + ctx := cfg.IDToken.With(t.Context(), &oidc.IDToken{Subject: knownUser.ID.String()}) r, w := test.RequestResponse("GET", "/example", test.WithContext(ctx)) server.ServeHTTP(w, r) @@ -43,14 +43,14 @@ func TestUser(t *testing.T) { unknownID := pls.GenerateULID() server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user := key.CurrentUser.From(r.Context()) + user := cfg.CurrentUser.From(r.Context()) require.NotNil(t, user) assert.Equal(t, domain.ID(unknownID), user.ID) w.WriteHeader(http.StatusTeapot) })) - ctx := key.IDToken.With(t.Context(), &oidc.IDToken{Subject: unknownID}) + ctx := cfg.IDToken.With(t.Context(), &oidc.IDToken{Subject: unknownID}) r, w := test.RequestResponse("GET", "/example", test.WithContext(ctx)) server.ServeHTTP(w, r) @@ -62,7 +62,7 @@ func TestUser(t *testing.T) { t.Run("when ID Token is not provided", func(t *testing.T) { server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user := key.CurrentUser.From(r.Context()) + user := cfg.CurrentUser.From(r.Context()) require.Nil(t, user) w.WriteHeader(http.StatusTeapot) |
