66 "sync"
77 "time"
88
9+ "github.com/google/uuid"
10+
911 "github.com/PythonHacker24/linux-acl-management-backend/config"
1012)
1113
@@ -24,7 +26,7 @@ type Manager struct {
2426}
2527
2628/* for creating a session for user - used by HTTP HANDLERS */
27- func (m * Manager ) CreateSession (username string ) error {
29+ func (m * Manager ) CreateSession (username , ipAddress , userAgent string ) error {
2830
2931 /* lock the ActiveSessions mutex till the function ends */
3032 m .mutex .Lock ()
@@ -35,13 +37,24 @@ func (m *Manager) CreateSession(username string) error {
3537 return fmt .Errorf ("user already exists in active sessions" )
3638 }
3739
40+ /* Generate session metadata */
41+ sessionID := uuid .New ().String ()
42+ now := time .Now ()
43+
3844 /* create the session */
3945 session := & Session {
46+ ID : sessionID ,
4047 Username : username ,
48+ IP : ipAddress ,
49+ UserAgent : userAgent ,
4150 Expiry : time .Now ().Add (time .Duration (config .BackendConfig .AppInfo .SessionTimeout ) * time .Hour ),
42- Timer : time .AfterFunc (time .Duration (config .BackendConfig .AppInfo .SessionTimeout )* time .Hour ,
51+ CreatedAt : now ,
52+ LastActiveAt : now ,
53+ Timer : time .AfterFunc (time .Duration (config .BackendConfig .AppInfo .SessionTimeout ) * time .Hour ,
4354 func () { m .ExpireSession (username ) },
4455 ),
56+ CompletedCount : 0 ,
57+ FailedCount : 0 ,
4558 TransactionQueue : list .New (),
4659 }
4760
@@ -76,7 +89,7 @@ func (m *Manager) ExpireSession(username string) {
7689
7790/* add transaction to a session */
7891func (m * Manager ) AddTransaction (username string , txn interface {}) error {
79- /* thread safety the manager mutex */
92+ /* thread safety for the manager */
8093 m .mutex .Lock ()
8194 defer m .mutex .Unlock ()
8295
@@ -95,3 +108,32 @@ func (m *Manager) AddTransaction(username string, txn interface{}) error {
95108
96109 return nil
97110}
111+
112+ /* refresh the session timer */
113+ func (m * Manager ) RefreshTimer (username string ) error {
114+ /* thread safety for the manager */
115+ m .mutex .Lock ()
116+ defer m .mutex .Unlock ()
117+
118+ /* get session from sessionMap */
119+ session , exists := m .sessionsMap [username ]
120+ if ! exists {
121+ return fmt .Errorf ("Session not found" )
122+ }
123+
124+ /* reset the expiry time and last active time */
125+ session .Expiry = time .Now ().Add (time .Duration (config .BackendConfig .AppInfo .SessionTimeout ) * time .Hour )
126+ session .LastActiveAt = time .Now ()
127+
128+ /* stop the session timer */
129+ if session .Timer != nil {
130+ session .Timer .Stop ()
131+ }
132+
133+ /* reset the session timer */
134+ session .Timer = time .AfterFunc (time .Duration (config .BackendConfig .AppInfo .SessionTimeout ) * time .Hour ,
135+ func () { m .ExpireSession (username ) },
136+ )
137+
138+ return nil
139+ }
0 commit comments