56 lines
833 B
Go
56 lines
833 B
Go
|
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
|
||
|
}
|