|
@@ -4,6 +4,7 @@ import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"errors"
|
|
"errors"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "log"
|
|
"net/http"
|
|
"net/http"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
@@ -52,9 +53,29 @@ func NewSwitchableAccountPoolManager(sysdb *database.Database, parent *AuthAgent
|
|
ExpireTime: 604800,
|
|
ExpireTime: 604800,
|
|
authAgent: parent,
|
|
authAgent: parent,
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ //Do an initialization cleanup
|
|
|
|
+ go func() {
|
|
|
|
+ thisManager.RunNightlyCleanup()
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ //Return the manager
|
|
return &thisManager
|
|
return &thisManager
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// When called, this will clear the account switching pool in which all users session has expired
|
|
|
|
+func (m *SwitchableAccountPoolManager) RunNightlyCleanup() {
|
|
|
|
+ pools, err := m.GetAllPools()
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println("[auth] Unable to load account switching pools. Cleaning skipped: " + err.Error())
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, pool := range pools {
|
|
|
|
+ pool.DeletePoolIfAllUserSessionExpired()
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// Handle switchable account listing for this browser
|
|
// Handle switchable account listing for this browser
|
|
func (m *SwitchableAccountPoolManager) HandleSwitchableAccountListing(w http.ResponseWriter, r *http.Request) {
|
|
func (m *SwitchableAccountPoolManager) HandleSwitchableAccountListing(w http.ResponseWriter, r *http.Request) {
|
|
//Get username and pool id
|
|
//Get username and pool id
|
|
@@ -363,6 +384,21 @@ func (p *SwitchableAccountsPool) RemoveUser(username string) {
|
|
p.Save()
|
|
p.Save()
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Save changes of this pool to database
|
|
|
|
+func (p *SwitchableAccountsPool) DeletePoolIfAllUserSessionExpired() {
|
|
|
|
+ allExpred := true
|
|
|
|
+ for _, acc := range p.Accounts {
|
|
|
|
+ if !p.IsAccountExpired(acc) {
|
|
|
|
+ allExpred = false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if allExpred {
|
|
|
|
+ //All account expired. Remove this pool
|
|
|
|
+ p.Delete()
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// Save changes of this pool to database
|
|
// Save changes of this pool to database
|
|
func (p *SwitchableAccountsPool) Save() {
|
|
func (p *SwitchableAccountsPool) Save() {
|
|
p.parent.Database.Write("auth_acswitch", p.UUID, p)
|
|
p.parent.Database.Write("auth_acswitch", p.UUID, p)
|
|
@@ -372,3 +408,8 @@ func (p *SwitchableAccountsPool) Save() {
|
|
func (p *SwitchableAccountsPool) Delete() error {
|
|
func (p *SwitchableAccountsPool) Delete() error {
|
|
return p.parent.Database.Delete("auth_acswitch", p.UUID)
|
|
return p.parent.Database.Delete("auth_acswitch", p.UUID)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// Check if an account is expired
|
|
|
|
+func (p *SwitchableAccountsPool) IsAccountExpired(acc *SwitchableAccount) bool {
|
|
|
|
+ return time.Now().Unix() > acc.LastSwitch+p.parent.ExpireTime
|
|
|
|
+}
|