5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-10 13:20:27 +00:00
matterbridge/vendor/github.com/mattermost/mattermost-server/v5/model/session.go

236 lines
5.4 KiB
Go
Raw Normal View History

2017-08-16 21:37:37 +00:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2020-08-09 22:29:54 +00:00
// See LICENSE.txt for license information.
2016-04-10 21:39:38 +00:00
package model
import (
"encoding/json"
"io"
2020-08-09 22:29:54 +00:00
"strconv"
2016-09-17 13:19:18 +00:00
"strings"
2020-08-09 22:29:54 +00:00
"github.com/mattermost/mattermost-server/v5/shared/mlog"
2016-04-10 21:39:38 +00:00
)
const (
2017-08-16 21:37:37 +00:00
SESSION_COOKIE_TOKEN = "MMAUTHTOKEN"
SESSION_COOKIE_USER = "MMUSERID"
2020-08-09 22:29:54 +00:00
SESSION_COOKIE_CSRF = "MMCSRF"
2017-08-16 21:37:37 +00:00
SESSION_CACHE_SIZE = 35000
SESSION_PROP_PLATFORM = "platform"
SESSION_PROP_OS = "os"
SESSION_PROP_BROWSER = "browser"
SESSION_PROP_TYPE = "type"
SESSION_PROP_USER_ACCESS_TOKEN_ID = "user_access_token_id"
2020-08-09 22:29:54 +00:00
SESSION_PROP_IS_BOT = "is_bot"
SESSION_PROP_IS_BOT_VALUE = "true"
2017-08-16 21:37:37 +00:00
SESSION_TYPE_USER_ACCESS_TOKEN = "UserAccessToken"
SESSION_TYPE_CLOUD_KEY = "CloudKey"
SESSION_TYPE_REMOTECLUSTER_TOKEN = "RemoteClusterToken"
2020-08-09 22:29:54 +00:00
SESSION_PROP_IS_GUEST = "is_guest"
2017-08-16 21:37:37 +00:00
SESSION_ACTIVITY_TIMEOUT = 1000 * 60 * 5 // 5 minutes
SESSION_USER_ACCESS_TOKEN_EXPIRY = 100 * 365 // 100 years
2016-04-10 21:39:38 +00:00
)
//msgp StringMap
type StringMap map[string]string
2020-10-19 21:40:00 +00:00
//msgp:tuple Session
// Session contains the user session details.
// This struct's serializer methods are auto-generated. If a new field is added/removed,
// please run make gen-serialized.
2016-04-10 21:39:38 +00:00
type Session struct {
2016-05-15 21:02:30 +00:00
Id string `json:"id"`
Token string `json:"token"`
CreateAt int64 `json:"create_at"`
ExpiresAt int64 `json:"expires_at"`
LastActivityAt int64 `json:"last_activity_at"`
UserId string `json:"user_id"`
DeviceId string `json:"device_id"`
Roles string `json:"roles"`
IsOAuth bool `json:"is_oauth"`
2020-10-19 21:40:00 +00:00
ExpiredNotify bool `json:"expired_notify"`
2016-05-15 21:02:30 +00:00
Props StringMap `json:"props"`
TeamMembers []*TeamMember `json:"team_members" db:"-"`
2020-08-09 22:29:54 +00:00
Local bool `json:"local" db:"-"`
}
// Returns true if the session is unrestricted, which should grant it
// with all permissions. This is used for local mode sessions
func (s *Session) IsUnrestricted() bool {
return s.Local
2016-04-10 21:39:38 +00:00
}
func (s *Session) DeepCopy() *Session {
copySession := *s
if s.Props != nil {
copySession.Props = CopyStringMap(s.Props)
}
if s.TeamMembers != nil {
copySession.TeamMembers = make([]*TeamMember, len(s.TeamMembers))
for index, tm := range s.TeamMembers {
copySession.TeamMembers[index] = new(TeamMember)
*copySession.TeamMembers[index] = *tm
}
}
return &copySession
2018-02-08 23:11:04 +00:00
}
func (s *Session) ToJson() string {
b, _ := json.Marshal(s)
return string(b)
2016-04-10 21:39:38 +00:00
}
func SessionFromJson(data io.Reader) *Session {
var s *Session
json.NewDecoder(data).Decode(&s)
return s
2016-04-10 21:39:38 +00:00
}
func (s *Session) PreSave() {
if s.Id == "" {
s.Id = NewId()
2016-04-10 21:39:38 +00:00
}
if s.Token == "" {
s.Token = NewId()
2017-08-16 21:37:37 +00:00
}
2016-04-10 21:39:38 +00:00
s.CreateAt = GetMillis()
s.LastActivityAt = s.CreateAt
2016-04-10 21:39:38 +00:00
if s.Props == nil {
s.Props = make(map[string]string)
2016-04-10 21:39:38 +00:00
}
}
func (s *Session) Sanitize() {
s.Token = ""
2016-04-10 21:39:38 +00:00
}
func (s *Session) IsExpired() bool {
2016-04-10 21:39:38 +00:00
if s.ExpiresAt <= 0 {
2016-04-10 21:39:38 +00:00
return false
}
if GetMillis() > s.ExpiresAt {
2016-04-10 21:39:38 +00:00
return true
}
return false
}
2020-10-19 21:40:00 +00:00
// Deprecated: SetExpireInDays is deprecated and should not be used.
// Use (*App).SetSessionExpireInDays instead which handles the
// cases where the new ExpiresAt is not relative to CreateAt.
func (s *Session) SetExpireInDays(days int) {
if s.CreateAt == 0 {
s.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * int64(days))
2016-08-15 16:47:31 +00:00
} else {
s.ExpiresAt = s.CreateAt + (1000 * 60 * 60 * 24 * int64(days))
2016-08-15 16:47:31 +00:00
}
2016-04-10 21:39:38 +00:00
}
func (s *Session) AddProp(key string, value string) {
2016-04-10 21:39:38 +00:00
if s.Props == nil {
s.Props = make(map[string]string)
2016-04-10 21:39:38 +00:00
}
s.Props[key] = value
2016-04-10 21:39:38 +00:00
}
func (s *Session) GetTeamByTeamId(teamId string) *TeamMember {
for _, team := range s.TeamMembers {
2016-05-15 21:02:30 +00:00
if team.TeamId == teamId {
return team
}
}
return nil
}
func (s *Session) IsMobileApp() bool {
return s.DeviceId != "" || s.IsMobile()
2020-08-09 22:29:54 +00:00
}
func (s *Session) IsMobile() bool {
val, ok := s.Props[USER_AUTH_SERVICE_IS_MOBILE]
2020-08-09 22:29:54 +00:00
if !ok {
return false
}
isMobile, err := strconv.ParseBool(val)
if err != nil {
mlog.Debug("Error parsing boolean property from Session", mlog.Err(err))
2020-08-09 22:29:54 +00:00
return false
}
return isMobile
}
func (s *Session) IsSaml() bool {
val, ok := s.Props[USER_AUTH_SERVICE_IS_SAML]
2020-08-09 22:29:54 +00:00
if !ok {
return false
}
isSaml, err := strconv.ParseBool(val)
if err != nil {
mlog.Debug("Error parsing boolean property from Session", mlog.Err(err))
2020-08-09 22:29:54 +00:00
return false
}
return isSaml
}
func (s *Session) IsOAuthUser() bool {
val, ok := s.Props[USER_AUTH_SERVICE_IS_OAUTH]
2020-10-19 21:40:00 +00:00
if !ok {
return false
}
isOAuthUser, err := strconv.ParseBool(val)
if err != nil {
mlog.Debug("Error parsing boolean property from Session", mlog.Err(err))
2020-10-19 21:40:00 +00:00
return false
}
return isOAuthUser
}
func (s *Session) IsSSOLogin() bool {
return s.IsOAuthUser() || s.IsSaml()
2016-09-17 13:19:18 +00:00
}
func (s *Session) GetUserRoles() []string {
return strings.Fields(s.Roles)
2016-11-12 21:00:53 +00:00
}
func (s *Session) GenerateCSRF() string {
token := NewId()
s.AddProp("csrf", token)
return token
}
func (s *Session) GetCSRF() string {
if s.Props == nil {
return ""
}
return s.Props["csrf"]
}
2016-04-10 21:39:38 +00:00
func SessionsToJson(o []*Session) string {
b, err := json.Marshal(o)
if err != nil {
2016-04-10 21:39:38 +00:00
return "[]"
}
return string(b)
2016-04-10 21:39:38 +00:00
}
func SessionsFromJson(data io.Reader) []*Session {
var o []*Session
json.NewDecoder(data).Decode(&o)
return o
2016-04-10 21:39:38 +00:00
}