My idea is as follows.
When connection from client-side (browser) is requested, server generates random token on websocket upgrade request and sends it via HTTP Set-Cookie header. Then, client authenticates via Challenge-Response authentication over websocket connection. On successful auth, server saves previously generated token in database for user. token cookie is needed to restore session, when user re-initiates websocket connection.
My question is, how can I temporarily store token on memory per connection (per user), so that I can access it later and save in database for that user? (sort of "request context").
you could do something like this
// initialize an empty map
var Clients = make(map[*websocket.Conn]bool)
and then add the connection or token to the map like this
// upgrade connection to websocket and add it to the map
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
return
}
Clients[conn] = true