fix auth and post creation

This commit is contained in:
Pedro Nasser
2016-09-01 14:57:50 -03:00
parent 10de43f8d8
commit cd3e045f54
4 changed files with 22 additions and 65 deletions

View File

@@ -10,6 +10,8 @@ import (
"github.com/iron-io/functions/examples/blog/routes"
)
var noAuth = map[string]interface{}{}
func main() {
request := fmt.Sprintf("%s %s", os.Getenv("METHOD"), os.Getenv("ROUTE"))
@@ -23,30 +25,36 @@ func main() {
return
}
// PUBLIC REQUESTS
switch request {
case "GET /posts":
route.HandlePostList(db, noAuth)
return
case "GET /posts/:id":
route.HandlePostRead(db, noAuth)
return
}
// GETTING TOKEN
if os.Getenv("ROUTE") == "/token" {
route.HandleToken(db)
return
}
// AUTHENTICATION
auth, valid := route.Authentication()
if !valid {
route.SendError("Invalid authentication")
return
}
switch request {
case "GET /posts":
route.HandlePostList(db, auth)
break
case "POST /posts":
// AUTHENTICATED ONLY REQUESTS
if request == "POST /posts" {
route.HandlePostCreate(db, auth)
break
case "GET /posts/:id":
route.HandlePostRead(db, auth)
break
default:
route.SendError("Not found")
return
}
route.SendError("Not found")
}
func createUser(db *database.Database) bool {

View File

@@ -6,4 +6,5 @@ type Post struct {
ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
Title string `json:"title" bson:"title"`
Body string `json:"body" bson:"body"`
User string `json:"user" bsom:"user"`
}

View File

@@ -24,6 +24,8 @@ func HandlePostCreate(db *database.Database, auth map[string]interface{}) {
return
}
post.User = auth["user"].(string)
SendResponse(Response{
"post": post,
})

View File

@@ -89,57 +89,3 @@ func Authentication() (map[string]interface{}, bool) {
return nil, false
}
// func New(db *database.Database) *gin.Engine {
// DB = db
// r := gin.New()
// r.POST("/auth", func(c *gin.Context) {
// username := c.PostForm("username")
// password := c.PostForm("password")
// user, err := db.GetUser(username)
// if err != nil {
// c.JSON(500, gin.H{"message": "Could not generate token"})
// return
// }
// err = bcrypt.CompareHashAndPassword(user.Password, []byte(password))
// if err != nil {
// c.JSON(500, gin.H{"message": "Could not generate token"})
// return
// }
// token := jwt_lib.New(jwt_lib.GetSigningMethod("HS256"))
// claims := token.Claims.(jwt_lib.MapClaims)
// claims["ID"] = username
// claims["exp"] = time.Now().Add(time.Hour * 1).Unix()
// tokenString, err := token.SignedString([]byte(jwtSignKey))
// if err != nil {
// c.JSON(500, gin.H{"message": "Could not generate token"})
// return
// }
// c.JSON(200, gin.H{"token": tokenString})
// })
// r.POST("/testuser", func(c *gin.Context) {
// _, err := db.SaveUser(&models.User{
// Username: "test",
// Password: []byte("test"),
// })
// if err != nil {
// c.JSON(500, gin.H{"message": "Could create test user"})
// return
// }
// c.JSON(200, gin.H{"message": "test user created"})
// })
// blog := r.Group("/blog")
// blog.Use(jwtAuth(jwtSignKey))
// blog.GET("/posts", handlePostList)
// blog.POST("/posts", handlePostCreate)
// blog.GET("/posts/:id", handlePostRead)
// return r
// }