diff --git a/fakecas.go b/fakecas.go index a53624a..9b95ce3 100644 --- a/fakecas.go +++ b/fakecas.go @@ -4,8 +4,11 @@ import ( "flag" "fmt" "github.com/labstack/echo" + "github.com/labstack/echo/engine/standard" mw "github.com/labstack/echo/middleware" + "github.com/rs/cors" "gopkg.in/mgo.v2" + "os" ) var ( @@ -19,17 +22,27 @@ var ( func main() { flag.Parse() e := echo.New() - e.Use(mw.Logger()) + e.Use(mw.LoggerFromConfig(mw.LoggerConfig{ + Format: "${time_rfc3339} ${method} ${uri} ${status} ${response_time} ${response_size}\n", + Output: os.Stdout, + })) e.Use(mw.Recover()) - e.Use(CorsMiddleWare()) + e.Use(standard.WrapMiddleware(cors.New(cors.Options{ + AllowCredentials: true, + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "PUT", "POST", "DELETE"}, + AllowedHeaders: []string{"Range", "Content-Type", "Authorization", "X-Requested-With"}, + ExposedHeaders: []string{"Range", "Content-Type", "Authorization", "X-Requested-With"}, + }).Handler)) + + e.Get("/login", Login) e.Post("/login", Login) e.Get("/logout", Logout) e.Get("/oauth2/profile", OAuth) e.Get("/p3/serviceValidate", ServiceValidate) - fmt.Println("Expecting database", *DatabaseName, " to be running at", *DatabaseAddress) - fmt.Println("Listening on", *Host) + fmt.Println("Expecting database", *DatabaseName, "to be running at", *DatabaseAddress) DatabaseSession, err := mgo.Dial(*DatabaseAddress) if err != nil { @@ -39,5 +52,6 @@ func main() { UserCollection = DatabaseSession.DB(*DatabaseName).C("user") - e.Run(*Host) + fmt.Println("Listening on", *Host) + e.Run(standard.New(*Host)) } diff --git a/middleware.go b/middleware.go deleted file mode 100644 index 6b88def..0000000 --- a/middleware.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import "github.com/labstack/echo" - -func CorsMiddleWare() echo.MiddlewareFunc { - return func(h echo.HandlerFunc) echo.HandlerFunc { - return func(c *echo.Context) error { - c.Response().Header().Add("Access-Control-Allow-Origin", "*") - c.Response().Header().Add("Access-Control-Allow-Headers", "Range, Content-Type, Authorization, Cache-Control, X-Requested-With") - c.Response().Header().Add("Access-Control-Expose-Headers", "Range, Content-Type, Authorization, Cache-Control, X-Requested-With") - c.Response().Header().Add("Cache-control", "no-store, no-cache, must-revalidate, max-age=0") - - if c.Request().Method == "OPTIONS" { - c.Response().Header().Add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE") - return c.NoContent(204) - } - h(c) - return nil - } - } -} diff --git a/static.go b/static.go new file mode 100644 index 0000000..3fa8474 --- /dev/null +++ b/static.go @@ -0,0 +1,49 @@ +package main + +var UNREGISTERED = ` + + + Open Science Framework | Sign In + + + + + + +
+
+
+ + + +
+
+ +
+ +

Account has not been confirmed.

+

This login email has been registered but not confirmed. Please check your email (and spam folder). Click here to resend your confirmation email.

+
+ +
+
+ Back to OSF +
+
+ + + +
+ +` diff --git a/types.go b/types.go index 6154872..665a783 100644 --- a/types.go +++ b/types.go @@ -13,12 +13,13 @@ type OAuthResponse struct { } type User struct { - Id string `bson:"_id"` - Username string `bson:"username"` - Emails []string `bson:"emails"` - Fullname string `bson:"fullname"` - GivenName string `bson:"given_name"` - FamilyName string `bson:"family_name"` + Id string `bson:"_id"` + Username string `bson:"username"` + Emails []string `bson:"emails"` + Fullname string `bson:"fullname"` + GivenName string `bson:"given_name"` + FamilyName string `bson:"family_name"` + IsRegistered bool `bson:"is_registered"` } type ServiceResponse struct { diff --git a/views.go b/views.go index f0f6d0a..19b4210 100644 --- a/views.go +++ b/views.go @@ -9,35 +9,43 @@ import ( "strings" ) -func Login(c *echo.Context) error { - redir, err := url.Parse(c.Form("service")) +func Login(c echo.Context) error { + redir, err := url.Parse(c.FormValue("service")) if err != nil { c.Error(err) return nil } + result := User{} + + if err = UserCollection.Find(bson.M{"username": c.FormValue("username")}).One(&result); err != nil { + fmt.Println("User", c.FormValue("ticket"), "not found.") + return c.NoContent(http.StatusNotFound) + } + + if !result.IsRegistered { + return c.HTML(200, UNREGISTERED) + } + query := redir.Query() - query.Set("ticket", c.Form("username")) + query.Set("ticket", c.FormValue("username")) redir.RawQuery = query.Encode() fmt.Println("Logging in and redirecting to", redir) - c.Redirect(http.StatusFound, redir.String()) - return nil + return c.Redirect(http.StatusFound, redir.String()) } -func Logout(c *echo.Context) error { - fmt.Println("Logging out and redirecting to", c.Form("service")) - c.Redirect(http.StatusFound, c.Form("service")) - return nil +func Logout(c echo.Context) error { + fmt.Println("Logging out and redirecting to", c.FormValue("service")) + return c.Redirect(http.StatusFound, c.FormValue("service")) } -func ServiceValidate(c *echo.Context) error { +func ServiceValidate(c echo.Context) error { result := User{} - err := UserCollection.Find(bson.M{"emails": c.Form("ticket")}).One(&result) - if err != nil { - fmt.Println("User", c.Form("ticket"), "not found.") + if err := UserCollection.Find(bson.M{"emails": c.FormValue("ticket")}).One(&result); err != nil { + fmt.Println("User", c.FormValue("ticket"), "not found.") return c.NoContent(http.StatusNotFound) } @@ -55,10 +63,10 @@ func ServiceValidate(c *echo.Context) error { return c.XML(http.StatusOK, response) } -func OAuth(c *echo.Context) error { +func OAuth(c echo.Context) error { result := User{} err := UserCollection.Find(bson.M{ - "_id": strings.Replace(c.Request().Header.Get("Authorization"), "Bearer ", "", 1), + "_id": strings.Replace(c.Request().Header().Get("Authorization"), "Bearer ", "", 1), }).One(&result) if err != nil {