-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (37 loc) · 965 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/punyaikhwan/echo-rsa-jwt"
mw "github.com/punyaikhwan/echo-rsa-jwt/middleware"
)
func tesJWT(c echo.Context) (err error) {
return c.JSON(http.StatusOK, echo_rsa_jwt.DecodeJWT(c.Get("user").(*jwt.Token)))
}
func genJWT(c echo.Context) (err error) {
claims := map[string]interface{}{
"name": c.FormValue("name"),
"nim": c.FormValue("nim"),
}
token, err := echo_rsa_jwt.GenerateJWT(echo_rsa_jwt.GenerateJWTInput{
PrivateKeyPath: "private.pem",
Claims: claims,
MinuteToExpire: 30,
})
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]string{
"token": token,
})
}
func main() {
e := echo.New()
e.GET("/tesjwt", tesJWT, mw.JWTEchoRSA("public.pem"))
e.POST("/genjwt", genJWT)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Logger.Fatal(e.Start(":9000"))
}