-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
93 lines (74 loc) · 2.17 KB
/
middleware.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package oidc
import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/konstantinfoerster/card-service-go/internal/common"
"github.com/konstantinfoerster/card-service-go/internal/common/auth"
"github.com/konstantinfoerster/card-service-go/internal/common/config"
commonhttp "github.com/konstantinfoerster/card-service-go/internal/common/http"
)
type MiddlewareConfig struct {
Extractor func(*fiber.Ctx, string) (*auth.User, error)
Key string
AllowEmptyCookie bool
}
func NewOauthMiddleware(svc UserService, options ...func(*MiddlewareConfig)) fiber.Handler {
c := MiddlewareConfig{
Extractor: func(ctx *fiber.Ctx, cookie string) (*auth.User, error) {
if cookie == "" {
return nil, common.NewAuthorizationError(fmt.Errorf("no running session found"), "no-session")
}
jwtToken, err := commonhttp.DecodeBase64[JSONWebToken](cookie)
if err != nil {
return nil, err
}
return svc.GetAuthenticatedUser(jwtToken.Provider, jwtToken)
},
}
for _, optionFn := range options {
optionFn(&c)
}
return newTokenExtractHandler(c)
}
func FromConfig(cfg config.Oidc) func(*MiddlewareConfig) {
return func(c *MiddlewareConfig) {
c.Key = cfg.SessionCookieNameOrDefault()
}
}
func AllowEmptyCookie() func(*MiddlewareConfig) {
return func(c *MiddlewareConfig) {
c.AllowEmptyCookie = true
}
}
func newTokenExtractHandler(config ...MiddlewareConfig) fiber.Handler {
// Init config
var cfg MiddlewareConfig
if len(config) > 0 {
cfg = config[0]
}
if cfg.Key == "" {
cfg.Key = "SESSION"
}
if cfg.Extractor == nil {
panic("OAuth handler requires an extractor function")
}
return func(c *fiber.Ctx) error {
// Extract and verify key
cookieValue := c.Cookies(cfg.Key)
if cookieValue == "" {
if cfg.AllowEmptyCookie {
return c.Next()
}
return common.NewAuthorizationError(fmt.Errorf("unauthorized"), "unauthorized")
}
value, err := cfg.Extractor(c, cookieValue)
if err == nil && value != nil {
auth.UserToCtx(c, value)
return c.Next()
}
if err != nil {
return common.NewAuthorizationError(err, "unauthorized")
}
return common.NewAuthorizationError(fmt.Errorf("invalid or expired session"), "unauthorized")
}
}