Basic JWT is now working #12
@ -18,7 +18,7 @@ type JwtToken struct {
|
|||||||
jwt.RegisteredClaims
|
jwt.RegisteredClaims
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateJwt() (string, error) {
|
func generateJwt(username string) (string, error) {
|
||||||
//TODO use env here
|
//TODO use env here
|
||||||
secret := []byte("ThisIsABadSecretDontReallyUseThis")
|
secret := []byte("ThisIsABadSecretDontReallyUseThis")
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ func generateJwt() (string, error) {
|
|||||||
claims := token.Claims.(jwt.MapClaims)
|
claims := token.Claims.(jwt.MapClaims)
|
||||||
claims["exp"] = time.Now().Add(10 * time.Minute)
|
claims["exp"] = time.Now().Add(10 * time.Minute)
|
||||||
claims["authorized"] = true
|
claims["authorized"] = true
|
||||||
claims["username"] = "someone"
|
claims["username"] = username
|
||||||
|
|
||||||
tokenString, err := token.SignedString(secret)
|
tokenString, err := token.SignedString(secret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -86,7 +86,7 @@ func (h *Handler) AuthLogin(c echo.Context) error {
|
|||||||
return c.JSON(http.StatusInternalServerError, err)
|
return c.JSON(http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := generateJwt()
|
token, err := generateJwt(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, err)
|
return c.JSON(http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ func (h *Handler) HelloBody(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusBadRequest, HelloWhoResponse{
|
return c.JSON(http.StatusBadRequest, HelloWhoResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Error: err.Error(),
|
Error: err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,6 @@ func (h *Handler) HelloBody(c echo.Context) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) ProtectedRoute(c echo.Context) error {
|
||||||
func (h *Handler) ProtectedRoute(c echo.Context)error {
|
return c.JSON(http.StatusOK, "You have a good bearer token!")
|
||||||
return nil
|
|
||||||
}
|
}
|
@ -43,7 +43,8 @@ func (h *Handler) Register(v1 *echo.Group) {
|
|||||||
demo.GET("/hello/body", h.HelloBody)
|
demo.GET("/hello/body", h.HelloBody)
|
||||||
|
|
||||||
protected := v1.Group("/demo/protected")
|
protected := v1.Group("/demo/protected")
|
||||||
protected.GET("/", h.ProtectedRoute)
|
protected.Use(echojwt.WithConfig(jwtConfig))
|
||||||
|
protected.GET("", h.ProtectedRoute)
|
||||||
|
|
||||||
//recipes := v1.Group("/recipe")
|
//recipes := v1.Group("/recipe")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user