Live video on the AT Protocol

oauth: don't clobber the context you goof

+19 -10
+16 -2
pkg/api/api.go
··· 18 19 "github.com/NYTimes/gziphandler" 20 "github.com/bluesky-social/indigo/api/bsky" 21 "github.com/julienschmidt/httprouter" 22 "github.com/rs/cors" 23 sloghttp "github.com/samber/slog-http" ··· 126 // api/playback/iame.li/hls/source/000000000000.ts 127 128 func (a *StreamplaceAPI) Handler(ctx context.Context) (http.Handler, error) { 129 var xrpc http.Handler 130 xrpc, err := spxrpc.NewServer(ctx, a.CLI, a.Model) 131 if err != nil { 132 return nil, err 133 } 134 - 135 xrpc = a.op.OAuthMiddleware(xrpc) 136 router := httprouter.New() 137 router.Handler("GET", "/oauth/*anything", a.op.Handler()) 138 router.Handler("POST", "/oauth/*anything", a.op.Handler()) 139 router.Handler("GET", "/.well-known/oauth-authorization-server", a.op.Handler()) ··· 243 handler = sloghttp.New(slog.Default())(handler) 244 handler = a.RateLimitMiddleware(ctx)(handler) 245 246 return handler, nil 247 } 248 - 249 func copyHeader(dst, src http.Header) { 250 for k, vv := range src { 251 // we'll handle CORS ourselves, thanks
··· 18 19 "github.com/NYTimes/gziphandler" 20 "github.com/bluesky-social/indigo/api/bsky" 21 + "github.com/google/uuid" 22 "github.com/julienschmidt/httprouter" 23 "github.com/rs/cors" 24 sloghttp "github.com/samber/slog-http" ··· 127 // api/playback/iame.li/hls/source/000000000000.ts 128 129 func (a *StreamplaceAPI) Handler(ctx context.Context) (http.Handler, error) { 130 + 131 var xrpc http.Handler 132 xrpc, err := spxrpc.NewServer(ctx, a.CLI, a.Model) 133 if err != nil { 134 return nil, err 135 } 136 xrpc = a.op.OAuthMiddleware(xrpc) 137 router := httprouter.New() 138 + 139 router.Handler("GET", "/oauth/*anything", a.op.Handler()) 140 router.Handler("POST", "/oauth/*anything", a.op.Handler()) 141 router.Handler("GET", "/.well-known/oauth-authorization-server", a.op.Handler()) ··· 245 handler = sloghttp.New(slog.Default())(handler) 246 handler = a.RateLimitMiddleware(ctx)(handler) 247 248 + // this needs to be LAST so nothing else clobbers the context 249 + handler = a.ContextMiddleware(ctx)(handler) 250 + 251 return handler, nil 252 } 253 + func (a *StreamplaceAPI) ContextMiddleware(ctx context.Context) func(next http.Handler) http.Handler { 254 + return func(next http.Handler) http.Handler { 255 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 256 + uuid := uuid.New().String() 257 + ctx = log.WithLogValues(ctx, "requestID", uuid, "method", r.Method, "path", r.URL.Path) 258 + r = r.WithContext(ctx) 259 + next.ServeHTTP(w, r) 260 + }) 261 + } 262 + } 263 func copyHeader(dst, src http.Header) { 264 for k, vv := range src { 265 // we'll handle CORS ourselves, thanks
+1 -1
pkg/oproxy/oauth_middleware.go
··· 124 return nil, fmt.Errorf("could not get oauth session: %w", err) 125 } 126 if session == nil { 127 - return nil, nil 128 } 129 if session.RevokedAt != nil { 130 return nil, fmt.Errorf("oauth session revoked")
··· 124 return nil, fmt.Errorf("could not get oauth session: %w", err) 125 } 126 if session == nil { 127 + return nil, echo.NewHTTPError(http.StatusUnauthorized, "oauth session not found") 128 } 129 if session.RevokedAt != nil { 130 return nil, fmt.Errorf("oauth session revoked")
+2 -7
pkg/spxrpc/spxrpc.go
··· 4 "context" 5 "net/http" 6 7 - "github.com/google/uuid" 8 "github.com/labstack/echo/v4" 9 "stream.place/streamplace/pkg/config" 10 "stream.place/streamplace/pkg/log" ··· 24 cli: cli, 25 model: model, 26 } 27 - e.Use(s.ErrorHandlingMiddleware(ctx)) 28 err := s.RegisterHandlersPlaceStream(e) 29 if err != nil { 30 return nil, err ··· 46 s.e.ServeHTTP(w, r) 47 } 48 49 - func (s *Server) ErrorHandlingMiddleware(ctx context.Context) echo.MiddlewareFunc { 50 return func(next echo.HandlerFunc) echo.HandlerFunc { 51 return func(c echo.Context) error { 52 - req := c.Request() 53 - uuid := uuid.New().String() 54 - ctx = log.WithLogValues(ctx, "requestID", uuid, "method", req.Method, "path", req.URL.Path) 55 - c.SetRequest(req.WithContext(ctx)) 56 err := next(c) 57 if err == nil { 58 return nil
··· 4 "context" 5 "net/http" 6 7 "github.com/labstack/echo/v4" 8 "stream.place/streamplace/pkg/config" 9 "stream.place/streamplace/pkg/log" ··· 23 cli: cli, 24 model: model, 25 } 26 + e.Use(s.ErrorHandlingMiddleware()) 27 err := s.RegisterHandlersPlaceStream(e) 28 if err != nil { 29 return nil, err ··· 45 s.e.ServeHTTP(w, r) 46 } 47 48 + func (s *Server) ErrorHandlingMiddleware() echo.MiddlewareFunc { 49 return func(next echo.HandlerFunc) echo.HandlerFunc { 50 return func(c echo.Context) error { 51 err := next(c) 52 if err == nil { 53 return nil