github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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_COOKIE_CSRF               = "MMCSRF"
    16  	SESSION_CACHE_SIZE                = 35000
    17  	SESSION_PROP_PLATFORM             = "platform"
    18  	SESSION_PROP_OS                   = "os"
    19  	SESSION_PROP_BROWSER              = "browser"
    20  	SESSION_PROP_TYPE                 = "type"
    21  	SESSION_PROP_USER_ACCESS_TOKEN_ID = "user_access_token_id"
    22  	SESSION_PROP_IS_BOT               = "is_bot"
    23  	SESSION_PROP_IS_BOT_VALUE         = "true"
    24  	SESSION_TYPE_USER_ACCESS_TOKEN    = "UserAccessToken"
    25  	SESSION_PROP_IS_GUEST             = "is_guest"
    26  	SESSION_ACTIVITY_TIMEOUT          = 1000 * 60 * 5 // 5 minutes
    27  	SESSION_USER_ACCESS_TOKEN_EXPIRY  = 100 * 365     // 100 years
    28  )
    29  
    30  type Session struct {
    31  	Id             string          `json:"id"`
    32  	Token          string          `json:"token"`
    33  	CreateAt       int64           `json:"create_at"`
    34  	ExpiresAt      int64           `json:"expires_at"`
    35  	LastActivityAt int64           `json:"last_activity_at"`
    36  	UserId         string          `json:"user_id"`
    37  	DeviceId       string          `json:"device_id"`
    38  	Roles          string          `json:"roles"`
    39  	IsOAuth        bool            `json:"is_oauth"`
    40  	Props          StringMap       `json:"props"`
    41  	BranchMembers  []*BranchMember `json:"branch_members" db:"-"`
    42  }
    43  
    44  func (me *Session) DeepCopy() *Session {
    45  	copySession := *me
    46  
    47  	if me.Props != nil {
    48  		copySession.Props = CopyStringMap(me.Props)
    49  	}
    50  
    51  	if me.BranchMembers != nil {
    52  		copySession.BranchMembers = make([]*BranchMember, len(me.BranchMembers))
    53  		for index, tm := range me.BranchMembers {
    54  			copySession.BranchMembers[index] = new(BranchMember)
    55  			*copySession.BranchMembers[index] = *tm
    56  		}
    57  	}
    58  
    59  	return &copySession
    60  }
    61  
    62  func (me *Session) ToJson() string {
    63  	b, _ := json.Marshal(me)
    64  	return string(b)
    65  }
    66  
    67  func SessionFromJson(data io.Reader) *Session {
    68  	var me *Session
    69  	json.NewDecoder(data).Decode(&me)
    70  	return me
    71  }
    72  
    73  func (me *Session) PreSave() {
    74  	if me.Id == "" {
    75  		me.Id = NewId()
    76  	}
    77  
    78  	if me.Token == "" {
    79  		me.Token = NewId()
    80  	}
    81  
    82  	me.CreateAt = GetMillis()
    83  	me.LastActivityAt = me.CreateAt
    84  
    85  	if me.Props == nil {
    86  		me.Props = make(map[string]string)
    87  	}
    88  }
    89  
    90  func (me *Session) Sanitize() {
    91  	me.Token = ""
    92  }
    93  
    94  func (me *Session) IsExpired() bool {
    95  
    96  	if me.ExpiresAt <= 0 {
    97  		return false
    98  	}
    99  
   100  	if GetMillis() > me.ExpiresAt {
   101  		return true
   102  	}
   103  
   104  	return false
   105  }
   106  
   107  func (me *Session) SetExpireInDays(days int) {
   108  	if me.CreateAt == 0 {
   109  		me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * int64(days))
   110  	} else {
   111  		me.ExpiresAt = me.CreateAt + (1000 * 60 * 60 * 24 * int64(days))
   112  	}
   113  }
   114  
   115  func (me *Session) AddProp(key string, value string) {
   116  
   117  	if me.Props == nil {
   118  		me.Props = make(map[string]string)
   119  	}
   120  
   121  	me.Props[key] = value
   122  }
   123  
   124  func (me *Session) GetBranchByBranchId(branchId string) *BranchMember {
   125  	for _, branch := range me.BranchMembers {
   126  		if branch.BranchId == branchId {
   127  			return branch
   128  		}
   129  	}
   130  	return nil
   131  }
   132  
   133  func (me *Session) IsMobileApp() bool {
   134  	return len(me.DeviceId) > 0
   135  }
   136  
   137  func (me *Session) GetUserRoles() []string {
   138  	return strings.Fields(me.Roles)
   139  }
   140  
   141  func (me *Session) GenerateCSRF() string {
   142  	token := NewId()
   143  	me.AddProp("csrf", token)
   144  	return token
   145  }
   146  
   147  func (me *Session) GetCSRF() string {
   148  	if me.Props == nil {
   149  		return ""
   150  	}
   151  
   152  	return me.Props["csrf"]
   153  }
   154  
   155  func SessionsToJson(o []*Session) string {
   156  	if b, err := json.Marshal(o); err != nil {
   157  		return "[]"
   158  	} else {
   159  		return string(b)
   160  	}
   161  }
   162  
   163  func SessionsFromJson(data io.Reader) []*Session {
   164  	var o []*Session
   165  	json.NewDecoder(data).Decode(&o)
   166  	return o
   167  }