30 lines
673 B
Go
30 lines
673 B
Go
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"bitbucket.org/skeris/profile/jwt_adapter"
|
||
|
"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
|
||
|
}
|
||
|
|
||
|
ctx := context.WithValue(r.Context(), "JWT", adapter)
|
||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||
|
})
|
||
|
}
|