// MWFunc is a middleware func for JWT Authorization
MWFunc is a middleware func for JWT Authorization
// MWFunc is a middleware func for JWT Authorization
func (j *JWT) MWFunc(next http.Handler) http.Handler {
	var (
		missingAuthorizationHeader, _ = json.Marshal(errMsg{Message: "Missing Authorization header"})
		missingBearerKeyword, _       = json.Marshal(errMsg{Message: "Missing Bearer keyword"})
		cannotParseToken, _           = json.Marshal(errMsg{Message: errorParsingToken.Error()})
		cannotRetreiveSession, _      = json.Marshal(errMsg{Message: "Error retreiving session"})
	)
	fn := func(w http.ResponseWriter, r *http.Request) {
		ah := r.Header.Get("Authorization")
		if ah == "" {
			w.WriteHeader(http.StatusUnauthorized)
			w.Write(missingAuthorizationHeader)
			return
		}
		spl := strings.Split(ah, " ")
		if spl[0] != "Bearer" {
			w.WriteHeader(http.StatusUnauthorized)
			w.Write(missingBearerKeyword)
			return
		}
		token := spl[1]
		if err := j.ParseToken(token); err != nil {
			w.WriteHeader(http.StatusUnauthorized)
			w.Write(cannotParseToken)
			return
		}
		user, err := j.sess.Get(token)
		if err != nil {
			w.WriteHeader(http.StatusUnauthorized)
			w.Write(cannotRetreiveSession)
			return
		}
		ctx := r.Context()
		ctx = context.WithValue(ctx, TokenKey, token)
		ctx = context.WithValue(ctx, UserIDKey, user.ID)
		ctx = context.WithValue(ctx, UserDisplayNameKey, user.DisplayName)
		ctx = context.WithValue(ctx, UserRoleKey, user.Role)
		ctx = context.WithValue(ctx, UserEmailKey, user.Email)
		next.ServeHTTP(w, r.WithContext(ctx))
	}
	return http.HandlerFunc(fn)
}
func ( j * JWT ) MWFunc ( next http . Handler ) http . Handler { var ( missingAuthorizationHeader , _ = json . Marshal ( errMsg { Message : " " } ) missingBearerKeyword , _ = json . Marshal ( errMsg { Message : " " } ) cannotParseToken , _ = json . Marshal ( errMsg { Message : errorParsingToken . Error ( ) } ) cannotRetreiveSession , _ = json . Marshal ( errMsg { Message : " " } ) ) fn := func ( w http . ResponseWriter , r * http . Request ) { ah := r . Header . Get ( " " ) if ah == " " { w . WriteHeader ( http . StatusUnauthorized ) w . Write ( missingAuthorizationHeader ) return } spl := strings . Split ( ah , " " ) if spl [ 0 ] != " " { w . WriteHeader ( http . StatusUnauthorized ) w . Write ( missingBearerKeyword ) return } token := spl [ 1 ] if err := j . ParseToken ( token ) ; err != nil { w . WriteHeader ( http . StatusUnauthorized ) w . Write ( cannotParseToken ) return } user , err := j . sess . Get ( token ) if err != nil { w . WriteHeader ( http . StatusUnauthorized ) w . Write ( cannotRetreiveSession ) return } ctx := r . Context ( ) ctx = context . WithValue ( ctx , TokenKey , token ) ctx = context . WithValue ( ctx , UserIDKey , user . ID ) ctx = context . WithValue ( ctx , UserDisplayNameKey , user . DisplayName ) ctx = context . WithValue ( ctx , UserRoleKey , user . Role ) ctx = context . WithValue ( ctx , UserEmailKey , user . Email ) next . ServeHTTP ( w , r . WithContext ( ctx ) ) } return http . HandlerFunc ( fn ) }
MWFunc is a middleware func for JWT Authorization
MWFunc is a middleware func for JWT Authorization