impl oauth
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"local/gziphttp"
|
||||
"local/oauth2/oauth2client"
|
||||
"local/router"
|
||||
"local/todo-server/config"
|
||||
"log"
|
||||
@@ -22,32 +23,34 @@ func (s *Server) Routes() error {
|
||||
}{
|
||||
{
|
||||
path: "/",
|
||||
handler: s.gzip(s.index),
|
||||
handler: s.index,
|
||||
},
|
||||
{
|
||||
path: "/mytinytodo_lang.php",
|
||||
handler: s.gzip(s.lang),
|
||||
handler: s.lang,
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("/themes/%s%s", router.Wildcard, router.Wildcard),
|
||||
handler: s.gzip(s.handleDeviceCSS),
|
||||
handler: s.handleDeviceCSS,
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard),
|
||||
handler: s.gzip(s.phpProxy),
|
||||
handler: s.phpProxy,
|
||||
},
|
||||
{
|
||||
path: "/ajax.php",
|
||||
handler: s.gzip(s.HandleAjax),
|
||||
handler: s.HandleAjax,
|
||||
},
|
||||
}
|
||||
|
||||
for _, route := range routes {
|
||||
handler := route.handler
|
||||
handler = s.gzip(handler)
|
||||
handler = s.oauth(handler)
|
||||
if err := s.Add(route.path, route.handler); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
s.NotFound = s.gzip(s.index)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -143,6 +146,19 @@ func (s *Server) _static(w http.ResponseWriter, r *http.Request) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Server) oauth(h http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if config.OAuth != "" {
|
||||
err := oauth2client.Authenticate(config.OAuth, strings.Split(r.Host, ".")[0], w, r)
|
||||
if err != nil {
|
||||
log.Println("oauth failure", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
h(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if gziphttp.Can(r) {
|
||||
|
||||
Reference in New Issue
Block a user