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