This commit is contained in:
Daniel Ponte 2022-10-26 19:13:50 -04:00
parent c033dced54
commit b4b4fa51b2
2 changed files with 56 additions and 1 deletions

View file

@ -50,7 +50,7 @@ func (a *Authenticator) Provider(name string) AuthProvider {
func (a *Authenticator) InitAuth(s storage.Store) error {
a.Flows = make(FlowStore)
a.Sessions = make(SessionStore)
a.Sessions.init()
hap, err := NewHAProvider(s)
if err != nil {
return err

55
pkg/auth/session.go Normal file
View file

@ -0,0 +1,55 @@
package auth
import (
"net/http"
"time"
)
type SessionStore struct {
s map[TokenID]*Token
lastCull time.Time
}
type TokenID string
type Token struct {
ID TokenID
Ctime time.Time
Expires time.Duration
Addr string
}
func (ss *SessionStore) init() {
ss.s = make(map[TokenID]*Token)
}
const cullInterval = 5 * time.Minute
func (ss *SessionStore) cull() {
if now := time.Now(); now.Sub(ss.lastCull) > cullInterval {
for k, v := range ss.s {
if now.After(v.Ctime.Add(v.Expires)) {
delete(ss.s, k)
}
}
}
}
func (ss *SessionStore) register(t *Token) {
ss.cull()
ss.s[t.ID] = t
}
func (a *Authenticator) NewToken(r *http.Request, f *Flow) TokenID {
id := TokenID(genUUID())
t := &Token{
ID: id,
Ctime: time.Now(),
Addr: r.RemoteAddr,
}
a.Sessions.register(t)
return id
}