-
Notifications
You must be signed in to change notification settings - Fork 103
/
paseto.go
52 lines (46 loc) 路 1.2 KB
/
paseto.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
package pasetoware
import (
"errors"
"github.com/gofiber/fiber/v2"
)
// New ...
func New(authConfigs ...Config) fiber.Handler {
// Set default authConfig
config := configDefault(authConfigs...)
var extractor acquireToken
switch config.TokenLookup[0] {
case LookupHeader:
extractor = acquireFromHeader
case LookupQuery:
extractor = acquireFromQuery
case LookupParam:
extractor = acquireFromParams
case LookupCookie:
extractor = acquireFromCookie
default:
extractor = acquireFromHeader
}
// Return middleware handler
return func(c *fiber.Ctx) error {
token := extractor(c, config.TokenLookup[1])
// Filter request to skip middleware
if config.Next != nil && config.Next(c) {
return c.Next()
}
if token == "" {
return config.ErrorHandler(c, errors.New("bad: missing PASETO token"))
}
var decryptedData []byte
err := pasetoObject.Decrypt(token, config.SymmetricKey, &decryptedData, nil)
if err == nil {
var payload interface{}
payload, err = config.Validate(decryptedData)
if err == nil {
// Store user information from token into context.
c.Locals(config.ContextKey, payload)
return config.SuccessHandler(c)
}
}
return config.ErrorHandler(c, err)
}
}