summaryrefslogtreecommitdiffstats
path: root/back/cmd/authfs/sessdir.go
blob: 186484f8179c6df908f466584a7ba3c2ebff8a39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import (
	"sync"

	"git.samanthony.xyz/buth/back/auth"
	"git.samanthony.xyz/buth/back/qver"
)

type SessionsDir struct {
	*qver.Version

	mu       sync.Mutex // guards below
	sessions map[auth.SessId]*Session
}

func NewSessionsDir(rootVer *qver.Version) *SessionsDir {
	return &SessionsDir{
		qver.New(qver.Parent(rootVer)),
		sync.Mutex{},
		make(map[auth.SessId]*Session),
	}
}

func (dir *SessionsDir) Close() {
	dir.mu.Lock()
	// Don't unlock

	dir.Version.Close()
	for id, sess := range dir.sessions {
		sess.Close()
		delete(dir.sessions, id)
	}
}

// Get returns the session with the given ID if it exists, or false if it doesn't.
func (dir *SessionsDir) Get(id auth.SessId) (*Session, bool) {
	dir.mu.Lock()
	defer dir.mu.Unlock()
	sess, ok := dir.sessions[id]
	return sess, ok
}

// Owner returns the owner of the given session if it exists, or false if it doesn't.
func (dir *SessionsDir) Owner(id auth.SessId) (auth.Uname, bool) {
	if sess, ok := dir.Get(id); ok {
		return sess.Uname, ok
	}
	return "", false
}

func (dir *SessionsDir) Kill(id auth.SessId) {
	dir.mu.Lock()
	defer dir.mu.Unlock()
	if sess, ok := dir.sessions[id]; ok {
		sess.Close()
		delete(dir.sessions, id)
		dir.Version.Bump()
	}
}