heruvym/middleware/sse_middleware.go

40 lines
925 B
Go
Raw Normal View History

2021-05-15 14:03:10 +00:00
package middleware
import (
"context"
2021-09-05 15:24:13 +00:00
"heruvym/jwt_adapter"
2021-05-15 14:03:10 +00:00
"net/http"
)
func (mw *Middleware) MiddlewareGetJwt(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Escape non-GET requests
if r.Method != http.MethodGet {
next.ServeHTTP(w, r)
return
}
2021-09-05 15:24:13 +00:00
ctx := r.Context()
2021-05-15 14:03:10 +00:00
bearer := r.URL.Query().Get(jwt_adapter.DefaultHeaderKey)
if bearer != "" {
2021-05-15 14:03:10 +00:00
adapter, err := jwt_adapter.Decode(bearer)
if err == nil {
//mw.logger.Emit(ErrorJwtAccess{Err: err})
//w.WriteHeader(http.StatusUnauthorized)
//return
ctx = context.WithValue(r.Context(), jwt_adapter.DefaultHeaderKey, adapter)
//
}
} else {
sess := r.URL.Query().Get("s")
if sess == "" {
return
}
ctx = context.WithValue(r.Context(), jwt_adapter.DefaultHeaderKey, &jwt_adapter.JwtAdapter{Id: sess})
2021-05-15 14:03:10 +00:00
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}