github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/session.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io" 9 "strings" 10 ) 11 12 const ( 13 SESSION_COOKIE_TOKEN = "MMAUTHTOKEN" 14 SESSION_COOKIE_USER = "MMUSERID" 15 SESSION_CACHE_SIZE = 35000 16 SESSION_PROP_PLATFORM = "platform" 17 SESSION_PROP_OS = "os" 18 SESSION_PROP_BROWSER = "browser" 19 SESSION_PROP_TYPE = "type" 20 SESSION_PROP_USER_ACCESS_TOKEN_ID = "user_access_token_id" 21 SESSION_TYPE_USER_ACCESS_TOKEN = "UserAccessToken" 22 SESSION_ACTIVITY_TIMEOUT = 1000 * 60 * 5 // 5 minutes 23 SESSION_USER_ACCESS_TOKEN_EXPIRY = 100 * 365 // 100 years 24 ) 25 26 type Session struct { 27 Id string `json:"id"` 28 Token string `json:"token"` 29 CreateAt int64 `json:"create_at"` 30 ExpiresAt int64 `json:"expires_at"` 31 LastActivityAt int64 `json:"last_activity_at"` 32 UserId string `json:"user_id"` 33 DeviceId string `json:"device_id"` 34 Roles string `json:"roles"` 35 IsOAuth bool `json:"is_oauth"` 36 Props StringMap `json:"props"` 37 TeamMembers []*TeamMember `json:"team_members" db:"-"` 38 } 39 40 func (me *Session) DeepCopy() *Session { 41 copySession := *me 42 43 if me.Props != nil { 44 copySession.Props = CopyStringMap(me.Props) 45 } 46 47 if me.TeamMembers != nil { 48 copySession.TeamMembers = make([]*TeamMember, len(me.TeamMembers)) 49 for index, tm := range me.TeamMembers { 50 copySession.TeamMembers[index] = new(TeamMember) 51 *copySession.TeamMembers[index] = *tm 52 } 53 } 54 55 return ©Session 56 } 57 58 func (me *Session) ToJson() string { 59 b, _ := json.Marshal(me) 60 return string(b) 61 } 62 63 func SessionFromJson(data io.Reader) *Session { 64 var me *Session 65 json.NewDecoder(data).Decode(&me) 66 return me 67 } 68 69 func (me *Session) PreSave() { 70 if me.Id == "" { 71 me.Id = NewId() 72 } 73 74 if me.Token == "" { 75 me.Token = NewId() 76 } 77 78 me.CreateAt = GetMillis() 79 me.LastActivityAt = me.CreateAt 80 81 if me.Props == nil { 82 me.Props = make(map[string]string) 83 } 84 } 85 86 func (me *Session) Sanitize() { 87 me.Token = "" 88 } 89 90 func (me *Session) IsExpired() bool { 91 92 if me.ExpiresAt <= 0 { 93 return false 94 } 95 96 if GetMillis() > me.ExpiresAt { 97 return true 98 } 99 100 return false 101 } 102 103 func (me *Session) SetExpireInDays(days int) { 104 if me.CreateAt == 0 { 105 me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * int64(days)) 106 } else { 107 me.ExpiresAt = me.CreateAt + (1000 * 60 * 60 * 24 * int64(days)) 108 } 109 } 110 111 func (me *Session) AddProp(key string, value string) { 112 113 if me.Props == nil { 114 me.Props = make(map[string]string) 115 } 116 117 me.Props[key] = value 118 } 119 120 func (me *Session) GetTeamByTeamId(teamId string) *TeamMember { 121 for _, team := range me.TeamMembers { 122 if team.TeamId == teamId { 123 return team 124 } 125 } 126 127 return nil 128 } 129 130 func (me *Session) IsMobileApp() bool { 131 return len(me.DeviceId) > 0 132 } 133 134 func (me *Session) GetUserRoles() []string { 135 return strings.Fields(me.Roles) 136 } 137 138 func SessionsToJson(o []*Session) string { 139 if b, err := json.Marshal(o); err != nil { 140 return "[]" 141 } else { 142 return string(b) 143 } 144 } 145 146 func SessionsFromJson(data io.Reader) []*Session { 147 var o []*Session 148 json.NewDecoder(data).Decode(&o) 149 return o 150 }