Skip to content

Commit f3cdc81

Browse files
Implemented refersh for session and more fields for session
1 parent 04fbc1c commit f3cdc81

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

cmd/laclm/main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func exec() error {
4747
Use: "laclm <command> <subcommand>",
4848
Short: "Backend server for linux acl management",
4949
Example: heredoc.Doc(`
50-
$ laclm
5150
$ laclm --config /path/to/config.yaml
5251
`),
5352
Run: func(cmd *cobra.Command, args []string) {
@@ -187,8 +186,6 @@ func run(ctx context.Context) error {
187186
return err
188187
}
189188

190-
// <-ctx.Done()
191-
192189
/*
193190
after this, exit signal is triggered
194191
following code must be executed to shutdown graceful shutdown

internal/auth/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func LoginHandler(sessionManager *session.Manager) http.HandlerFunc {
4444
}
4545

4646
/* after building session manager */
47-
err = sessionManager.CreateSession(user.Username)
47+
err = sessionManager.CreateSession(user.Username, r.RemoteAddr, r.UserAgent())
4848
if err != nil {
4949
zap.L().Error("Error creating session",
5050
zap.Error(err),

internal/session/manager.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
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 */
7891
func (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+
}

internal/session/model.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,39 @@ import (
66
"time"
77
)
88

9-
/* session struct for a user */
9+
/*
10+
session struct for a user
11+
appropriate fields must always be updated when any request is made
12+
*/
1013
type Session struct {
14+
/* keep count of completed and failed transactions */
15+
CompletedCount int
16+
FailedCount int
17+
18+
/* unique ID of session [will be associated with the user forever in logs] */
19+
ID string
20+
21+
/* username of the user */
1122
Username string
23+
24+
/*
25+
IP and UserAgent for security logs
26+
also can be used for blacklisting and whitelistings
27+
illegal useragents can be caught as well as unauthorized IP addresses
28+
*/
29+
IP string
30+
UserAgent string
31+
32+
/* for logging user activity */
33+
CreatedAt time.Time
34+
LastActiveAt time.Time
1235
Expiry time.Time
1336
Timer *time.Timer
37+
38+
/* transactions issued by the user */
1439
TransactionQueue *list.List
40+
41+
/* mutex for thread safety */
1542
Mutex sync.Mutex
1643

1744
/*

0 commit comments

Comments
 (0)