rename auth service
This commit is contained in:
@@ -45,7 +45,14 @@ func (s *Session) GenerateCookie() *http.Cookie {
|
||||
}
|
||||
}
|
||||
|
||||
type SessionStore struct {
|
||||
type IAuthenticate interface {
|
||||
IsLogged(r *http.Request) bool
|
||||
Authenticate(username, password string) (*Session, error)
|
||||
}
|
||||
|
||||
var _ IAuthenticate = (*Authentication)(nil)
|
||||
|
||||
type Authentication struct {
|
||||
l sync.RWMutex
|
||||
|
||||
ctx context.Context
|
||||
@@ -54,10 +61,10 @@ type SessionStore struct {
|
||||
sessions map[string]*Session
|
||||
}
|
||||
|
||||
func NewSessionStore(ctx context.Context) *SessionStore {
|
||||
func NewAuthentication(ctx context.Context) *Authentication {
|
||||
ctxChild, fnCancel := context.WithCancel(ctx)
|
||||
|
||||
s := &SessionStore{
|
||||
s := &Authentication{
|
||||
ctx: ctxChild,
|
||||
fnCancel: fnCancel,
|
||||
sessions: map[string]*Session{},
|
||||
@@ -67,13 +74,13 @@ func NewSessionStore(ctx context.Context) *SessionStore {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SessionStore) purge() {
|
||||
s.l.Lock()
|
||||
defer s.l.Unlock()
|
||||
func (a *Authentication) purge() {
|
||||
a.l.Lock()
|
||||
defer a.l.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
toDelete := []*Session{}
|
||||
for _, session := range s.sessions {
|
||||
for _, session := range a.sessions {
|
||||
if now.After(session.expirationTime) {
|
||||
toDelete = append(toDelete, session)
|
||||
}
|
||||
@@ -81,18 +88,18 @@ func (s *SessionStore) purge() {
|
||||
|
||||
for _, session := range toDelete {
|
||||
log.Debug().Str("sessionId", session.sessionID).Msg("purge expired session")
|
||||
delete(s.sessions, session.sessionID)
|
||||
delete(a.sessions, session.sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SessionStore) purgeWorker() {
|
||||
func (a *Authentication) purgeWorker() {
|
||||
ticker := time.NewTicker(10 * time.Second) //nolint
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
s.purge()
|
||||
case <-s.ctx.Done():
|
||||
a.purge()
|
||||
case <-a.ctx.Done():
|
||||
log.Info().Msg("purge worker stopped")
|
||||
ticker.Stop()
|
||||
return
|
||||
@@ -101,45 +108,50 @@ func (s *SessionStore) purgeWorker() {
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *SessionStore) Stop() {
|
||||
s.fnCancel()
|
||||
func (a *Authentication) Stop() {
|
||||
a.fnCancel()
|
||||
}
|
||||
|
||||
func (s *SessionStore) Done() <-chan struct{} {
|
||||
return s.ctx.Done()
|
||||
func (a *Authentication) Done() <-chan struct{} {
|
||||
return a.ctx.Done()
|
||||
}
|
||||
|
||||
func (s *SessionStore) NewSession() (*Session, error) {
|
||||
func (a *Authentication) Authenticate(username, password string) (*Session, error) {
|
||||
adminUsername, adminPassword := GetEnv().GetCredentials()
|
||||
if username != adminUsername || password != adminPassword {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
sessionID, err := generateSessionID()
|
||||
if err != nil {
|
||||
log.Err(err).Msg("unable to generate sessionId")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.l.Lock()
|
||||
defer s.l.Unlock()
|
||||
a.l.Lock()
|
||||
defer a.l.Unlock()
|
||||
|
||||
if _, ok := s.sessions[sessionID]; ok {
|
||||
if _, ok := a.sessions[sessionID]; ok {
|
||||
log.Error().Str("sessionId", sessionID).Msg("sessionId collision")
|
||||
return nil, ErrSessionIDCollision
|
||||
}
|
||||
|
||||
now := time.Now().Add(GetEnv().GetSessionExpirationDuration())
|
||||
session := Session{expirationTime: now, sessionID: sessionID}
|
||||
s.sessions[sessionID] = &session
|
||||
a.sessions[sessionID] = &session
|
||||
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
func (s *SessionStore) IsLogged(r *http.Request) bool {
|
||||
func (a *Authentication) IsLogged(r *http.Request) bool {
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
s.l.RLock()
|
||||
defer s.l.RUnlock()
|
||||
a.l.RLock()
|
||||
defer a.l.RUnlock()
|
||||
|
||||
_, ok := s.sessions[cookie.Value]
|
||||
_, ok := a.sessions[cookie.Value]
|
||||
return ok
|
||||
}
|
||||
Reference in New Issue
Block a user