<commit_before>package main

import (
	"code.google.com/p/go.crypto/bcrypt"
	"github.com/hoisie/web"
	"math/rand"
	"strconv"
)

type SessionManager struct {
	sessions []string
	users    map[string]string
}

func NewSessionManager(config *Config) *SessionManager {
	return &SessionManager{make([]string, 0), config.Users}
}

func (sm *SessionManager) SessionExists(id string) bool {
	for _, i := range sm.sessions {
		if i == id {
			return true
		}
	}
	return false
}

func (sm *SessionManager) LoggedIn(ctx *web.Context) bool {
	if id, ok := ctx.GetSecureCookie("TDB-user"); ok && sm.SessionExists(id) {
		return true
	}
	return false
}

func (sm *SessionManager) Login(ctx *web.Context, user, pass string) bool {
	if sm.LoggedIn(ctx) {
		return true
	}
	if pHash, ok := sm.users[user]; ok && bcrypt.CompareHashAndPassword([]byte(pHash), []byte(pass)) == nil {
		id := makeSessionId()
		sm.sessions = append(sm.sessions, id)
		ctx.SetSecureCookie("TDB-user", id, 60)
		return true
	}
	return false
}

func makeSessionId() string {
	return strconv.Itoa(rand.Int())
}<commit_msg>lengthend session length to two minutes and made sessions expire on the server<commit_after>package main

import (
	"code.google.com/p/go.crypto/bcrypt"
	"github.com/hoisie/web"
	"math/rand"
	"strconv"
    "time"
)

type session struct{
    id string
    expiresOn time.Time
}

type SessionManager struct {
	sessions []session
	users    map[string]string
}

func NewSessionManager(config *Config) *SessionManager {
	return &SessionManager{make([]session, 0), config.Users}
}

func (sm *SessionManager) SessionExists(id string) bool {
	for _, s := range sm.sessions {
		if s.id == id && time.Now().Before(s.expiresOn) {
			return true
		}
	}
	return false
}

func (sm *SessionManager) removeExpired(){
    w := 0
    for _, s := range sm.sessions {
        if time.Now().After(s.expiresOn) {
            continue
        }
        sm.sessions[w] = s
        w++
    }
    sm.sessions = sm.sessions[:w]
}

func (sm *SessionManager) LoggedIn(ctx *web.Context) bool {
	if id, ok := ctx.GetSecureCookie("TDB-user"); ok && sm.SessionExists(id) {
		return true
	}
	return false
}

func (sm *SessionManager) Login(ctx *web.Context, user, pass string) bool {
    sm.removeExpired()
	if sm.LoggedIn(ctx) {
		return true
	}
	if pHash, ok := sm.users[user]; ok && bcrypt.CompareHashAndPassword([]byte(pHash), []byte(pass)) == nil {
        s := session{makeSessionId(), time.Now().Add(2*time.Minute)}
		sm.sessions = append(sm.sessions, s)
		ctx.SetSecureCookie("TDB-user", s.id, 120)
		return true
	}
	return false
}

func makeSessionId() string {
	return strconv.Itoa(rand.Int())
}
{'fraction_non_alphanumeric': 0.14345035862589656, 'fraction_numerical': 0.0033975084937712344, 'mean_word_length': 3.774774774774775, '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': '5339354', 'n_tokens_mistral': 981, 'n_tokens_neox': 951, 'n_words': 227}