Initial euclide.org release
This commit is contained in:
73
authCache.go
Normal file
73
authCache.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AuthCache structure, to avoid hitting to hard on the ldap servers
|
||||
type AuthCache struct {
|
||||
m sync.RWMutex
|
||||
users map[string]time.Time
|
||||
gpgKeys []string
|
||||
gpgTime time.Time
|
||||
}
|
||||
|
||||
// lock the structure
|
||||
func (a *AuthCache) lock() {
|
||||
a.m.Lock()
|
||||
}
|
||||
|
||||
// or unlock it
|
||||
func (a *AuthCache) unlock() {
|
||||
a.m.Unlock()
|
||||
}
|
||||
|
||||
// NewAuthCache initializes the cache
|
||||
func NewAuthCache() *AuthCache {
|
||||
return &AuthCache{
|
||||
users: make(map[string]time.Time),
|
||||
}
|
||||
}
|
||||
|
||||
// Get checks if a user is cached
|
||||
// and reinitialize counter to add another minute
|
||||
func (a *AuthCache) Get(user string) bool {
|
||||
a.lock()
|
||||
defer a.unlock()
|
||||
now := time.Now()
|
||||
if added, ok := a.users[user]; ok {
|
||||
if added.Add(5 * time.Minute).After(now) {
|
||||
a.users[user] = now
|
||||
return true
|
||||
}
|
||||
delete(a.users, user)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Set marks the user valid for a minute
|
||||
func (a *AuthCache) Set(user string) {
|
||||
a.lock()
|
||||
defer a.unlock()
|
||||
a.users[user] = time.Now()
|
||||
}
|
||||
|
||||
// PgpGet the pgp Keys if cached
|
||||
// and reinitialize the counter to add another minute
|
||||
func (a *AuthCache) PgpGet() []string {
|
||||
now := time.Now()
|
||||
if len(a.gpgKeys) > 0 && a.gpgTime.Add(5*time.Minute).After(now) {
|
||||
a.gpgTime = now
|
||||
return a.gpgKeys
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// PgpSet store the pgp Keys for a minute
|
||||
func (a *AuthCache) PgpSet(keys []string) {
|
||||
a.lock()
|
||||
defer a.unlock()
|
||||
a.gpgKeys = keys
|
||||
a.gpgTime = time.Now()
|
||||
}
|
||||
Reference in New Issue
Block a user