<commit_before>package middlewares

import (
	"net/http"
	"strings"

	"github.com/getfider/fider/app/pkg/jwt"
	"github.com/getfider/fider/app/pkg/web"
	"github.com/getfider/fider/app/storage"
)

// JwtGetter gets JWT token from cookie and add into context
func JwtGetter(users storage.User) web.MiddlewareFunc {
	return func(next web.HandlerFunc) web.HandlerFunc {
		return func(c web.Context) error {

			if cookie, err := c.Cookie("auth"); err == nil {
				if claims, err := jwt.Decode(cookie.Value); err == nil {
					if user, err := users.GetByID(claims.UserID); err == nil {
						if user.Tenant.ID == c.Tenant().ID {
							c.SetUser(user)
						}
					}
				} else {
					c.Logger().Error(err)
				}
			}

			return next(c)
		}
	}
}

// JwtSetter sets JWT token into cookie
func JwtSetter() web.MiddlewareFunc {
	return func(next web.HandlerFunc) web.HandlerFunc {
		return func(c web.Context) error {

			query := c.Request().URL.Query()

			jwt := query.Get("jwt")
			if jwt != "" {
				c.SetCookie(&http.Cookie{
					Name:     "auth",
					Value:    jwt,
					HttpOnly: true,
					Path:     "/",
				})

				query.Del("jwt")

				url := c.BaseURL() + c.Request().URL.Path
				querystring := query.Encode()
				if querystring != "" {
					url += "?" + querystring
				}

				return c.Redirect(http.StatusTemporaryRedirect, url)
			}

			return next(c)
		}
	}
}

func stripPort(hostport string) string {
	colon := strings.IndexByte(hostport, ':')
	if colon == -1 {
		return hostport
	}
	return hostport[:colon]
}
<commit_msg>long lived cookie<commit_after>package middlewares

import (
	"net/http"
	"strings"
	"time"

	"github.com/getfider/fider/app/pkg/jwt"
	"github.com/getfider/fider/app/pkg/web"
	"github.com/getfider/fider/app/storage"
)

// JwtGetter gets JWT token from cookie and add into context
func JwtGetter(users storage.User) web.MiddlewareFunc {
	return func(next web.HandlerFunc) web.HandlerFunc {
		return func(c web.Context) error {

			if cookie, err := c.Cookie("auth"); err == nil {
				if claims, err := jwt.Decode(cookie.Value); err == nil {
					if user, err := users.GetByID(claims.UserID); err == nil {
						if user.Tenant.ID == c.Tenant().ID {
							c.SetUser(user)
						}
					}
				} else {
					c.Logger().Error(err)
				}
			}

			return next(c)
		}
	}
}

// JwtSetter sets JWT token into cookie
func JwtSetter() web.MiddlewareFunc {
	return func(next web.HandlerFunc) web.HandlerFunc {
		return func(c web.Context) error {

			query := c.Request().URL.Query()

			jwt := query.Get("jwt")
			if jwt != "" {
				c.SetCookie(&http.Cookie{
					Name:     "auth",
					Value:    jwt,
					HttpOnly: true,
					Path:     "/",
					Expires:  time.Now().Add(365 * 24 * time.Hour),
				})

				query.Del("jwt")

				url := c.BaseURL() + c.Request().URL.Path
				querystring := query.Encode()
				if querystring != "" {
					url += "?" + querystring
				}

				return c.Redirect(http.StatusTemporaryRedirect, url)
			}

			return next(c)
		}
	}
}

func stripPort(hostport string) string {
	colon := strings.IndexByte(hostport, ':')
	if colon == -1 {
		return hostport
	}
	return hostport[:colon]
}

{'fraction_non_alphanumeric': 0.15298626636857235, 'fraction_numerical': 0.002235707441711913, 'mean_word_length': 2.7553956834532376, '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': '5592938', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1126, 'n_words': 237}