/
context.go
47 lines (35 loc) · 1.1 KB
/
context.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
package steranko
import (
"github.com/benpate/derp"
"github.com/golang-jwt/jwt/v4"
"github.com/labstack/echo/v4"
)
// Context extends the echo context with an authenticated JWT Token.
type Context struct {
steranko *Steranko
claims jwt.Claims
echo.Context
}
func (ctx *Context) Authorization() (jwt.Claims, error) {
// Only comput this once, then store in the context for next time.
if ctx.claims == nil {
// Retrieve the cookie value from the context
name := cookieName(ctx)
tokenString, err := ctx.Cookie(name)
if err != nil {
return nil, derp.Wrap(err, "steranko.Context.Claims", "Invalid cookie")
}
claims := ctx.steranko.UserService.NewClaims()
// Parse it as a JWT token
token, err := jwt.ParseWithClaims(tokenString.Value, claims, ctx.steranko.KeyService.FindJWTKey)
if err != nil {
return nil, derp.Wrap(err, "steranko.Context.Claims", "Error parsing token")
}
if !token.Valid {
return nil, derp.NewForbiddenError("steranko.Context.Claims", "Invalid token")
}
// Save this value in the context for next time.
ctx.claims = claims
}
return ctx.claims, nil
}