-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
160 lines (125 loc) · 4.38 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
65
66
67
68
69
70
71
72
73
74
75
76
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package http
// sackci
// Copyright (C) 2017 Maximilian Pachl
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------------
// imports
// ----------------------------------------------------------------------------------
import (
"time"
"sync"
"crypto/rand"
"encoding/base64"
"net/http"
"errors"
)
// ----------------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------------
const (
// Length of the session token in bytes.
TOKEN_SIZE = 64
)
var (
ErrExpired = errors.New("session is expired")
ErrNoCookie = errors.New("no session cookie")
ErrInvalidToken = errors.New("invalid session token")
)
// ----------------------------------------------------------------------------------
// types
// ----------------------------------------------------------------------------------
type SessionStore struct {
Sessions map[string]*session
CookieName string
mutex sync.Mutex
}
type session struct {
Creation time.Time
Timeout time.Duration
}
// ----------------------------------------------------------------------------------
// public functions
// ----------------------------------------------------------------------------------
// Creates a new session store.
func NewSessionStore(cookie string) (*SessionStore) {
return &SessionStore{
CookieName: cookie,
Sessions: make(map[string]*session),
}
}
// ----------------------------------------------------------------------------------
// public members
// ----------------------------------------------------------------------------------
// Checks if the given token belongs to a valid session.
func (s *SessionStore) ValidateRequest(r *http.Request) (string, error) {
// get the cookie which contains the session token
cookie, err := r.Cookie(s.CookieName)
if err != nil || cookie == nil {
return "", ErrNoCookie
}
// does the session exist?
session, exists := s.Sessions[cookie.Value]
if !exists {
return cookie.Value, ErrInvalidToken
}
// check if the session is expired
// TODO: maybe as a member of session?
expires := session.Creation.Add(session.Timeout)
if expires.Before(time.Now()) {
return cookie.Value, ErrExpired
}
return cookie.Value, nil
}
// Creates a new session with the given expiration time.
func (s *SessionStore) Create(timeout time.Duration) (string, error) {
token, err := s.newSessionToken()
if err != nil {
return "", err
}
s.mutex.Lock()
defer s.mutex.Unlock()
s.Sessions[token] = &session{
Creation: time.Now(),
Timeout: timeout,
}
return token, nil
}
// Deletes a session form the session store.
func (s *SessionStore) Delete(token string) {
s.mutex.Lock()
defer s.mutex.Unlock()
if _, exists := s.Sessions[token]; exists {
delete(s.Sessions, token)
}
}
// Refreshs the given session.
func (s *SessionStore) Refresh(token string) {
session, exists := s.Sessions[token]
if exists {
session.Creation = time.Now()
}
}
// ----------------------------------------------------------------------------------
// private members
// ----------------------------------------------------------------------------------
// Generates a new session token.
func (s *SessionStore) newSessionToken() (string, error) {
// generate a cryptographically secure random token
b := make([]byte, TOKEN_SIZE)
n, err := rand.Read(b)
if err != nil || n < TOKEN_SIZE {
return "", err
}
// TODO: how to make sure this token is unique?
// TODO: use JWT as token technology?
return base64.StdEncoding.EncodeToString(b), nil
}