session.go 2.48 KB
Newer Older
1
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
=Corey Hulen's avatar
=Corey Hulen committed
2 3 4 5 6 7 8 9 10 11
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
)

const (
=Corey Hulen's avatar
=Corey Hulen committed
12
	SESSION_COOKIE_TOKEN        = "MMTOKEN"
13
	SESSION_TIME_WEB_IN_DAYS    = 30
=Corey Hulen's avatar
=Corey Hulen committed
14
	SESSION_TIME_WEB_IN_SECS    = 60 * 60 * 24 * SESSION_TIME_WEB_IN_DAYS
15
	SESSION_TIME_MOBILE_IN_DAYS = 30
=Corey Hulen's avatar
=Corey Hulen committed
16
	SESSION_TIME_MOBILE_IN_SECS = 60 * 60 * 24 * SESSION_TIME_MOBILE_IN_DAYS
17 18
	SESSION_TIME_OAUTH_IN_DAYS  = 365
	SESSION_TIME_OAUTH_IN_SECS  = 60 * 60 * 24 * SESSION_TIME_OAUTH_IN_DAYS
=Corey Hulen's avatar
=Corey Hulen committed
19 20 21 22 23 24 25 26 27
	SESSION_CACHE_IN_SECS       = 60 * 10
	SESSION_CACHE_SIZE          = 10000
	SESSION_PROP_PLATFORM       = "platform"
	SESSION_PROP_OS             = "os"
	SESSION_PROP_BROWSER        = "browser"
)

type Session struct {
	Id             string    `json:"id"`
28
	Token          string    `json:"token"`
=Corey Hulen's avatar
=Corey Hulen committed
29 30 31 32 33 34 35
	CreateAt       int64     `json:"create_at"`
	ExpiresAt      int64     `json:"expires_at"`
	LastActivityAt int64     `json:"last_activity_at"`
	UserId         string    `json:"user_id"`
	TeamId         string    `json:"team_id"`
	DeviceId       string    `json:"device_id"`
	Roles          string    `json:"roles"`
36
	IsOAuth        bool      `json:"is_oauth"`
=Corey Hulen's avatar
=Corey Hulen committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	Props          StringMap `json:"props"`
}

func (me *Session) ToJson() string {
	b, err := json.Marshal(me)
	if err != nil {
		return ""
	} else {
		return string(b)
	}
}

func SessionFromJson(data io.Reader) *Session {
	decoder := json.NewDecoder(data)
	var me Session
	err := decoder.Decode(&me)
	if err == nil {
		return &me
	} else {
		return nil
	}
}

func (me *Session) PreSave() {
	if me.Id == "" {
		me.Id = NewId()
	}

65
	me.Token = NewId()
=Corey Hulen's avatar
=Corey Hulen committed
66 67 68 69 70 71 72 73 74 75

	me.CreateAt = GetMillis()
	me.LastActivityAt = me.CreateAt

	if me.Props == nil {
		me.Props = make(map[string]string)
	}
}

func (me *Session) Sanitize() {
76
	me.Token = ""
=Corey Hulen's avatar
=Corey Hulen committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
}

func (me *Session) IsExpired() bool {

	if me.ExpiresAt <= 0 {
		return false
	}

	if GetMillis() > me.ExpiresAt {
		return true
	}

	return false
}

func (me *Session) SetExpireInDays(days int64) {
	me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * days)
}

func (me *Session) AddProp(key string, value string) {

	if me.Props == nil {
		me.Props = make(map[string]string)
	}

	me.Props[key] = value
}

func SessionsToJson(o []*Session) string {
	if b, err := json.Marshal(o); err != nil {
		return "[]"
	} else {
		return string(b)
	}
}

func SessionsFromJson(data io.Reader) []*Session {
	decoder := json.NewDecoder(data)
	var o []*Session
	err := decoder.Decode(&o)
	if err == nil {
		return o
	} else {
		return nil
	}
}