<commit_before>package auth

import (
	"github.com/fluxxu/session"
	"github.com/fluxxu/util"
	"github.com/zenazn/goji/web"
	"net/http"
)

func sessionMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		//fmt.Println("session in")
		var sess *session.Session = nil
		var err error
		sid := r.Header.Get("X-TOKEN")
		if sid != "" {
			sess, err = sessionStore.Load(sid)
			if err != nil {
				util.Response(w).Error("load session error: " + err.Error())
				return
			}
			c.Env["session"] = sess
		}
		h.ServeHTTP(w, r)
		if sess != nil {
			sessionStore.Save(sess)
		}
		//fmt.Println("session out")
	}
	return http.HandlerFunc(fn)
}

var skipAuthPath = make(map[string]bool)

func Skip(path string) {
	skipAuthPath[path] = true
}

func authMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		// allow POST /api/token
		if r.Method == "POST" && r.URL.Path == "/api/token" {
			h.ServeHTTP(w, r)
			return
		}

		// skip
		if _, ok := skipAuthPath[r.URL.Path]; ok {
			h.ServeHTTP(w, r)
			return
		}

		// allow login user only
		if c.Env["session"] == nil {
			util.Response(w).Error("Unauthorized", 401)
			return
		}

		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}<commit_msg>Fix invalid token handling...<commit_after>package auth

import (
	"github.com/fluxxu/session"
	"github.com/fluxxu/util"
	"github.com/zenazn/goji/web"
	"net/http"
)

func sessionMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		//fmt.Println("session in")
		var sess *session.Session = nil
		var err error
		sid := r.Header.Get("X-TOKEN")
		if sid != "" {
			sess, err = sessionStore.Load(sid)
			if err != nil {
				util.Response(w).Error("load session error: " + err.Error())
				return
			}
			if sess != nil {
				c.Env["session"] = sess
			}
		}
		h.ServeHTTP(w, r)
		if sess != nil {
			sessionStore.Save(sess)
		}
		//fmt.Println("session out")
	}
	return http.HandlerFunc(fn)
}

var skipAuthPath = make(map[string]bool)

func Skip(path string) {
	skipAuthPath[path] = true
}

func authMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		// allow POST /api/token
		if r.Method == "POST" && r.URL.Path == opts.MuxBase+"/token" {
			h.ServeHTTP(w, r)
			return
		}

		// skip
		if _, ok := skipAuthPath[r.URL.Path]; ok {
			h.ServeHTTP(w, r)
			return
		}

		// allow login user only
		if _, ok := c.Env["session"]; !ok {
			util.Response(w).Error("Unauthorized", 401)
			return
		}

		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}
{'fraction_non_alphanumeric': 0.1669178598342125, 'fraction_numerical': 0.002260738507912585, 'mean_word_length': 3.0972222222222223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3165337', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1028, 'n_words': 219}