2021-05-15 14:03:10 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2021-05-15 20:10:07 +00:00
|
|
|
"bitbucket.org/skeris/heruvym/jwt_adapter"
|
2021-05-15 14:03:10 +00:00
|
|
|
"context"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
bearer := r.URL.Query().Get(jwt_adapter.DefaultHeaderKey)
|
|
|
|
|
|
|
|
adapter, err := jwt_adapter.Decode(bearer)
|
|
|
|
if err != nil {
|
|
|
|
mw.logger.Emit(ErrorJwtAccess{Err: err})
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:10:07 +00:00
|
|
|
ctx := context.WithValue(r.Context(), jwt_adapter.DefaultHeaderKey, adapter)
|
2021-05-15 14:03:10 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|