/
pionenroute.go
64 lines (52 loc) · 1.5 KB
/
pionenroute.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package pionenroute
import (
"github.com/Hackform/hfse/kappa"
"github.com/Hackform/hfse/model/pionenmodel"
"github.com/Hackform/hfse/route"
"github.com/Hackform/hfse/service/pionen"
"github.com/labstack/echo"
"net/http"
)
type (
PionenRoute struct {
route.RouteBase
authService kappa.Const
}
)
func New(path string, authService kappa.Const) *PionenRoute {
p := &PionenRoute{
authService: authService,
}
p.RouteBase.SetPath(path)
return p
}
//////////////
// Register //
//////////////
func (p *PionenRoute) Register(g *echo.Group) {
auth := p.GetService(p.authService).(*pionen.Pionen)
g.POST("/login", func(c echo.Context) error {
loginAttempt := pionenmodel.GetRequestLogin(c)
if jwtString, err := auth.GetJWT(loginAttempt.Value.Username, loginAttempt.Value.Password); err == nil {
return c.JSON(http.StatusOK, pionenmodel.RequestJWT{Value: pionenmodel.JWTToken{Token: jwtString}})
} else {
return echo.NewHTTPError(http.StatusBadRequest, "invalid password")
}
})
// for testing
g.GET("/unstable/verify", func(c echo.Context) error {
return c.JSON(http.StatusOK, true)
}, auth.MAuthUser())
// for testing
g.POST("/unstable/decode", func(c echo.Context) error {
req := pionenmodel.GetRequestJWT(c)
claims, err := auth.ParseJWT(req.Value.Token)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid token")
}
return c.JSON(http.StatusOK, claims)
})
}
func (p *PionenRoute) Middleware() []echo.MiddlewareFunc {
return []echo.MiddlewareFunc{}
}