Convert to GIN

This commit is contained in:
Jan Bader 2016-11-23 21:51:43 +01:00
parent 48eea7fb37
commit c5d8e858a4

75
main.go
View File

@ -5,9 +5,7 @@ import (
"time" "time"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/labstack/echo" "gopkg.in/gin-gonic/gin.v1"
"github.com/labstack/echo/engine/standard"
"github.com/labstack/echo/middleware"
) )
const ( const (
@ -16,69 +14,68 @@ const (
) )
func main() { func main() {
e := echo.New() router := gin.Default()
// Middleware // Middleware
e.Use(middleware.Logger()) //e.Use(middleware.Logger())
e.Use(middleware.Recover()) //e.Use(middleware.Recover())
e.Use(middleware.Static("static")) //e.Use(middleware.Static("static"))
a := e.Group("/api") a := router.Group("/api/v1")
a.POST("/login", login) {
a.POST("/login", loginPost)
// Unauthenticated routes // Unauthenticated routes
a.GET("/check", accessible) a.GET("/check", func(c *gin.Context) {
a.GET("/hello", func(c echo.Context) error { c.String(http.StatusOK, "Accessible")
return c.String(http.StatusOK, "Hello, World!") })
}) a.GET("/hello", func(c *gin.Context) {
c.String(http.StatusOK, "Hello, World!")
})
}
// Restricted group // Restricted group
r := a.Group("/restricted") r := a.Group("/restricted")
r.Use(middleware.JWT([]byte(secret))) {
r.GET("", restricted) //r.Use(middleware.JWT([]byte(secret)))
r.GET("", restricted)
}
e.Run(standard.New(":1323")) router.Run(":1323")
} }
func accessible(c echo.Context) error { func restricted(c *gin.Context) {
return c.String(http.StatusOK, "Accessible") //user, _ := c.Get("user") //.(*jwt.Token)
//name := user.Claims["name"].(string)
name := "jan"
c.String(http.StatusOK, "Welcome "+name+"!")
} }
func restricted(c echo.Context) error { func loginPost(c *gin.Context) {
user := c.Get("user").(*jwt.Token) username, _ := c.GetPostForm("username")
name := user.Claims["name"].(string) password, _ := c.GetPostForm("password")
return c.String(http.StatusOK, "Welcome "+name+"!")
}
func login(c echo.Context) error {
username := c.FormValue("username")
password := c.FormValue("password")
if username == "jan" && password == "passwort" { if username == "jan" && password == "passwort" {
// Create token // Create token
token := jwt.New(jwt.SigningMethodHS256) token := jwt.New(jwt.SigningMethodHS256)
// Set claims // Set claims
token.Claims["name"] = "Jan Bader" //token.Claims["name"] = "Jan Bader"
token.Claims["admin"] = true //token.Claims["admin"] = true
token.Claims["exp"] = time.Now().Add(time.Hour * expiration).Unix() //token.Claims["exp"] = time.Now().Add(time.Hour * expiration).Unix()
// Generate encoded token and send it as response. // Generate encoded token and send it as response.
t, err := token.SignedString([]byte(secret)) t, err := token.SignedString([]byte(secret))
if err != nil { if err != nil {
return err c.AbortWithStatus(http.StatusUnauthorized)
} }
cookie := new(echo.Cookie) c.SetCookie("authentication", t, (int)((expiration * time.Hour).Seconds()), "/", "localhost:8080", true, false)
cookie.SetName("authentication")
cookie.SetValue(t)
cookie.SetExpires(time.Now().Add(expiration * time.Hour))
c.SetCookie(cookie)
return c.JSON(http.StatusOK, map[string]string{ c.JSON(http.StatusOK, map[string]string{
"token": t, "token": t,
}) })
} }
return echo.ErrUnauthorized c.AbortWithStatus(http.StatusUnauthorized)
} }