diff options
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/key/init.go | 10 | ||||
| -rw-r--r-- | pkg/oidc/oidc.go | 3 | ||||
| -rw-r--r-- | pkg/web/middleware/unpack_token.go | 3 | ||||
| -rw-r--r-- | pkg/web/middleware/unpack_token_test.go | 7 |
4 files changed, 16 insertions, 7 deletions
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) })) |
