MM-23935 extend session expiry on user activity (#14275)

* MM-23935  extend session expiry on user activity

- if user types anything before a session expires the session will be extended to now + session length

- ensures new session expiries are not written to DB too frequently

- new session store func for updating session ExpiresAt

- session length defaults for mobile and web/ldap changed from 180 days to 30 days
Этот коммит содержится в:
Doug Lauder
2020-05-06 15:41:10 -04:00
коммит произвёл GitHub
родитель 58305b080f
Коммит 5e59b5f70c
18 изменённых файлов: 349 добавлений и 5 удалений

Просмотреть файл

@@ -5840,6 +5840,24 @@ func (s *OpenTracingLayerSessionStore) UpdateDeviceId(id string, deviceId string
return resultVar0, resultVar1
}
func (s *OpenTracingLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateExpiresAt")
s.Root.Store.SetContext(newCtx)
defer func() {
s.Root.Store.SetContext(origCtx)
}()
defer span.Finish()
resultVar0 := s.SessionStore.UpdateExpiresAt(sessionId, time)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (s *OpenTracingLayerSessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateLastActivityAt")

Просмотреть файл

@@ -160,6 +160,14 @@ func (me SqlSessionStore) PermanentDeleteSessionsByUser(userId string) *model.Ap
return nil
}
func (me SqlSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
_, err := me.GetMaster().Exec("UPDATE Sessions SET ExpiresAt = :ExpiresAt WHERE Id = :Id", map[string]interface{}{"ExpiresAt": time, "Id": sessionId})
if err != nil {
return model.NewAppError("SqlSessionStore.UpdateExpiresAt", "store.sql_session.update_expires_at.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError)
}
return nil
}
func (me SqlSessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError {
_, err := me.GetMaster().Exec("UPDATE Sessions SET LastActivityAt = :LastActivityAt WHERE Id = :Id", map[string]interface{}{"LastActivityAt": time, "Id": sessionId})
if err != nil {

Просмотреть файл

@@ -351,6 +351,7 @@ type SessionStore interface {
Remove(sessionIdOrToken string) *model.AppError
RemoveAllSessions() *model.AppError
PermanentDeleteSessionsByUser(teamId string) *model.AppError
UpdateExpiresAt(sessionId string, time int64) *model.AppError
UpdateLastActivityAt(sessionId string, time int64) *model.AppError
UpdateRoles(userId string, roles string) (string, *model.AppError)
UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, *model.AppError)

Просмотреть файл

@@ -213,6 +213,22 @@ func (_m *SessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int
return r0, r1
}
// UpdateExpiresAt provides a mock function with given fields: sessionId, time
func (_m *SessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
ret := _m.Called(sessionId, time)
var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, int64) *model.AppError); ok {
r0 = rf(sessionId, time)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.AppError)
}
}
return r0
}
// UpdateLastActivityAt provides a mock function with given fields: sessionId, time
func (_m *SessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError {
ret := _m.Called(sessionId, time)

Просмотреть файл

@@ -26,6 +26,7 @@ func TestSessionStore(t *testing.T, ss store.Store) {
t.Run("SessionRemoveToken", func(t *testing.T) { testSessionRemoveToken(t, ss) })
t.Run("SessionUpdateDeviceId", func(t *testing.T) { testSessionUpdateDeviceId(t, ss) })
t.Run("SessionUpdateDeviceId2", func(t *testing.T) { testSessionUpdateDeviceId2(t, ss) })
t.Run("UpdateExpiresAt", func(t *testing.T) { testSessionStoreUpdateExpiresAt(t, ss) })
t.Run("UpdateLastActivityAt", func(t *testing.T) { testSessionStoreUpdateLastActivityAt(t, ss) })
t.Run("SessionCount", func(t *testing.T) { testSessionCount(t, ss) })
}
@@ -212,6 +213,21 @@ func testSessionUpdateDeviceId2(t *testing.T, ss store.Store) {
require.Nil(t, err)
}
func testSessionStoreUpdateExpiresAt(t *testing.T, ss store.Store) {
s1 := &model.Session{}
s1.UserId = model.NewId()
s1, err := ss.Session().Save(s1)
require.Nil(t, err)
err = ss.Session().UpdateExpiresAt(s1.Id, 1234567890)
require.Nil(t, err)
session, err := ss.Session().Get(s1.Id)
require.Nil(t, err)
require.EqualValues(t, session.ExpiresAt, 1234567890, "ExpiresAt not updated correctly")
}
func testSessionStoreUpdateLastActivityAt(t *testing.T, ss store.Store) {
s1 := &model.Session{}
s1.UserId = model.NewId()

Просмотреть файл

@@ -5291,6 +5291,22 @@ func (s *TimerLayerSessionStore) UpdateDeviceId(id string, deviceId string, expi
return resultVar0, resultVar1
}
func (s *TimerLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
start := timemodule.Now()
resultVar0 := s.SessionStore.UpdateExpiresAt(sessionId, time)
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
if s.Root.Metrics != nil {
success := "false"
if resultVar0 == nil {
success = "true"
}
s.Root.Metrics.ObserveStoreMethodDuration("SessionStore.UpdateExpiresAt", success, elapsed)
}
return resultVar0
}
func (s *TimerLayerSessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError {
start := timemodule.Now()