fixed oauth bug
figured out it was how i was using sessions.
This commit is contained in:
parent
e63fee56dc
commit
6aebd3476c
@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"s.mcquay.me/dm/chipmunk"
|
"s.mcquay.me/dm/chipmunk"
|
||||||
|
|
||||||
|
"github.com/gorilla/context"
|
||||||
"github.com/kelseyhightower/envconfig"
|
"github.com/kelseyhightower/envconfig"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -98,7 +99,7 @@ func main() {
|
|||||||
log.Printf("serving at: http://%s:%d/", hostname, config.Port)
|
log.Printf("serving at: http://%s:%d/", hostname, config.Port)
|
||||||
|
|
||||||
addr := fmt.Sprintf("%s:%d", config.Host, config.Port)
|
addr := fmt.Sprintf("%s:%d", config.Host, config.Port)
|
||||||
err = http.ListenAndServe(addr, sm)
|
err = http.ListenAndServe(addr, context.ClearHandler(sm))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%+v", err)
|
log.Printf("%+v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
27
server.go
27
server.go
@ -77,7 +77,7 @@ func (s *Server) fakeSetup(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
|
||||||
//TODO add back in oauth
|
//TODO add back in oauth
|
||||||
//w.Header().Set("Content-Type", "application/json")
|
//w.Header().Set("Content-Type", "application/json")
|
||||||
//session, _ := store.Get(r, "creds")
|
//session, err := store.Get(r, "creds")
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
// return
|
// return
|
||||||
@ -133,7 +133,7 @@ func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) {
|
||||||
//TODO add back in oauth
|
//TODO add back in oauth
|
||||||
//w.Header().Set("Content-Type", "application/json")
|
//w.Header().Set("Content-Type", "application/json")
|
||||||
//session, _ := store.Get(r, "creds")
|
//session, err := store.Get(r, "creds")
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
// return
|
// return
|
||||||
@ -174,7 +174,7 @@ func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
//TODO add back in oauth
|
//TODO add back in oauth
|
||||||
//w.Header().Set("Content-Type", "application/json")
|
//w.Header().Set("Content-Type", "application/json")
|
||||||
//session, _ := store.Get(r, "creds")
|
//session, err := store.Get(r, "creds")
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
// return
|
// return
|
||||||
@ -228,7 +228,6 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer email.Body.Close()
|
defer email.Body.Close()
|
||||||
data, _ := ioutil.ReadAll(email.Body)
|
data, _ := ioutil.ReadAll(email.Body)
|
||||||
u := userInfo{}
|
u := userInfo{}
|
||||||
@ -242,8 +241,10 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
|
|||||||
if authorizedEmail(u.Email) {
|
if authorizedEmail(u.Email) {
|
||||||
session, err := store.Get(r, "creds")
|
session, err := store.Get(r, "creds")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
if !session.IsNew {
|
||||||
return
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
session.Values["authenticated"] = true
|
session.Values["authenticated"] = true
|
||||||
session.Values["uname"] = u.Email
|
session.Values["uname"] = u.Email
|
||||||
@ -280,19 +281,19 @@ func (s *Server) auth(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, string(b), http.StatusUnauthorized)
|
http.Error(w, string(b), http.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) logout(w http.ResponseWriter, req *http.Request) {
|
func (s *Server) logout(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := store.Get(req, "creds")
|
session, err := store.Get(r, "creds")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
delete(session.Values, "authenticated")
|
delete(session.Values, "authenticated")
|
||||||
delete(session.Values, "uname")
|
delete(session.Values, "uname")
|
||||||
session.Save(req, w)
|
session.Save(r, w)
|
||||||
http.Redirect(w, req, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) serverInfo(w http.ResponseWriter, req *http.Request) {
|
func (s *Server) serverInfo(w http.ResponseWriter, r *http.Request) {
|
||||||
output := struct {
|
output := struct {
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
Start string `json:"start"`
|
Start string `json:"start"`
|
||||||
@ -309,6 +310,10 @@ func (s *Server) serverInfo(w http.ResponseWriter, req *http.Request) {
|
|||||||
func (s *Server) plist(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) plist(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := store.Get(r, "creds")
|
session, err := store.Get(r, "creds")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if session.IsNew {
|
||||||
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user