Created
December 1, 2015 16:24
-
-
Save yageek/78e43c83b56467fc8338 to your computer and use it in GitHub Desktop.
Cookie Expiring error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package backend | |
import ( | |
"encoding/json" | |
"net/http" | |
"time" | |
"github.com/codegangsta/negroni" | |
"github.com/gorilla/mux" | |
"appengine" | |
"appengine/user" | |
) | |
const ( | |
PATH_PREFIX = "/work/" | |
COOKIE_ID = "GOEAT-ID" | |
) | |
func init() { | |
app := negroni.New() | |
app.UseHandler(Router()) | |
http.Handle("/", app) | |
} | |
func Router() *mux.Router { | |
r := mux.NewRouter() | |
subRouter := r.PathPrefix(PATH_PREFIX).Subrouter() | |
subRouter.HandleFunc("/sign", LoginHandler) | |
subRouter.HandleFunc("/userinfo", UserInfo) | |
subRouter.HandleFunc("/logout", Logout) | |
return r | |
} | |
func sessionCookie(r *http.Request) (*http.Cookie, error) { | |
return r.Cookie(COOKIE_ID) | |
} | |
func Logout(w http.ResponseWriter, r *http.Request) { | |
ctx := appengine.NewContext(r) | |
url, err := user.LogoutURL(ctx, "/") | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
expiredCookie := &http.Cookie{Name: COOKIE_ID, MaxAge: -10, Expires: time.Now()} | |
http.SetCookie(w, expiredCookie) | |
w.Header().Set("Location", url) | |
w.WriteHeader(http.StatusFound) | |
} | |
func AuthHandler(next http.HandlerFunc) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | |
cookie, err := sessionCookie(req) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusUnauthorized) | |
return | |
} | |
ctx := appengine.NewContext(req) | |
u := user.Current(ctx) | |
if u == nil || u.ID != cookie.Value || cookie.MaxAge < 0 { | |
http.Error(w, "Invalid credential", http.StatusUnauthorized) | |
cookie.MaxAge = -1000 | |
cookie.Expires = time.Unix(1, 0) | |
http.SetCookie(w, cookie) | |
return | |
} | |
next(w, req) | |
}) | |
} | |
func LoginHandler(w http.ResponseWriter, r *http.Request) { | |
ctx := appengine.NewContext(r) | |
u := user.Current(ctx) | |
if u == nil { | |
url, err := user.LoginURL(ctx, r.URL.String()) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.Header().Set("Location", url) | |
w.WriteHeader(http.StatusFound) | |
return | |
} | |
cookie := &http.Cookie{Name: COOKIE_ID, Value: u.ID, Path: "/", MaxAge: 0} | |
http.SetCookie(w, cookie) | |
w.Header().Set("Location", "/") | |
w.WriteHeader(http.StatusFound) | |
} | |
func UserInfo(w http.ResponseWriter, req *http.Request) { | |
ctx := appengine.NewContext(req) | |
u := user.Current(ctx) | |
if u == nil { | |
http.Error(w, "Invalid", http.StatusInternalServerError) | |
return | |
} | |
enc := json.NewEncoder(w) | |
err := enc.Encode(u) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment