Files
mc-tgbot/services/mc/auth.go
2023-08-28 15:57:58 +08:00

53 lines
866 B
Go

package mc
import (
"sync"
"tg-mc/models"
"time"
)
type Auth interface {
IsAuthed(u models.User, expireMode bool) bool
Auth(u models.User)
Reject(u models.User)
}
type Authcator struct {
UserMap *sync.Map
}
var authcator *Authcator
func GetAuthcator() Auth {
if authcator == nil {
authcator = &Authcator{
UserMap: &sync.Map{},
}
}
return authcator
}
func (a *Authcator) IsAuthed(u models.User, expireMode bool) bool {
if u.MCName != "VaalaCat" {
return true
}
if approveTime, ok := a.UserMap.Load(u.MCName); ok {
if !expireMode {
return true
} else if time.Since(approveTime.(time.Time)) < 30*time.Second {
return true
} else {
return false
}
}
return false
}
func (a *Authcator) Auth(u models.User) {
a.UserMap.Store(u.MCName, time.Now())
}
func (a *Authcator) Reject(u models.User) {
a.UserMap.Delete(u.MCName)
}