Mercurial > gemma
annotate pkg/auth/store.go @ 1321:8d9754007089
Used keyed initializer to make 'go vet' happy.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sat, 24 Nov 2018 16:40:34 +0100 |
parents | a244b18cb916 |
children | 20b9c3f261db |
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 |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
25 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
|
26 |
493
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
27 // 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
|
28 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
|
29 |
493
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
30 type SessionStore struct { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
31 storage *bolt.DB |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
32 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
|
33 cmds chan func() |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
36 var sessionsBucket = []byte("sessions") |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
37 |
493
8a0737aa6ab6
The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
486
diff
changeset
|
38 func NewSessionStore(filename string) (*SessionStore, error) { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
39 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
40 ss := &SessionStore{ |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
41 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
|
42 cmds: make(chan func()), |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
43 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
44 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
|
45 return nil, err |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
46 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
47 go ss.run() |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
48 return ss, nil |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
49 } |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
50 |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
51 // 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
|
52 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
|
53 |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
54 // No file, nothing to restore/persist. |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
55 if filename == "" { |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
56 return nil |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
57 } |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
59 db, err := bolt.Open(filename, 0600, nil) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
60 if err != nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
61 return err |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
62 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
63 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
64 err = db.Update(func(tx *bolt.Tx) error { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
65 b, err := tx.CreateBucketIfNotExists(sessionsBucket) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
66 if err != nil { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
67 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
68 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
69 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
70 // pre-load sessions |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
71 c := b.Cursor() |
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 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
|
74 var session Session |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
75 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
|
76 return err |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
77 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
78 ss.sessions[string(k)] = &session |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
79 } |
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 return nil |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
82 }) |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
83 |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
84 if err != nil { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
85 db.Close() |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
86 return err |
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 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
89 ss.storage = db |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
90 return nil |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
93 func (ss *SessionStore) run() { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
94 for { |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
95 select { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
96 case cmd := <-ss.cmds: |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
97 cmd() |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
98 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
|
99 ss.cleanToken() |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
100 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
101 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
102 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
103 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
104 func (ss *SessionStore) cleanToken() { |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
105 now := time.Now() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
106 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
|
107 expires := time.Unix(session.ExpiresAt, 0) |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
108 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
|
109 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
110 ss.remove(token) |
203
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
111 } |
6a802aed7f99
Persistent session store: Implemented cleanDB/cleanToken.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
202
diff
changeset
|
112 } |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
113 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
114 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
115 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
|
116 if ss.storage == nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
117 return |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
118 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
119 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
|
120 b := tx.Bucket(sessionsBucket) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
121 return b.Delete([]byte(token)) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
122 }) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
123 if err != nil { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
124 log.Printf("error: %v\n", err) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
125 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
126 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
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 func (ss *SessionStore) Delete(token string) bool { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
129 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
|
130 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
131 if _, found := ss.sessions[token]; !found { |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
132 res <- false |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
133 return |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
134 } |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
135 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
136 ss.remove(token) |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
137 res <- true |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
138 } |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
139 return <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
142 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
|
143 if ss.storage == nil { |
204
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
diff
changeset
|
144 return |
3d0988d9f867
De-virtualize the connection pool implementation.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
203
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 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
|
147 b := tx.Bucket(sessionsBucket) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
148 var buf bytes.Buffer |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
149 if err := session.serialize(&buf); err != nil { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
150 return err |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
151 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
152 return b.Put([]byte(token), buf.Bytes()) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
153 }) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
154 if err != nil { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
155 log.Printf("error: %v\n", err) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
156 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
157 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
158 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
159 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
|
160 res := make(chan struct{}) |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
161 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
162 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
163 defer close(res) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
164 s := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
165 if s == nil { |
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
166 s = session |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
167 ss.sessions[token] = session |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
168 } |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
169 s.touch() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
170 ss.store(token, s) |
199
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
171 } |
ddc7ef95c247
Implemented Add of persistent sessions.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
197
diff
changeset
|
172 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
173 <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
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 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
|
177 |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
178 type result struct { |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
179 newToken string |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
180 err error |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
181 } |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
182 |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
183 resCh := make(chan result) |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
184 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
185 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
186 session := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
187 if session == nil { |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
188 resCh <- result{err: ErrNoSuchToken} |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
189 } else { |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
190 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
191 ss.remove(token) |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
192 newToken := GenerateSessionKey() |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
193 // 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
|
194 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
|
195 ss.sessions[newToken] = session |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
196 ss.store(newToken, session) |
200
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
197 resCh <- result{newToken: newToken} |
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 |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
201 r := <-resCh |
8426a92fda00
Persistent session store: Implmented Renew.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
199
diff
changeset
|
202 return r.newToken, r.err |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
203 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
204 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
205 func (ss *SessionStore) Session(token string) *Session { |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
206 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
|
207 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
208 session := ss.sessions[token] |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
209 if session == nil { |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
210 res <- nil |
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
211 } else { |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
212 session.touch() |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
213 ss.store(token, session) |
498
22e1bf563a04
Throw away the connection level for sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
495
diff
changeset
|
214 res <- session |
202
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
215 } |
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
216 } |
dba50c51cda7
Persistent session store: Implemented Session.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
201
diff
changeset
|
217 return <-res |
190
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
218 } |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
219 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
220 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
|
221 ss.cmds <- func() { |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
222 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
|
223 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
|
224 delete(ss.sessions, token) |
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
225 ss.remove(token) |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
226 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
227 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
228 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
229 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
220
diff
changeset
|
230 |
513
b6796cd91604
Simplified the function channel inside the session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
498
diff
changeset
|
231 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
|
232 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
|
233 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
|
234 ss.storage = nil |
193
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
235 return db.Close() |
1585c334e8a7
More on persisting sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
190
diff
changeset
|
236 } |
614
be8b79109679
Fixed textual oversight from the session store refactoring.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
527
diff
changeset
|
237 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
|
238 return nil |
3457a60fb12d
Added stub for a persistent session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
239 } |