Mercurial > gemma
annotate pkg/auth/store.go @ 1921:0059aa870a39 v2
Hotfixed stretch import.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Fri, 18 Jan 2019 20:26:21 +0100 |
parents | 9e0beb373690 |
children | 0db742c7813d |
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 |
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
|
22 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
|
23 ) |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
25 // 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
|
26 // 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
|
27 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
|
28 |
493
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
29 // 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
|
30 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
|
31 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
32 // 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
|
33 type SessionStore struct { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
34 storage *bolt.DB |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
35 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
|
36 cmds chan func() |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
39 var sessionsBucket = []byte("sessions") |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
40 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
41 // 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
|
42 // 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
|
43 // 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
|
44 // 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
|
45 // 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
|
46 func NewSessionStore(filename string) (*SessionStore, error) { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
47 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
48 ss := &SessionStore{ |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
49 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
|
50 cmds: make(chan func()), |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
51 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
52 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
|
53 return nil, err |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
54 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
55 go ss.run() |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
56 return ss, nil |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
57 } |
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 // 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
|
60 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
|
61 |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
62 // No file, nothing to restore/persist. |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
63 if filename == "" { |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
64 return nil |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
65 } |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
67 db, err := bolt.Open(filename, 0600, nil) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
68 if err != nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
69 return err |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
70 } |
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 err = db.Update(func(tx *bolt.Tx) error { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
73 b, err := tx.CreateBucketIfNotExists(sessionsBucket) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
74 if err != nil { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
75 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
76 } |
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 // pre-load sessions |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
79 c := b.Cursor() |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
80 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
81 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
|
82 var session Session |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
83 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
|
84 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
85 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
86 ss.sessions[string(k)] = &session |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
87 } |
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 return nil |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
90 }) |
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 if err != nil { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
93 db.Close() |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
94 return err |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
95 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
96 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
97 ss.storage = db |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
98 return nil |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
101 func (ss *SessionStore) run() { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
102 for { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
103 select { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
104 case cmd := <-ss.cmds: |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
105 cmd() |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
106 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
|
107 ss.cleanToken() |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
108 } |
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 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
112 func (ss *SessionStore) cleanToken() { |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
113 now := time.Now() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
114 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
|
115 expires := time.Unix(session.ExpiresAt, 0) |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
116 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
|
117 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
118 ss.remove(token) |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
119 } |
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
120 } |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
121 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
122 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
123 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
|
124 if ss.storage == nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
125 return |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
126 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
127 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
|
128 b := tx.Bucket(sessionsBucket) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
129 return b.Delete([]byte(token)) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
130 }) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
131 if err != nil { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
132 log.Printf("error: %v\n", err) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
133 } |
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 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
136 // 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
|
137 // 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
|
138 func (ss *SessionStore) Delete(token string) bool { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
139 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
|
140 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
141 if _, found := ss.sessions[token]; !found { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
142 res <- false |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
143 return |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
144 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
145 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
146 ss.remove(token) |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
147 res <- true |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
148 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
149 return <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
152 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
|
153 if ss.storage == nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
154 return |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
155 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
156 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
|
157 b := tx.Bucket(sessionsBucket) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
158 var buf bytes.Buffer |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
159 if err := session.serialize(&buf); err != nil { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
160 return err |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
161 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
162 return b.Put([]byte(token), buf.Bytes()) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
163 }) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
164 if err != nil { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
165 log.Printf("error: %v\n", err) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
166 } |
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 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
169 // 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
|
170 // 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
|
171 // be replaced. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
172 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
|
173 res := make(chan struct{}) |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
174 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
175 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
176 defer close(res) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
177 s := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
178 if s == nil { |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
179 s = session |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
180 ss.sessions[token] = session |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
181 } |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
182 s.touch() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
183 ss.store(token, s) |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
184 } |
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
185 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
186 <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
187 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
188 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
189 // 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
|
190 // 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
|
191 // freshed up one. |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
192 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
|
193 |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
194 type result struct { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
195 newToken string |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
196 err error |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
197 } |
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 resCh := make(chan result) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
200 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
201 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
202 session := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
203 if session == nil { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
204 resCh <- result{err: ErrNoSuchToken} |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
205 } else { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
206 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
207 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
|
208 newToken := generateSessionKey() |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
209 // TODO: Ensure that this is not racy! |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
210 session.ExpiresAt = time.Now().Add(maxTokenValid).Unix() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
211 ss.sessions[newToken] = session |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
212 ss.store(newToken, session) |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
213 resCh <- result{newToken: newToken} |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
214 } |
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 r := <-resCh |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
218 return r.newToken, r.err |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
219 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
220 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
221 // 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
|
222 // 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
|
223 func (ss *SessionStore) Session(token string) *Session { |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
224 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
|
225 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
226 session := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
227 if session == nil { |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
228 res <- nil |
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
229 } else { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
230 session.touch() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
231 ss.store(token, session) |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
232 res <- session |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
233 } |
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 return <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
236 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
237 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
238 // 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
|
239 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
|
240 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
241 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
|
242 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
|
243 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
244 ss.remove(token) |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
245 } |
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 |
1343
9e0beb373690
Added comments how to use the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1342
diff
changeset
|
250 // 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
|
251 // 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
|
252 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
|
253 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
|
254 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
|
255 ss.storage = nil |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
256 return db.Close() |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
257 } |
614
be8b79109679
Fixed textual oversight from the session store refactoring.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
527
diff
changeset
|
258 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
|
259 return nil |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
260 } |