mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fix auth and post creation
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
|||||||
"github.com/iron-io/functions/examples/blog/routes"
|
"github.com/iron-io/functions/examples/blog/routes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var noAuth = map[string]interface{}{}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
request := fmt.Sprintf("%s %s", os.Getenv("METHOD"), os.Getenv("ROUTE"))
|
request := fmt.Sprintf("%s %s", os.Getenv("METHOD"), os.Getenv("ROUTE"))
|
||||||
|
|
||||||
@@ -23,30 +25,36 @@ func main() {
|
|||||||
return
|
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" {
|
if os.Getenv("ROUTE") == "/token" {
|
||||||
route.HandleToken(db)
|
route.HandleToken(db)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AUTHENTICATION
|
||||||
auth, valid := route.Authentication()
|
auth, valid := route.Authentication()
|
||||||
if !valid {
|
if !valid {
|
||||||
route.SendError("Invalid authentication")
|
route.SendError("Invalid authentication")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch request {
|
// AUTHENTICATED ONLY REQUESTS
|
||||||
case "GET /posts":
|
if request == "POST /posts" {
|
||||||
route.HandlePostList(db, auth)
|
|
||||||
break
|
|
||||||
case "POST /posts":
|
|
||||||
route.HandlePostCreate(db, auth)
|
route.HandlePostCreate(db, auth)
|
||||||
break
|
return
|
||||||
case "GET /posts/:id":
|
|
||||||
route.HandlePostRead(db, auth)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
route.SendError("Not found")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
route.SendError("Not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUser(db *database.Database) bool {
|
func createUser(db *database.Database) bool {
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ type Post struct {
|
|||||||
ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
|
ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
|
||||||
Title string `json:"title" bson:"title"`
|
Title string `json:"title" bson:"title"`
|
||||||
Body string `json:"body" bson:"body"`
|
Body string `json:"body" bson:"body"`
|
||||||
|
User string `json:"user" bsom:"user"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ func HandlePostCreate(db *database.Database, auth map[string]interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
post.User = auth["user"].(string)
|
||||||
|
|
||||||
SendResponse(Response{
|
SendResponse(Response{
|
||||||
"post": post,
|
"post": post,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -89,57 +89,3 @@ func Authentication() (map[string]interface{}, bool) {
|
|||||||
|
|
||||||
return nil, false
|
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
|
|
||||||
// }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user