From 0e2b321e6f5ab5983bc3428aa455dac7012c36ee Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Fri, 13 Jan 2017 15:17:50 -0500 Subject: Refactor and migrate more functions out of api into app package (#5063) --- app/session.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'app/session.go') diff --git a/app/session.go b/app/session.go index 29c961e81..3bb167891 100644 --- a/app/session.go +++ b/app/session.go @@ -92,3 +92,55 @@ func InvalidateAllCaches() { func SessionCacheLength() int { return sessionCache.Len() } + +func RevokeSessionsForDeviceId(userId string, deviceId string, currentSessionId string) *model.AppError { + if result := <-Srv.Store.Session().GetSessions(userId); result.Err != nil { + return result.Err + } else { + sessions := result.Data.([]*model.Session) + for _, session := range sessions { + if session.DeviceId == deviceId && session.Id != currentSessionId { + l4g.Debug(utils.T("api.user.login.revoking.app_error"), session.Id, userId) + if err := RevokeSession(session); err != nil { + // Soft error so we still remove the other sessions + l4g.Error(err.Error()) + } + } + } + } + + return nil +} + +func RevokeSessionById(sessionId string) *model.AppError { + if result := <-Srv.Store.Session().Get(sessionId); result.Err != nil { + return result.Err + } else { + return RevokeSession(result.Data.(*model.Session)) + } +} + +func RevokeSession(session *model.Session) *model.AppError { + if session.IsOAuth { + if err := RevokeAccessToken(session.Token); err != nil { + return err + } + } else { + if result := <-Srv.Store.Session().Remove(session.Id); result.Err != nil { + return result.Err + } + } + + RevokeWebrtcToken(session.Id) + RemoveAllSessionsForUserId(session.UserId) + + return nil +} + +func AttachDeviceId(sessionId string, deviceId string, expiresAt int64) *model.AppError { + if result := <-Srv.Store.Session().UpdateDeviceId(sessionId, deviceId, expiresAt); result.Err != nil { + return result.Err + } + + return nil +} -- cgit v1.2.3-1-g7c22