@@ -38,6 +38,8 @@ import (
38
38
"github.com/grafana/quickpizza/pkg/web"
39
39
)
40
40
41
+ const tokenLength = 16
42
+
41
43
// Variables storing prometheus metrics.
42
44
var (
43
45
pizzaRecommendations = promauto .NewCounterVec (prometheus.CounterOpts {
@@ -117,7 +119,7 @@ func NewServer() (*Server, error) {
117
119
router .Use (cors .New (cors.Options {
118
120
AllowedOrigins : []string {"*" },
119
121
AllowedMethods : []string {"GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" },
120
- AllowedHeaders : []string {"Accept" , "Authorization" , "Content-Type" , "X-CSRF-Token" , "X-User-ID" },
122
+ AllowedHeaders : []string {"Accept" , "Authorization" , "Content-Type" , "X-CSRF-Token" },
121
123
ExposedHeaders : []string {"Link" },
122
124
AllowCredentials : true ,
123
125
MaxAge : 300 , // Maximum value not ignored by any of major browsers
@@ -741,13 +743,19 @@ func SvelteKitHandler(path string) http.Handler {
741
743
742
744
func ValidateUserMiddleware (next http.Handler ) http.Handler {
743
745
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
744
- userID := r .Header .Get ("X-User-ID" )
745
- if userID == "" {
746
+ auth := r .Header .Get ("Authorization" )
747
+ prefix , token , found := strings .Cut (auth , " " )
748
+ prefix = strings .ToLower (prefix )
749
+
750
+ // Here, we would actually check the token against the DB, or
751
+ // verify it using a private key (e.g. for JWT), but for this
752
+ // testing service we just check its length.
753
+ if ! found || prefix != "token" || len (token ) != tokenLength {
746
754
w .WriteHeader (http .StatusUnauthorized )
747
755
return
748
756
}
749
757
750
- ctx := context .WithValue (r .Context (), "user " , userID )
758
+ ctx := context .WithValue (r .Context (), "authorization " , auth )
751
759
next .ServeHTTP (w , r .WithContext (ctx ))
752
760
})
753
761
}
0 commit comments