MM-41211: Replaces SessionLength*InDays with SessionLength*InHours. (#19838)
* MM-41211: Replaces SessionLength[Web|Mobile|SSO]InDays with SessionLength[Web|Mobile|SSO]InHours. * MM-41211: Clear the value of the old config settings.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
4bc2ed3973
Коммит
cb3d8f0a1c
@@ -277,10 +277,10 @@ type AppIface interface {
|
||||
SessionHasPermissionToManageBot(session model.Session, botUserId string) *model.AppError
|
||||
// SessionIsRegistered determines if a specific session has been registered
|
||||
SessionIsRegistered(session model.Session) bool
|
||||
// SetSessionExpireInDays sets the session's expiry the specified number of days
|
||||
// SetSessionExpireInHours sets the session's expiry the specified number of hours
|
||||
// relative to either the session creation date or the current time, depending
|
||||
// on the `ExtendSessionOnActivity` config setting.
|
||||
SetSessionExpireInDays(session *model.Session, days int)
|
||||
SetSessionExpireInHours(session *model.Session, hours int)
|
||||
// SetStatusDoNotDisturbTimed takes endtime in unix epoch format in UTC
|
||||
// and sets status of given userId to dnd which will be restored back after endtime
|
||||
SetStatusDoNotDisturbTimed(userId string, endtime int64)
|
||||
|
||||
@@ -83,7 +83,7 @@ func (a *App) getSessionExpiredPushMessage(session *model.Session) string {
|
||||
T := i18n.GetUserTranslations(locale)
|
||||
|
||||
siteName := *a.Config().TeamSettings.SiteName
|
||||
props := map[string]interface{}{"siteName": siteName, "daysCount": *a.Config().ServiceSettings.SessionLengthMobileInDays}
|
||||
props := map[string]interface{}{"siteName": siteName, "hoursCount": *a.Config().ServiceSettings.SessionLengthMobileInHours}
|
||||
|
||||
return T("api.push_notifications.session.expired", props)
|
||||
}
|
||||
|
||||
24
app/login.go
24
app/login.go
@@ -178,7 +178,7 @@ func (a *App) DoLogin(c *request.Context, w http.ResponseWriter, r *http.Request
|
||||
session.GenerateCSRF()
|
||||
|
||||
if deviceID != "" {
|
||||
a.ch.srv.userService.SetSessionExpireInDays(session, *a.Config().ServiceSettings.SessionLengthMobileInDays)
|
||||
a.ch.srv.userService.SetSessionExpireInHours(session, *a.Config().ServiceSettings.SessionLengthMobileInHours)
|
||||
|
||||
// A special case where we logout of all other sessions with the same Id
|
||||
if err := a.RevokeSessionsForDeviceId(user.Id, deviceID, ""); err != nil {
|
||||
@@ -186,11 +186,11 @@ func (a *App) DoLogin(c *request.Context, w http.ResponseWriter, r *http.Request
|
||||
return err
|
||||
}
|
||||
} else if isMobile {
|
||||
a.ch.srv.userService.SetSessionExpireInDays(session, *a.Config().ServiceSettings.SessionLengthMobileInDays)
|
||||
a.ch.srv.userService.SetSessionExpireInHours(session, *a.Config().ServiceSettings.SessionLengthMobileInHours)
|
||||
} else if isOAuthUser || isSaml {
|
||||
a.ch.srv.userService.SetSessionExpireInDays(session, *a.Config().ServiceSettings.SessionLengthSSOInDays)
|
||||
a.ch.srv.userService.SetSessionExpireInHours(session, *a.Config().ServiceSettings.SessionLengthSSOInHours)
|
||||
} else {
|
||||
a.ch.srv.userService.SetSessionExpireInDays(session, *a.Config().ServiceSettings.SessionLengthWebInDays)
|
||||
a.ch.srv.userService.SetSessionExpireInHours(session, *a.Config().ServiceSettings.SessionLengthWebInHours)
|
||||
}
|
||||
|
||||
ua := uasurfer.Parse(r.UserAgent())
|
||||
@@ -245,9 +245,9 @@ func (a *App) AttachCloudSessionCookie(c *request.Context, w http.ResponseWriter
|
||||
secure = true
|
||||
}
|
||||
|
||||
maxAge := *a.Config().ServiceSettings.SessionLengthWebInDays * 60 * 60 * 24
|
||||
maxAgeSeconds := *a.Config().ServiceSettings.SessionLengthWebInHours * 60 * 60
|
||||
subpath, _ := utils.GetSubpathFromConfig(a.Config())
|
||||
expiresAt := time.Unix(model.GetMillis()/1000+int64(maxAge), 0)
|
||||
expiresAt := time.Unix(model.GetMillis()/1000+int64(maxAgeSeconds), 0)
|
||||
|
||||
domain := ""
|
||||
if siteURL, err := url.Parse(a.GetSiteURL()); err == nil {
|
||||
@@ -276,7 +276,7 @@ func (a *App) AttachCloudSessionCookie(c *request.Context, w http.ResponseWriter
|
||||
Name: model.SessionCookieCloudUrl,
|
||||
Value: workspaceName,
|
||||
Path: subpath,
|
||||
MaxAge: maxAge,
|
||||
MaxAge: maxAgeSeconds,
|
||||
Expires: expiresAt,
|
||||
Domain: domain,
|
||||
Secure: secure,
|
||||
@@ -292,16 +292,16 @@ func (a *App) AttachSessionCookies(c *request.Context, w http.ResponseWriter, r
|
||||
secure = true
|
||||
}
|
||||
|
||||
maxAge := *a.Config().ServiceSettings.SessionLengthWebInDays * 60 * 60 * 24
|
||||
maxAgeSeconds := *a.Config().ServiceSettings.SessionLengthWebInHours * 60 * 60
|
||||
domain := a.GetCookieDomain()
|
||||
subpath, _ := utils.GetSubpathFromConfig(a.Config())
|
||||
|
||||
expiresAt := time.Unix(model.GetMillis()/1000+int64(maxAge), 0)
|
||||
expiresAt := time.Unix(model.GetMillis()/1000+int64(maxAgeSeconds), 0)
|
||||
sessionCookie := &http.Cookie{
|
||||
Name: model.SessionCookieToken,
|
||||
Value: c.Session().Token,
|
||||
Path: subpath,
|
||||
MaxAge: maxAge,
|
||||
MaxAge: maxAgeSeconds,
|
||||
Expires: expiresAt,
|
||||
HttpOnly: true,
|
||||
Domain: domain,
|
||||
@@ -312,7 +312,7 @@ func (a *App) AttachSessionCookies(c *request.Context, w http.ResponseWriter, r
|
||||
Name: model.SessionCookieUser,
|
||||
Value: c.Session().UserId,
|
||||
Path: subpath,
|
||||
MaxAge: maxAge,
|
||||
MaxAge: maxAgeSeconds,
|
||||
Expires: expiresAt,
|
||||
Domain: domain,
|
||||
Secure: secure,
|
||||
@@ -322,7 +322,7 @@ func (a *App) AttachSessionCookies(c *request.Context, w http.ResponseWriter, r
|
||||
Name: model.SessionCookieCsrf,
|
||||
Value: c.Session().GetCSRF(),
|
||||
Path: subpath,
|
||||
MaxAge: maxAge,
|
||||
MaxAge: maxAgeSeconds,
|
||||
Expires: expiresAt,
|
||||
Domain: domain,
|
||||
Secure: secure,
|
||||
|
||||
26
app/oauth.go
26
app/oauth.go
@@ -314,10 +314,10 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectURI, c
|
||||
} else {
|
||||
// Return the same token and no need to create a new session
|
||||
accessRsp = &model.AccessResponse{
|
||||
AccessToken: accessData.Token,
|
||||
TokenType: model.AccessTokenType,
|
||||
RefreshToken: accessData.RefreshToken,
|
||||
ExpiresIn: int32((accessData.ExpiresAt - model.GetMillis()) / 1000),
|
||||
AccessToken: accessData.Token,
|
||||
TokenType: model.AccessTokenType,
|
||||
RefreshToken: accessData.RefreshToken,
|
||||
ExpiresInSeconds: int32((accessData.ExpiresAt - model.GetMillis()) / 1000),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -335,10 +335,10 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectURI, c
|
||||
}
|
||||
|
||||
accessRsp = &model.AccessResponse{
|
||||
AccessToken: session.Token,
|
||||
TokenType: model.AccessTokenType,
|
||||
RefreshToken: accessData.RefreshToken,
|
||||
ExpiresIn: int32(*a.Config().ServiceSettings.SessionLengthSSOInDays * 60 * 60 * 24),
|
||||
AccessToken: session.Token,
|
||||
TokenType: model.AccessTokenType,
|
||||
RefreshToken: accessData.RefreshToken,
|
||||
ExpiresInSeconds: int32(*a.Config().ServiceSettings.SessionLengthSSOInHours * 60 * 60),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -371,7 +371,7 @@ func (a *App) newSession(app *model.OAuthApp, user *model.User) (*model.Session,
|
||||
// Set new token an session
|
||||
session := &model.Session{UserId: user.Id, Roles: user.Roles, IsOAuth: true}
|
||||
session.GenerateCSRF()
|
||||
a.ch.srv.userService.SetSessionExpireInDays(session, *a.Config().ServiceSettings.SessionLengthSSOInDays)
|
||||
a.ch.srv.userService.SetSessionExpireInHours(session, *a.Config().ServiceSettings.SessionLengthSSOInHours)
|
||||
session.AddProp(model.SessionPropPlatform, app.Name)
|
||||
session.AddProp(model.SessionPropOAuthAppID, app.Id)
|
||||
session.AddProp(model.SessionPropMattermostAppID, app.MattermostAppID)
|
||||
@@ -407,10 +407,10 @@ func (a *App) newSessionUpdateToken(app *model.OAuthApp, accessData *model.Acces
|
||||
return nil, model.NewAppError("newSessionUpdateToken", "web.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
accessRsp := &model.AccessResponse{
|
||||
AccessToken: session.Token,
|
||||
RefreshToken: accessData.RefreshToken,
|
||||
TokenType: model.AccessTokenType,
|
||||
ExpiresIn: int32(*a.Config().ServiceSettings.SessionLengthSSOInDays * 60 * 60 * 24),
|
||||
AccessToken: session.Token,
|
||||
RefreshToken: accessData.RefreshToken,
|
||||
TokenType: model.AccessTokenType,
|
||||
ExpiresInSeconds: int32(*a.Config().ServiceSettings.SessionLengthSSOInHours * 60 * 60),
|
||||
}
|
||||
|
||||
return accessRsp, nil
|
||||
|
||||
@@ -79,7 +79,7 @@ func TestOAuthRevokeAccessToken(t *testing.T) {
|
||||
session.UserId = model.NewId()
|
||||
session.Token = model.NewId()
|
||||
session.Roles = model.SystemUserRoleId
|
||||
th.App.SetSessionExpireInDays(session, 1)
|
||||
th.App.SetSessionExpireInHours(session, 24)
|
||||
|
||||
var err *model.AppError
|
||||
session, err = th.App.CreateSession(session)
|
||||
@@ -111,7 +111,7 @@ func TestOAuthDeleteApp(t *testing.T) {
|
||||
session.Token = model.NewId()
|
||||
session.Roles = model.SystemUserRoleId
|
||||
session.IsOAuth = true
|
||||
th.App.ch.srv.userService.SetSessionExpireInDays(session, 1)
|
||||
th.App.ch.srv.userService.SetSessionExpireInHours(session, 24)
|
||||
|
||||
session, _ = th.App.CreateSession(session)
|
||||
|
||||
|
||||
@@ -15309,9 +15309,9 @@ func (a *OpenTracingAppLayer) SetSearchEngine(se *searchengine.Broker) {
|
||||
a.app.SetSearchEngine(se)
|
||||
}
|
||||
|
||||
func (a *OpenTracingAppLayer) SetSessionExpireInDays(session *model.Session, days int) {
|
||||
func (a *OpenTracingAppLayer) SetSessionExpireInHours(session *model.Session, hours int) {
|
||||
origCtx := a.ctx
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SetSessionExpireInDays")
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SetSessionExpireInHours")
|
||||
|
||||
a.ctx = newCtx
|
||||
a.app.Srv().Store.SetContext(newCtx)
|
||||
@@ -15321,7 +15321,7 @@ func (a *OpenTracingAppLayer) SetSessionExpireInDays(session *model.Session, day
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
a.app.SetSessionExpireInDays(session, days)
|
||||
a.app.SetSessionExpireInHours(session, hours)
|
||||
}
|
||||
|
||||
func (a *OpenTracingAppLayer) SetStatusAwayIfNeeded(userID string, manual bool) {
|
||||
|
||||
@@ -307,22 +307,22 @@ func (a *App) GetSessionLengthInMillis(session *model.Session) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
var days int
|
||||
var hours int
|
||||
if session.IsMobileApp() {
|
||||
days = *a.Config().ServiceSettings.SessionLengthMobileInDays
|
||||
hours = *a.Config().ServiceSettings.SessionLengthMobileInHours
|
||||
} else if session.IsSSOLogin() {
|
||||
days = *a.Config().ServiceSettings.SessionLengthSSOInDays
|
||||
hours = *a.Config().ServiceSettings.SessionLengthSSOInHours
|
||||
} else {
|
||||
days = *a.Config().ServiceSettings.SessionLengthWebInDays
|
||||
hours = *a.Config().ServiceSettings.SessionLengthWebInHours
|
||||
}
|
||||
return int64(days * 24 * 60 * 60 * 1000)
|
||||
return int64(hours * 60 * 60 * 1000)
|
||||
}
|
||||
|
||||
// SetSessionExpireInDays sets the session's expiry the specified number of days
|
||||
// SetSessionExpireInHours sets the session's expiry the specified number of hours
|
||||
// relative to either the session creation date or the current time, depending
|
||||
// on the `ExtendSessionOnActivity` config setting.
|
||||
func (a *App) SetSessionExpireInDays(session *model.Session, days int) {
|
||||
a.ch.srv.userService.SetSessionExpireInDays(session, days)
|
||||
func (a *App) SetSessionExpireInHours(session *model.Session, hours int) {
|
||||
a.ch.srv.userService.SetSessionExpireInHours(session, hours)
|
||||
}
|
||||
|
||||
func (a *App) CreateUserAccessToken(token *model.UserAccessToken) (*model.UserAccessToken, *model.AppError) {
|
||||
@@ -411,7 +411,7 @@ func (a *App) createSessionForUserAccessToken(tokenString string) (*model.Sessio
|
||||
} else {
|
||||
session.AddProp(model.SessionPropIsGuest, "false")
|
||||
}
|
||||
a.ch.srv.userService.SetSessionExpireInDays(session, model.SessionUserAccessTokenExpiry)
|
||||
a.ch.srv.userService.SetSessionExpireInHours(session, model.SessionUserAccessTokenExpiryHours)
|
||||
|
||||
session, nErr = a.Srv().Store.Session().Save(session)
|
||||
if nErr != nil {
|
||||
|
||||
@@ -155,9 +155,9 @@ func TestApp_GetSessionLengthInMillis(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthMobileInDays = 3 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthSSOInDays = 2 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthWebInDays = 1 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthMobileInHours = 3 * 24 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthSSOInHours = 2 * 24 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthWebInHours = 24 })
|
||||
|
||||
t.Run("get session length mobile", func(t *testing.T) {
|
||||
session := &model.Session{
|
||||
@@ -244,9 +244,9 @@ func TestApp_ExtendExpiryIfNeeded(t *testing.T) {
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ExtendSessionLengthWithActivity = true })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthMobileInDays = 3 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthSSOInDays = 2 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthWebInDays = 1 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthMobileInHours = 3 * 24 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthSSOInHours = 2 * 24 })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionLengthWebInHours = 24 })
|
||||
|
||||
t.Run("expired session should not be extended", func(t *testing.T) {
|
||||
expires := model.GetMillis() - hourMillis
|
||||
|
||||
@@ -199,14 +199,14 @@ func (us *UserService) RevokeAccessToken(token string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetSessionExpireInDays sets the session's expiry the specified number of days
|
||||
// SetSessionExpireInHours sets the session's expiry the specified number of hours
|
||||
// relative to either the session creation date or the current time, depending
|
||||
// on the `ExtendSessionOnActivity` config setting.
|
||||
func (us *UserService) SetSessionExpireInDays(session *model.Session, days int) {
|
||||
func (us *UserService) SetSessionExpireInHours(session *model.Session, hours int) {
|
||||
if session.CreateAt == 0 || *us.config().ServiceSettings.ExtendSessionLengthWithActivity {
|
||||
session.ExpiresAt = model.GetMillis() + (1000 * 60 * 60 * 24 * int64(days))
|
||||
session.ExpiresAt = model.GetMillis() + (1000 * 60 * 60 * int64(hours))
|
||||
} else {
|
||||
session.ExpiresAt = session.CreateAt + (1000 * 60 * 60 * 24 * int64(days))
|
||||
session.ExpiresAt = session.CreateAt + (1000 * 60 * 60 * int64(hours))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestCache(t *testing.T) {
|
||||
require.Empty(t, rkeys)
|
||||
}
|
||||
|
||||
func TestSetSessionExpireInDays(t *testing.T) {
|
||||
func TestSetSessionExpireInHours(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
@@ -91,7 +91,7 @@ func TestSetSessionExpireInDays(t *testing.T) {
|
||||
CreateAt: create,
|
||||
ExpiresAt: model.GetMillis() + dayInMillis,
|
||||
}
|
||||
th.service.SetSessionExpireInDays(session, tt.days)
|
||||
th.service.SetSessionExpireInHours(session, tt.days*24)
|
||||
|
||||
// must be within 5 seconds of expected time.
|
||||
require.GreaterOrEqual(t, session.ExpiresAt, tt.want-grace)
|
||||
@@ -112,7 +112,7 @@ func TestOAuthRevokeAccessToken(t *testing.T) {
|
||||
session.UserId = model.NewId()
|
||||
session.Token = model.NewId()
|
||||
session.Roles = model.SystemUserRoleId
|
||||
th.service.SetSessionExpireInDays(session, 1)
|
||||
th.service.SetSessionExpireInHours(session, 24)
|
||||
|
||||
session, _ = th.service.CreateSession(session)
|
||||
err = th.service.RevokeAccessToken(session.Token)
|
||||
|
||||
Ссылка в новой задаче
Block a user