<commit_before>package util

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gorilla/sessions"

	"buildblast/shared/db"
)

const (
	SESSION_NAME = "default-session"
)

var FLASH_KEYS = []string{
	"invalid_login",
	"username_taken",
	"email_taken",
	"signup_username",
	"signup_email",
	"show_login",
	"show_signup",
	"email_confirmed",
}

type CookieJar struct {
	store   *sessions.CookieStore
	session *sessions.Session
	r       *http.Request
	w       http.ResponseWriter
}

func NewCookieJar(w http.ResponseWriter, r *http.Request, config *ServerConfig) *CookieJar {
	cj := new(CookieJar)
	cj.store = sessions.NewCookieStore(config.CookieKeyPairs...)
	cj.session, _ = cj.store.Get(r, SESSION_NAME)
	if cj.session.IsNew {
		cj.session.Options.Domain = config.CookieDomain
		cj.session.Options.MaxAge = 86400 * 7 // 1 Week
		cj.session.Options.HttpOnly = false
		cj.session.Options.Secure = config.CookieSecure
	}
	cj.w = w
	cj.r = r
	return cj
}

func (cj *CookieJar) Authenticate(dbc *db.Database) (db.Account, db.AccountSession, bool) {
	emptyAccount := db.Account{}
	emptySession := db.AccountSession{}
	sessionKey := cj.GetSessionKey()
	accountSession, err := dbc.GetAccountSession(sessionKey)
	if err != nil {
		// Session not valid. Remove it.
		cj.ClearSessionKey()
		return emptyAccount, emptySession, false<commit_msg>Log an error when failing to read a session cookie.<commit_after>package util

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gorilla/sessions"

	"buildblast/shared/db"
)

const (
	SESSION_NAME = "default-session"
)

var FLASH_KEYS = []string{
	"invalid_login",
	"username_taken",
	"email_taken",
	"signup_username",
	"signup_email",
	"show_login",
	"show_signup",
	"email_confirmed",
}

type CookieJar struct {
	store   *sessions.CookieStore
	session *sessions.Session
	r       *http.Request
	w       http.ResponseWriter
}

func NewCookieJar(w http.ResponseWriter, r *http.Request, config *ServerConfig) *CookieJar {
	cj := new(CookieJar)
	cj.store = sessions.NewCookieStore(config.CookieKeyPairs...)
	var err error
	cj.session, err = cj.store.Get(r, SESSION_NAME)
	if err != nil {
		fmt.Println("Error reading session store:", err)
	}
	if cj.session.IsNew {
		cj.session.Options.Domain = config.CookieDomain
		cj.session.Options.MaxAge = 86400 * 7 // 1 Week
		cj.session.Options.HttpOnly = false
		cj.session.Options.Secure = config.CookieSecure
	}
	cj.w = w
	cj.r = r
	return cj
}

func (cj *CookieJar) Authenticate(dbc *db.Database) (db.Account, db.AccountSession, bool) {
	emptyAccount := db.Account{}
	emptySession := db.AccountSession{}
	sessionKey := cj.GetSessionKey()
	accountSession, err := dbc.GetAccountSession(sessionKey)
	if err != nil {
		// Session not valid. Remove it.
		cj.ClearSessionKey()
		return emptyAccount, emptySession, false
{'fraction_non_alphanumeric': 0.12849964106245512, 'fraction_numerical': 0.005025125628140704, 'mean_word_length': 4.248587570621469, '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': '4587246', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1004, 'n_words': 214}