/
authed.go
67 lines (53 loc) · 1.44 KB
/
authed.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
65
66
67
package middleware
import (
"errors"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/eyecuelab/go-api/internal/models"
"github.com/eyecuelab/kit/web"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type (
// AuthedContext web api authed context extension
AuthedContext interface {
web.ApiContext
User() *models.User
LoggedIn() bool
}
authedContext struct {
web.ApiContext
user *models.User
}
authedContextLookup struct {
web.AuthedContextLookup
}
)
func (c *authedContext) User() *models.User {
return c.user
}
func (c *authedContext) LoggedIn() bool {
return c.user != nil
}
func (cl *authedContextLookup) Lookup(c echo.Context) (echo.Context, error) {
ac := &authedContext{c.(web.ApiContext), nil}
token, ok := ac.Get("user").(*jwt.Token)
if !ok {
return nil, errors.New("Failed to find token")
}
claims, k := token.Claims.(jwt.MapClaims)
if !k {
return nil, errors.New("invalid claims")
}
id := claims["user"].(float64)
ac.user = &models.User{ID: int(id)}
return ac, ac.user.Find()
}
func (cl *authedContextLookup) Context(c echo.Context) echo.Context {
return &authedContext{c.(web.ApiContext), nil}
}
// Authed returns a middleware for authed routes
func Authed() echo.MiddlewareFunc {
middleware.ErrJWTMissing = echo.NewHTTPError(http.StatusUnauthorized, "Missing authorization header")
return web.AuthedWithConfig(web.AuthedConfig{Skipper: web.AuthedSkipper()}, &authedContextLookup{})
}