From bbd07352892a11745c86b092e7c5ec70f055ea01 Mon Sep 17 00:00:00 2001 From: Bolke de Bruin <bolke@xs4all.nl> Date: Wed, 12 Oct 2022 16:50:13 +0200 Subject: [PATCH] Fix context when using spnego --- cmd/rdpgw/common/remote.go | 13 +++++++++++++ cmd/rdpgw/main.go | 2 +- go.mod | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cmd/rdpgw/common/remote.go b/cmd/rdpgw/common/remote.go index 9e91942..f835e6e 100644 --- a/cmd/rdpgw/common/remote.go +++ b/cmd/rdpgw/common/remote.go @@ -2,6 +2,7 @@ package common import ( "context" + "github.com/jcmturner/goidentity/v6" "log" "net" "net/http" @@ -44,6 +45,18 @@ func EnrichContext(next http.Handler) http.Handler { }) } +func FixKerberosContext(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + id := goidentity.FromHTTPRequestContext(r) + if id != nil { + ctx = context.WithValue(ctx, UsernameCtx, id.UserName()) + } + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + func GetClientIp(ctx context.Context) string { s, ok := ctx.Value(ClientIPCtx).(string) if !ok { diff --git a/cmd/rdpgw/main.go b/cmd/rdpgw/main.go index 6d91b79..d7a78fc 100644 --- a/cmd/rdpgw/main.go +++ b/cmd/rdpgw/main.go @@ -215,7 +215,7 @@ func main() { } http.Handle("/remoteDesktopGateway/", common.EnrichContext( spnego.SPNEGOKRB5Authenticate( - http.HandlerFunc(gw.HandleGatewayProtocol), + common.FixKerberosContext(http.HandlerFunc(gw.HandleGatewayProtocol)), keytab, service.Logger(log.Default()))), ) diff --git a/go.mod b/go.mod index ad14f4b..01c99e0 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/gorilla/sessions v1.2.1 github.com/gorilla/websocket v1.5.0 github.com/jcmturner/gofork v1.7.6 + github.com/jcmturner/goidentity/v6 v6.0.1 github.com/knadh/koanf v1.4.2 github.com/msteinert/pam v1.0.0 github.com/patrickmn/go-cache v2.1.0+incompatible @@ -31,7 +32,6 @@ require ( github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/jcmturner/aescts/v2 v2.0.0 // indirect github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect - github.com/jcmturner/goidentity/v6 v6.0.1 // indirect github.com/jcmturner/rpc/v2 v2.0.3 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect -- GitLab