Add persistent session storage with SQLite
This commit is contained in:
parent
747d9fe3c7
commit
b7072a5865
10 changed files with 135 additions and 43 deletions
|
|
@ -4,13 +4,31 @@ import (
|
|||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/internal/models"
|
||||
)
|
||||
|
||||
type token struct {
|
||||
userName string
|
||||
created time.Time
|
||||
lastAccess time.Time
|
||||
func (a *Authenticator) newSession(user string) (http.Cookie, error) {
|
||||
key := generateKey()
|
||||
var session models.Token
|
||||
var cookie http.Cookie
|
||||
session.User = user
|
||||
session.Expiration = time.Now().Add(time.Hour * 24)
|
||||
a.sessions[key] = session
|
||||
err := a.s.AddSession(key, user, session.Expiration)
|
||||
if err != nil {
|
||||
return cookie, err
|
||||
}
|
||||
|
||||
cookie.Name = "token"
|
||||
cookie.Value = key
|
||||
cookie.HttpOnly = true
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Path = "/"
|
||||
//cookie.Secure = true
|
||||
return cookie, nil
|
||||
}
|
||||
|
||||
func generateKey() string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue