summaryrefslogtreecommitdiffstats
path: root/back/cmd/authfs/sess.go
blob: 3d0cca37568c0fcbce1b4f5b36fcd67fd26dd7b8 (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
package main

import (
	"sync/atomic"
	"time"

	p9 "github.com/Harvey-OS/ninep/protocol"

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

type Session struct {
	auth.SessId
	auth.Uname
	qid   p9.QID
	timer *time.Timer
	dead  *atomic.Bool
}

func NewSession(id auth.SessId, uname auth.Uname) *Session {
	dead := new(atomic.Bool)
	timer := time.AfterFunc(auth.SessTimeout, func() { dead.Store(true) })
	return &Session{
		id,
		uname,
		p9.QID{Path: NextQidPath()},
		timer,
		dead,
	}
}

// Close expires the session immediately.
func (s *Session) Close() {
	s.timer.Stop()
	s.dead.Store(true)
}

// IsActive returns true if the session has not yet expired.
func (s *Session) IsActive() bool {
	return !s.dead.Load()
}

// Extend resets the session's timer or returns false if it has
// already expired.
func (s *Session) Extend() bool {
	if s.timer.Stop() {
		s.timer.Reset(auth.SessTimeout)
		return true
	}
	// already expired
	return false
}

func (s *Session) Qid() (p9.QID, error) { return s.qid, nil }