Mercurial > gemma
annotate pkg/auth/store.go @ 4087:1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Due to limitation of JSON we encode these values as 0 and +/- MaxFloat64.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 25 Jul 2019 17:15:00 +0200 |
parents | 0db742c7813d |
children | 7cccf7fef3e8 |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
614
diff
changeset
|
13 |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package auth |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
17 "bytes" |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
18 "errors" |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "log" |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
20 "time" |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
21 |
2639
0db742c7813d
Make session timeout configurable
Tom Gottfried <tom@intevation.de>
parents:
1343
diff
changeset
|
22 "gemma.intevation.de/gemma/pkg/config" |
527
4a637b333417
The CoreOS guys moved bbolt from github.com/coreos/ to github.com/etcd-io/.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
513
diff
changeset
|
23 bolt "github.com/etcd-io/bbolt" |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 ) |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
26 // ErrNoSuchToken is returned if a given token does not |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
27 // exists th the session store. |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
28 var ErrNoSuchToken = errors.New("No such token") |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
29 |
493
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
30 // Sessions is the global connection pool. |
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
31 var Sessions *SessionStore |
220
11d1a488b08f
Depend command line parsing on Viper/Cobra. Configuration should be persistent now.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
213
diff
changeset
|
32 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
33 // SessionStore encapsulates a set of currently active sessions. |
493
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
34 type SessionStore struct { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
35 storage *bolt.DB |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
36 sessions map[string]*Session |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
37 cmds chan func() |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
40 var sessionsBucket = []byte("sessions") |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
41 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
42 // NewSessionStore creates a new session store. |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
43 // If the filename is empty the session are only hold in memory. |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
44 // If the filename is not empty the sessions are mirrored to |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
45 // a file with this name. Use the later option if you want |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
46 // a persistent session store. |
493
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
47 func NewSessionStore(filename string) (*SessionStore, error) { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
48 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
49 ss := &SessionStore{ |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
50 sessions: make(map[string]*Session), |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
51 cmds: make(chan func()), |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
52 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
53 if err := ss.openStorage(filename); err != nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
54 return nil, err |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
55 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
56 go ss.run() |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
57 return ss, nil |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
58 } |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
59 |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
60 // openStorage opens a storage file. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
61 func (ss *SessionStore) openStorage(filename string) error { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
62 |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
63 // No file, nothing to restore/persist. |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
64 if filename == "" { |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
65 return nil |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
66 } |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
68 db, err := bolt.Open(filename, 0600, nil) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
69 if err != nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
70 return err |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
71 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
72 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
73 err = db.Update(func(tx *bolt.Tx) error { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
74 b, err := tx.CreateBucketIfNotExists(sessionsBucket) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
75 if err != nil { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
76 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
77 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
78 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
79 // pre-load sessions |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
80 c := b.Cursor() |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
81 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
82 for k, v := c.First(); k != nil; k, v = c.Next() { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
83 var session Session |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
84 if err := session.deserialize(bytes.NewReader(v)); err != nil { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
85 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
86 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
87 ss.sessions[string(k)] = &session |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
88 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
89 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
90 return nil |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
91 }) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
92 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
93 if err != nil { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
94 db.Close() |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
95 return err |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
96 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
97 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
98 ss.storage = db |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
99 return nil |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
102 func (ss *SessionStore) run() { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
103 for { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
104 select { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
105 case cmd := <-ss.cmds: |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
106 cmd() |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
107 case <-time.After(time.Minute * 5): |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
108 ss.cleanToken() |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
109 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
110 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
111 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
112 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
113 func (ss *SessionStore) cleanToken() { |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
114 now := time.Now() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
115 for token, session := range ss.sessions { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
116 expires := time.Unix(session.ExpiresAt, 0) |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
117 if expires.Before(now) { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
118 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
119 ss.remove(token) |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
120 } |
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
121 } |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
122 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
123 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
124 func (ss *SessionStore) remove(token string) { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
125 if ss.storage == nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
126 return |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
127 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
128 err := ss.storage.Update(func(tx *bolt.Tx) error { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
129 b := tx.Bucket(sessionsBucket) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
130 return b.Delete([]byte(token)) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
131 }) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
132 if err != nil { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
133 log.Printf("error: %v\n", err) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
134 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
135 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
136 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
137 // Delete removes a session identified by its token from the |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
138 // session store. Returns true if there was such s session. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
139 func (ss *SessionStore) Delete(token string) bool { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
140 res := make(chan bool) |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
141 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
142 if _, found := ss.sessions[token]; !found { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
143 res <- false |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
144 return |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
145 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
146 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
147 ss.remove(token) |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
148 res <- true |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
149 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
150 return <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
153 func (ss *SessionStore) store(token string, session *Session) { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
154 if ss.storage == nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
155 return |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
156 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
157 err := ss.storage.Update(func(tx *bolt.Tx) error { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
158 b := tx.Bucket(sessionsBucket) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
159 var buf bytes.Buffer |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
160 if err := session.serialize(&buf); err != nil { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
161 return err |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
162 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
163 return b.Put([]byte(token), buf.Bytes()) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
164 }) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
165 if err != nil { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
166 log.Printf("error: %v\n", err) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
167 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
168 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
169 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
170 // Add puts a session into the session store identified by |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
171 // a given token. An old session with the same key will |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
172 // be replaced. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
173 func (ss *SessionStore) Add(token string, session *Session) { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
174 res := make(chan struct{}) |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
175 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
176 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
177 defer close(res) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
178 s := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
179 if s == nil { |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
180 s = session |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
181 ss.sessions[token] = session |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
182 } |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
183 s.touch() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
184 ss.store(token, s) |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
185 } |
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
186 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
187 <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
188 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
189 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
190 // Renew refreshes a session. It takes an old token to |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
191 // identify a session and returns a new token with the |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
192 // freshed up one. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
193 func (ss *SessionStore) Renew(token string) (string, error) { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
194 |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
195 type result struct { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
196 newToken string |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
197 err error |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
198 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
199 |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
200 resCh := make(chan result) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
201 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
202 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
203 session := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
204 if session == nil { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
205 resCh <- result{err: ErrNoSuchToken} |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
206 } else { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
207 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
208 ss.remove(token) |
1342
20b9c3f261db
Added comments how to create a new session for a given user and password.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
209 newToken := generateSessionKey() |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
210 // TODO: Ensure that this is not racy! |
2639
0db742c7813d
Make session timeout configurable
Tom Gottfried <tom@intevation.de>
parents:
1343
diff
changeset
|
211 session.ExpiresAt = time.Now().Add(config.SessionTimeout()).Unix() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
212 ss.sessions[newToken] = session |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
213 ss.store(newToken, session) |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
214 resCh <- result{newToken: newToken} |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
215 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
216 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
217 |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
218 r := <-resCh |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
219 return r.newToken, r.err |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
220 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
221 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
222 // Session returns the session associated with given token. |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
223 // Returns nil if no matching session was found. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
224 func (ss *SessionStore) Session(token string) *Session { |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
225 res := make(chan *Session) |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
226 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
227 session := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
228 if session == nil { |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
229 res <- nil |
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
230 } else { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
231 session.touch() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
232 ss.store(token, session) |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
233 res <- session |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
234 } |
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
235 } |
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
236 return <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
237 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
238 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
239 // Logout removes all sessions of a given user from the session store. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
240 func (ss *SessionStore) Logout(user string) { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
241 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
242 for token, session := range ss.sessions { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
243 if session.User == user { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
244 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
245 ss.remove(token) |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
246 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
247 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
248 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
249 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
250 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
251 // Shutdown closes the session store. |
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
252 // If using the persistent mode the backing session database is closed. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
253 func (ss *SessionStore) Shutdown() error { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
254 if db := ss.storage; db != nil { |
614
be8b79109679
Fixed textual oversight from the session store refactoring.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
527
diff
changeset
|
255 log.Println("info: shutdown persistent session store.") |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
256 ss.storage = nil |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
257 return db.Close() |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
258 } |
614
be8b79109679
Fixed textual oversight from the session store refactoring.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
527
diff
changeset
|
259 log.Println("info: shutdown in-memory session store.") |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
260 return nil |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
261 } |