Mercurial > gemma
comparison auth/persistent.go @ 200:8426a92fda00
Persistent session store: Implmented Renew.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sun, 22 Jul 2018 09:25:25 +0200 |
parents | ddc7ef95c247 |
children | 80dc7bbe97db |
comparison
equal
deleted
inserted
replaced
199:ddc7ef95c247 | 200:8426a92fda00 |
---|---|
78 | 78 |
79 func (pcp *PersistentConnectionPool) cleanToken() { | 79 func (pcp *PersistentConnectionPool) cleanToken() { |
80 log.Println("cleanToken: Not implemented, yet.") | 80 log.Println("cleanToken: Not implemented, yet.") |
81 } | 81 } |
82 | 82 |
83 func (pcp *PersistentConnectionPool) remove(token string) { | |
84 err := pcp.db.Update(func(tx *bolt.Tx) error { | |
85 b := tx.Bucket(sessionsBucket) | |
86 return b.Delete([]byte(token)) | |
87 }) | |
88 if err != nil { | |
89 log.Printf("error: %v\n", err) | |
90 } | |
91 } | |
92 | |
83 func (pcp *PersistentConnectionPool) Delete(token string) bool { | 93 func (pcp *PersistentConnectionPool) Delete(token string) bool { |
84 res := make(chan bool) | 94 res := make(chan bool) |
85 pcp.cmds <- func(pcp *PersistentConnectionPool) { | 95 pcp.cmds <- func(pcp *PersistentConnectionPool) { |
86 conn, found := pcp.conns[token] | 96 conn, found := pcp.conns[token] |
87 if !found { | 97 if !found { |
88 res <- false | 98 res <- false |
89 return | 99 return |
90 } | 100 } |
91 conn.close() | 101 conn.close() |
92 delete(pcp.conns, token) | 102 delete(pcp.conns, token) |
93 err := pcp.db.Update(func(tx *bolt.Tx) error { | 103 pcp.remove(token) |
94 b := tx.Bucket(sessionsBucket) | |
95 return b.Delete([]byte(token)) | |
96 }) | |
97 if err != nil { | |
98 log.Printf("error: %v\n", err) | |
99 } | |
100 res <- true | 104 res <- true |
101 } | 105 } |
102 return <-res | 106 return <-res |
107 } | |
108 | |
109 func (pcp *PersistentConnectionPool) store(token string, con *Connection) { | |
110 err := pcp.db.Update(func(tx *bolt.Tx) error { | |
111 b := tx.Bucket(sessionsBucket) | |
112 var buf bytes.Buffer | |
113 if err := con.serialize(&buf); err != nil { | |
114 return err | |
115 } | |
116 return b.Put([]byte(token), buf.Bytes()) | |
117 }) | |
118 if err != nil { | |
119 log.Printf("error: %v\n", err) | |
120 } | |
103 } | 121 } |
104 | 122 |
105 func (pcp *PersistentConnectionPool) Add(token string, session *Session) *Connection { | 123 func (pcp *PersistentConnectionPool) Add(token string, session *Session) *Connection { |
106 res := make(chan *Connection) | 124 res := make(chan *Connection) |
107 | 125 |
110 if con == nil { | 128 if con == nil { |
111 con = &Connection{} | 129 con = &Connection{} |
112 pcp.conns[token] = con | 130 pcp.conns[token] = con |
113 } | 131 } |
114 con.set(session) | 132 con.set(session) |
115 err := pcp.db.Update(func(tx *bolt.Tx) error { | 133 pcp.store(token, con) |
116 b := tx.Bucket(sessionsBucket) | |
117 var buf bytes.Buffer | |
118 if err := con.serialize(&buf); err != nil { | |
119 return err | |
120 } | |
121 return b.Put([]byte(token), buf.Bytes()) | |
122 }) | |
123 if err != nil { | |
124 log.Printf("error: %v\n", err) | |
125 } | |
126 res <- con | 134 res <- con |
127 } | 135 } |
128 | 136 |
129 con := <-res | 137 con := <-res |
130 return con | 138 return con |
131 } | 139 } |
132 | 140 |
133 func (pcp *PersistentConnectionPool) Renew(token string) (string, error) { | 141 func (pcp *PersistentConnectionPool) Renew(token string) (string, error) { |
134 log.Println("Renew: Not implemented, yet.") | 142 |
135 return "", nil | 143 type result struct { |
144 newToken string | |
145 err error | |
146 } | |
147 | |
148 resCh := make(chan result) | |
149 | |
150 pcp.cmds <- func(cp *PersistentConnectionPool) { | |
151 con := pcp.conns[token] | |
152 if con == nil { | |
153 resCh <- result{err: ErrNoSuchToken} | |
154 } else { | |
155 delete(pcp.conns, token) | |
156 pcp.remove(token) | |
157 newToken := GenerateSessionKey() | |
158 // TODO: Ensure that this is not racy! | |
159 con.session.ExpiresAt = time.Now().Add(maxTokenValid).Unix() | |
160 pcp.conns[newToken] = con | |
161 pcp.store(newToken, con) | |
162 resCh <- result{newToken: newToken} | |
163 } | |
164 } | |
165 | |
166 r := <-resCh | |
167 return r.newToken, r.err | |
136 } | 168 } |
137 | 169 |
138 func (pcp *PersistentConnectionPool) Do(token string, fn func(*sql.DB) error) error { | 170 func (pcp *PersistentConnectionPool) Do(token string, fn func(*sql.DB) error) error { |
139 log.Println("Do: Not implemented, yet.") | 171 log.Println("Do: Not implemented, yet.") |
140 return nil | 172 return nil |