PLT-3691 Fixing mobile session (#3652)
* PLT-3691 Fixing mobile session * Fixing unit tests
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
bfa04c0ab0
Коммит
946302d9a2
23
api/user.go
23
api/user.go
@@ -684,8 +684,29 @@ func attachDeviceId(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sessionCache.Remove(c.Session.Token)
|
sessionCache.Remove(c.Session.Token)
|
||||||
|
c.Session.SetExpireInDays(*utils.Cfg.ServiceSettings.SessionLengthMobileInDays)
|
||||||
|
|
||||||
if result := <-Srv.Store.Session().UpdateDeviceId(c.Session.Id, deviceId); result.Err != nil {
|
maxAge := *utils.Cfg.ServiceSettings.SessionLengthMobileInDays * 60 * 60 * 24
|
||||||
|
|
||||||
|
secure := false
|
||||||
|
if GetProtocol(r) == "https" {
|
||||||
|
secure = true
|
||||||
|
}
|
||||||
|
|
||||||
|
expiresAt := time.Unix(model.GetMillis()/1000+int64(maxAge), 0)
|
||||||
|
sessionCookie := &http.Cookie{
|
||||||
|
Name: model.SESSION_COOKIE_TOKEN,
|
||||||
|
Value: c.Session.Token,
|
||||||
|
Path: "/",
|
||||||
|
MaxAge: maxAge,
|
||||||
|
Expires: expiresAt,
|
||||||
|
HttpOnly: true,
|
||||||
|
Secure: secure,
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(w, sessionCookie)
|
||||||
|
|
||||||
|
if result := <-Srv.Store.Session().UpdateDeviceId(c.Session.Id, deviceId, c.Session.ExpiresAt); result.Err != nil {
|
||||||
c.Err = result.Err
|
c.Err = result.Err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,7 +83,11 @@ func (me *Session) IsExpired() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (me *Session) SetExpireInDays(days int) {
|
func (me *Session) SetExpireInDays(days int) {
|
||||||
me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * int64(days))
|
if me.CreateAt == 0 {
|
||||||
|
me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * int64(days))
|
||||||
|
} else {
|
||||||
|
me.ExpiresAt = me.CreateAt + (1000 * 60 * 60 * 24 * int64(days))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *Session) AddProp(key string, value string) {
|
func (me *Session) AddProp(key string, value string) {
|
||||||
|
|||||||
@@ -283,12 +283,12 @@ func (me SqlSessionStore) UpdateRoles(userId, roles string) StoreChannel {
|
|||||||
return storeChannel
|
return storeChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me SqlSessionStore) UpdateDeviceId(id, deviceId string) StoreChannel {
|
func (me SqlSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) StoreChannel {
|
||||||
storeChannel := make(StoreChannel)
|
storeChannel := make(StoreChannel)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
result := StoreResult{}
|
result := StoreResult{}
|
||||||
if _, err := me.GetMaster().Exec("UPDATE Sessions SET DeviceId = :DeviceId WHERE Id = :Id", map[string]interface{}{"DeviceId": deviceId, "Id": id}); err != nil {
|
if _, err := me.GetMaster().Exec("UPDATE Sessions SET DeviceId = :DeviceId, ExpiresAt = :ExpiresAt WHERE Id = :Id", map[string]interface{}{"DeviceId": deviceId, "Id": id, "ExpiresAt": expiresAt}); err != nil {
|
||||||
result.Err = model.NewLocAppError("SqlSessionStore.UpdateDeviceId", "store.sql_session.update_device_id.app_error", nil, err.Error())
|
result.Err = model.NewLocAppError("SqlSessionStore.UpdateDeviceId", "store.sql_session.update_device_id.app_error", nil, err.Error())
|
||||||
} else {
|
} else {
|
||||||
result.Data = deviceId
|
result.Data = deviceId
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ func TestSessionUpdateDeviceId(t *testing.T) {
|
|||||||
s1.UserId = model.NewId()
|
s1.UserId = model.NewId()
|
||||||
Must(store.Session().Save(&s1))
|
Must(store.Session().Save(&s1))
|
||||||
|
|
||||||
if rs1 := (<-store.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE+":1234567890")); rs1.Err != nil {
|
if rs1 := (<-store.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)); rs1.Err != nil {
|
||||||
t.Fatal(rs1.Err)
|
t.Fatal(rs1.Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ func TestSessionUpdateDeviceId(t *testing.T) {
|
|||||||
s2.UserId = model.NewId()
|
s2.UserId = model.NewId()
|
||||||
Must(store.Session().Save(&s2))
|
Must(store.Session().Save(&s2))
|
||||||
|
|
||||||
if rs2 := (<-store.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE+":1234567890")); rs2.Err != nil {
|
if rs2 := (<-store.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)); rs2.Err != nil {
|
||||||
t.Fatal(rs2.Err)
|
t.Fatal(rs2.Err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ type SessionStore interface {
|
|||||||
PermanentDeleteSessionsByUser(teamId string) StoreChannel
|
PermanentDeleteSessionsByUser(teamId string) StoreChannel
|
||||||
UpdateLastActivityAt(sessionId string, time int64) StoreChannel
|
UpdateLastActivityAt(sessionId string, time int64) StoreChannel
|
||||||
UpdateRoles(userId string, roles string) StoreChannel
|
UpdateRoles(userId string, roles string) StoreChannel
|
||||||
UpdateDeviceId(id string, deviceId string) StoreChannel
|
UpdateDeviceId(id string, deviceId string, expiresAt int64) StoreChannel
|
||||||
AnalyticsSessionCount() StoreChannel
|
AnalyticsSessionCount() StoreChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user