Skip to content

Commit

Permalink
feat: open sessions per user (#142) (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaqueveras committed Oct 10, 2023
1 parent 5b45a6c commit 3af4b44
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"access_log_directory": "/var/log/powersso/access.log",
"error_log_directory": "/var/log/powersso/error.log",
"permission_base": "github.com/isaqueveras/power-sso",
"access_control_allow_origin": "*"
"access_control_allow_origin": "*",
"open_sessions_per_user": 5
},
"database": {
"host": "localhost",
Expand Down
1 change: 1 addition & 0 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ServerConfig struct {
ErrorLogDirectory string `json:"error_log_directory"`
PermissionBase string `json:"permission_base"`
AccessControlAllowOrigin string `json:"access_control_allow_origin"`
OpenSessionsPerUser int64 `json:"open_sessions_per_user"`
SSL bool `json:"ssl"`
CSRF bool `json:"srf"`
Debug bool `json:"debug"`
Expand Down
15 changes: 15 additions & 0 deletions infrastructure/persistencie/auth/postgres/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/isaqueveras/powersso/config"
"github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/oops"
)
Expand All @@ -29,6 +30,20 @@ func (pg *PGSession) Create(userID *uuid.UUID, clientIP, userAgent *string) (ses
return nil, oops.Err(err)
}

if _, err = pg.DB.Builder.
Update("sessions").
Set("deleted_at", squirrel.Expr("NOW()")).
Where(`id NOT IN (
SELECT id FROM sessions
WHERE user_id = ? AND deleted_at IS NULL
ORDER BY created_at DESC
LIMIT ?
)`, userID, config.Get().Server.OpenSessionsPerUser).
Where("user_id = ?", userID).
Exec(); err != nil {
return nil, oops.Err(err)
}

if _, err = pg.DB.Builder.
Update("users").
Set("attempts", 0).
Expand Down

0 comments on commit 3af4b44

Please sign in to comment.