1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
_ "net/http/pprof"
"github.com/centrifugal/centrifuge"
"github.com/dchest/uniuri"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
// SessionName is the key used to access the session store.
const SessionName = "_session"
// Store can/should be set by applications. The default is a cookie store.
var Store sessions.Store
func init() {
if os.Getenv("SESSION_SECRET") == "" {
log.Fatal("SESSION_SECRET environment variable required")
}
if os.Getenv("GOOGLE_CLIENT_ID") == "" {
log.Fatal("GOOGLE_CLIENT_ID environment variable required")
}
if os.Getenv("GOOGLE_CLIENT_SECRET") == "" {
log.Fatal("GOOGLE_CLIENT_SECRET environment variable required")
}
key := []byte(os.Getenv("SESSION_SECRET"))
cookieStore := sessions.NewCookieStore(key)
cookieStore.Options.HttpOnly = true
cookieStore.Options.MaxAge = 3600
Store = cookieStore
}
var googleOauthConfig = &oauth2.Config{
RedirectURL: "http://localhost:3000/callback",
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email"},
Endpoint: google.Endpoint,
}
// GoogleUser ...
type GoogleUser struct {
ID string `json:"id"`
Email string `json:"email"`
VerifiedEmail bool `json:"verified_email"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Link string `json:"link"`
Picture string `json:"picture"`
Gender string `json:"gender"`
Locale string `json:"locale"`
}
func authMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
value, _ := GetFromSession("user", r)
if value == "" {
http.Redirect(w, r, "/account", http.StatusTemporaryRedirect)
return
}
var user *GoogleUser
err := json.Unmarshal([]byte(value), &user)
if err != nil {
log.Printf("Error unmarshaling Google user %s\n", err.Error())
_ = Logout(w, r)
http.Redirect(w, r, "/account", http.StatusTemporaryRedirect)
return
}
ctx := r.Context()
ctx = centrifuge.SetCredentials(ctx, ¢rifuge.Credentials{
UserID: user.Email,
})
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
})
}
func createCentrifugeNode() (*centrifuge.Node, error) {
node, err := centrifuge.New(centrifuge.DefaultConfig)
if err != nil {
return nil, err
}
node.OnConnect(func(client *centrifuge.Client) {
log.Printf("client %s connected via %s", client.UserID(), client.Transport().Name())
client.OnSubscribe(func(e centrifuge.SubscribeEvent, cb centrifuge.SubscribeCallback) {
log.Printf("client %s subscribes on channel %s", client.UserID(), e.Channel)
cb(centrifuge.SubscribeReply{}, nil)
})
client.OnPublish(func(e centrifuge.PublishEvent, cb centrifuge.PublishCallback) {
log.Printf("client %s publishes into channel %s: %s", client.UserID(), e.Channel, string(e.Data))
cb(centrifuge.PublishReply{}, nil)
})
client.OnDisconnect(func(e centrifuge.DisconnectEvent) {
log.Printf("client %s disconnected", client.UserID())
})
})
if err := node.Run(); err != nil {
return nil, err
}
return node, nil
}
func main() {
node, err := createCentrifugeNode()
if err != nil {
log.Fatal(err)
}
router := mux.NewRouter().StrictSlash(true)
// Chat handlers.
router.Handle("/", authMiddleware(http.FileServer(http.Dir("./"))))
router.Handle("/connection/websocket", authMiddleware(centrifuge.NewWebsocketHandler(node, centrifuge.WebsocketConfig{})))
// Auth handlers.
router.HandleFunc("/account", accountHandler)
router.HandleFunc("/login", loginHandler)
router.HandleFunc("/logout", logoutHandler)
router.HandleFunc("/callback", callbackHandler)
if err := http.ListenAndServe(":3000", router); err != nil {
log.Fatalln(err)
}
}
func accountHandler(w http.ResponseWriter, r *http.Request) {
value, _ := GetFromSession("user", r)
if value != "" {
_, _ = fmt.Fprintln(w, "<a href='/logout'>Logout</a>")
return
}
_, _ = fmt.Fprintln(w, "<a href='/login'>Log in with Google</a>")
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
oauthStateString := uniuri.New()
rawURL := googleOauthConfig.AuthCodeURL(oauthStateString)
parsedURL, err := url.Parse(rawURL)
if err != nil {
log.Printf("error parsing url: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
state := parsedURL.Query().Get("state")
err = StoreInSession("state", state, r, w)
if err != nil {
log.Printf("error storing in session %s\n", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, rawURL, http.StatusTemporaryRedirect)
}
func logoutHandler(w http.ResponseWriter, r *http.Request) {
_ = Logout(w, r)
http.Redirect(w, r, "/account", http.StatusTemporaryRedirect)
}
func callbackHandler(w http.ResponseWriter, r *http.Request) {
state := r.FormValue("state")
if state == "" {
_, _ = fmt.Fprintf(w, "Empty state received")
return
}
savedState, _ := GetFromSession("state", r)
if savedState == "" {
_, _ = fmt.Fprintf(w, "No state in session")
return
}
if state != savedState {
_, _ = fmt.Fprintf(w, "Invalid state")
return
}
code := r.FormValue("code")
token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
_, _ = fmt.Fprintf(w, "Code exchange failed with error %s\n", err.Error())
return
}
if !token.Valid() {
_, _ = fmt.Fprintln(w, "Retrieved invalid token")
return
}
client := &http.Client{
Timeout: 5 * time.Second,
}
response, err := client.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
if err != nil {
log.Printf("Error getting user from token %s\n", err.Error())
return
}
defer func() { _ = response.Body.Close() }()
contents, err := ioutil.ReadAll(response.Body)
var user *GoogleUser
err = json.Unmarshal(contents, &user)
if err != nil {
log.Printf("Error unmarshaling Google user %s\n", err.Error())
return
}
err = StoreInSession("user", string(contents), r, w)
if err != nil {
log.Printf("Error storing in session %s\n", err.Error())
return
}
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
// StoreInSession stores a specified key/value pair in the session.
func StoreInSession(key string, value string, req *http.Request, res http.ResponseWriter) error {
session, _ := Store.New(req, SessionName)
if err := updateSessionValue(session, key, value); err != nil {
return err
}
return session.Save(req, res)
}
// GetFromSession retrieves a previously-stored value from the session.
// If no value has previously been stored at the specified key, it will return an error.
func GetFromSession(key string, req *http.Request) (string, error) {
session, _ := Store.Get(req, SessionName)
value, err := getSessionValue(session, key)
if err != nil {
return "", errors.New("could not find a matching session for this request")
}
return value, nil
}
func getSessionValue(session *sessions.Session, key string) (string, error) {
value := session.Values[key]
if value == nil {
return "", fmt.Errorf("could not find a matching session for this request")
}
rdata := strings.NewReader(value.(string))
r, err := gzip.NewReader(rdata)
if err != nil {
return "", err
}
s, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
return string(s), nil
}
func updateSessionValue(session *sessions.Session, key, value string) error {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte(value)); err != nil {
return err
}
if err := gz.Flush(); err != nil {
return err
}
if err := gz.Close(); err != nil {
return err
}
session.Values[key] = b.String()
return nil
}
// Logout invalidates a user session.
func Logout(res http.ResponseWriter, req *http.Request) error {
session, err := Store.Get(req, SessionName)
if err != nil {
return err
}
session.Options.MaxAge = -1
session.Values = make(map[interface{}]interface{})
err = session.Save(req, res)
if err != nil {
return errors.New("could not delete user session")
}
return nil
}
|