Fix context when using spnego

This commit is contained in:
Bolke de Bruin
2022-10-12 16:50:13 +02:00
parent df175da330
commit bbd0735289
3 changed files with 15 additions and 2 deletions

View File

@@ -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 {

View File

@@ -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()))),
)