You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.1 KiB
56 lines
1.1 KiB
package models
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/binary"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// The Session model represents a user being logged in.
|
|
type Session struct {
|
|
ID string
|
|
UserID int
|
|
Expires time.Time
|
|
}
|
|
|
|
// GenerateID generates a unique ID for the session that's 64 base36 digits long.
|
|
func (session *Session) GenerateID() error {
|
|
result := ""
|
|
offset := 0
|
|
data := make([]byte, 128)
|
|
|
|
_, err := rand.Read(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for len(result) < 64 {
|
|
result += strconv.FormatUint(binary.LittleEndian.Uint64(data[offset:]), 36)
|
|
offset += 8
|
|
|
|
if offset >= 128 {
|
|
_, err = rand.Read(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
offset = 0
|
|
}
|
|
}
|
|
|
|
session.ID = result[:64]
|
|
|
|
return nil
|
|
}
|
|
|
|
// SessionRepository is an interface for all database operations
|
|
// the user model makes.
|
|
type SessionRepository interface {
|
|
// FindSessionByID finds a non-expired session by ID.
|
|
FindSessionByID(id int) (Session, error)
|
|
InsertSession(session Session) error
|
|
RemoveSession(session Session) error
|
|
ClearUserSessions(user User) error
|
|
RemoveExpiredSessions() error
|
|
}
|