<commit_before>package permissions

import (
	"crypto/sha256"
	"crypto/subtle"
	"errors"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"time"

	"code.google.com/p/go.crypto/bcrypt"
	"github.com/xyproto/simpleredis"
)

const (
	defaultRedisServer = ":6379"
)

var (
	minConfirmationCodeLength = 20 // minimum length of the confirmation code
)

type UserState struct {
	// see: http://redis.io/topics/data-types
	users        *simpleredis.HashMap        // Hash map of users, with several different fields per user ("loggedin", "confirmed", "email" etc)
	usernames    *simpleredis.Set            // A list of all usernames, for easy enumeration
	unconfirmed  *simpleredis.Set            // A list of unconfirmed usernames, for easy enumeration
	pool         *simpleredis.ConnectionPool // A connection pool for Redis
	dbindex      int                         // Redis database index
	cookieSecret string                      // Secret for storing secure cookies
	cookieTime   int64                       // How long a cookie should last, in seconds
	passwordAlgo string                      // The hashing algorithm to utilize default: "bcrypt" allowed: ("sha256", "bcrypt")
}

// Huge interface for making it possible to depend on different versions of this permission package
// TODO: Make the simpleredis things into interfaces
type UserStateKeeper interface {
	DatabaseIndex() int
	Pool() *simpleredis.ConnectionPool
	Close()
	UserRights(req *http.Request) bool
	HasUser(username string) bool
	BooleanField(username, fieldname string) bool
	SetBooleanField(username, fieldname string, val bool)
	IsConfirmed(username string) bool
	IsLoggedIn(username string) bool
	AdminRights(req *http.Request) bool
	IsAdmin(username string) bool
	UsernameCookie(req *http.Request) (string, error)
	SetUsernameCookie(w http.ResponseWriter, username string) error
	AllUsernames() ([]string, error)
	Email(username string) (string, error)
	PasswordHash(username string) (string, error)
	AllUnconfirmedUsernames() ([]string, error)
	ConfirmationCode(username string) (string, error)
	Users() *simpleredis.HashMap
	AddUnconfirmed(username, confirmationCode string)
	RemoveUnconfirmed(username string)
	MarkConfirmed(username string)
	RemoveUser(username string)
	SetAdminStatus(username string)
	RemoveAdminStatus(username string)
	addUserUnchecked(username, passwordHash, email string)
	AddUser(username, password, email string)
	SetLoggedIn(username string)<commit_msg>Minor change to a comment<commit_after>package permissions

import (
	"crypto/sha256"
	"crypto/subtle"
	"errors"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"time"

	"code.google.com/p/go.crypto/bcrypt"
	"github.com/xyproto/simpleredis"
)

const (
	defaultRedisServer = ":6379"
)

var (
	minConfirmationCodeLength = 20 // minimum length of the confirmation code
)

type UserState struct {
	// see: http://redis.io/topics/data-types
	users        *simpleredis.HashMap        // Hash map of users, with several different fields per user ("loggedin", "confirmed", "email" etc)
	usernames    *simpleredis.Set            // A list of all usernames, for easy enumeration
	unconfirmed  *simpleredis.Set            // A list of unconfirmed usernames, for easy enumeration
	pool         *simpleredis.ConnectionPool // A connection pool for Redis
	dbindex      int                         // Redis database index
	cookieSecret string                      // Secret for storing secure cookies
	cookieTime   int64                       // How long a cookie should last, in seconds
	passwordAlgo string                      // The hashing algorithm to utilize default: "bcrypt" allowed: ("sha256", "bcrypt")
}

// Huge interface for making it possible to depend on different versions of the permission package
// TODO: Make the simpleredis things into interfaces
type UserStateKeeper interface {
	DatabaseIndex() int
	Pool() *simpleredis.ConnectionPool
	Close()
	UserRights(req *http.Request) bool
	HasUser(username string) bool
	BooleanField(username, fieldname string) bool
	SetBooleanField(username, fieldname string, val bool)
	IsConfirmed(username string) bool
	IsLoggedIn(username string) bool
	AdminRights(req *http.Request) bool
	IsAdmin(username string) bool
	UsernameCookie(req *http.Request) (string, error)
	SetUsernameCookie(w http.ResponseWriter, username string) error
	AllUsernames() ([]string, error)
	Email(username string) (string, error)
	PasswordHash(username string) (string, error)
	AllUnconfirmedUsernames() ([]string, error)
	ConfirmationCode(username string) (string, error)
	Users() *simpleredis.HashMap
	AddUnconfirmed(username, confirmationCode string)
	RemoveUnconfirmed(username string)
	MarkConfirmed(username string)
	RemoveUser(username string)
	SetAdminStatus(username string)
	RemoveAdminStatus(username string)
	addUserUnchecked(username, passwordHash, email string)
	AddUser(username, password, email string)
	SetLoggedIn(username string)
{'fraction_non_alphanumeric': 0.08782272819678796, 'fraction_numerical': 0.005692213864606627, 'mean_word_length': 3.9848024316109423, 'pattern_counts': {'":': 2, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2701217', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1406, 'n_words': 437}