-
Notifications
You must be signed in to change notification settings - Fork 1
/
extauth.go
158 lines (142 loc) · 4.38 KB
/
extauth.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package extauth
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/rs/zerolog/log"
)
func init() {
caddy.RegisterModule(Middleware{})
httpcaddyfile.RegisterHandlerDirective("extauth", parseCaddyfile)
}
// Middleware implements an HTTP handler that calls an external service for authentication
type Middleware struct {
Endpoint string // http endpoint endpoint for the auth request
Timeout time.Duration // timeout for the auth request
CopyRequestHeaders []string // headers to copy from the incoming request to the auth request
CopyResponseHeaders []string // headers to copy from the auth response to the incoming request
SetHeaders map[string]string // headers to set in the auth request
httpClient *http.Client
}
// CaddyModule returns the Caddy module information.
func (Middleware) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.extauth",
New: func() caddy.Module { return new(Middleware) },
}
}
// Provision implements caddy.Provisioner.
func (m *Middleware) Provision(ctx caddy.Context) error {
m.httpClient = &http.Client{
Timeout: m.Timeout,
}
return nil
}
// Validate implements caddy.Validator.
func (m *Middleware) Validate() error {
if m.Endpoint == "" {
return errors.New("'endpoint' is required")
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
// create auth request
authReq, err := http.NewRequest(http.MethodGet, m.Endpoint, nil)
if err != nil {
return err
}
// copy headers from the incoming request
for _, name := range m.CopyRequestHeaders {
authReq.Header.Set(name, r.Header.Get(name))
}
// set additional headers
for key, val := range m.SetHeaders {
if val == "{http.request.uri}" {
val = r.URL.RequestURI()
}
if val == "{http.request.method}" {
val = r.Method
}
authReq.Header.Set(key, val)
}
// perform the request
resp, err := m.httpClient.Do(authReq)
if err != nil || resp.StatusCode != http.StatusOK {
// something went wrong or the server responded with something != 200 -> reject request with 401
log.Error().Str("url", r.URL.RequestURI()).Msg("failed to authenticate")
w.WriteHeader(http.StatusUnauthorized)
return nil
}
log.Info().Str("url", r.URL.RequestURI()).Msg("successfully authenticated")
// copy the user defined response headers to the incoming request before going on to the next handler in the chain
for _, name := range m.CopyResponseHeaders {
val := resp.Header.Get(name)
if val != "" {
r.Header.Set(name, resp.Header.Get(name))
}
}
// call next handler
return next.ServeHTTP(w, r)
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) (err error) {
m.SetHeaders = make(map[string]string)
d.NextArg()
for d.NextBlock(0) {
switch d.Val() {
case "endpoint":
if !d.AllArgs(&m.Endpoint) {
return d.ArgErr()
}
case "timeout":
timeoutStr := ""
if !d.AllArgs(&timeoutStr) {
return d.ArgErr()
}
m.Timeout, err = time.ParseDuration(timeoutStr)
if err != nil {
return fmt.Errorf("can't parse timeout: %w", err)
}
case "copy-request-header":
name := ""
for d.Args(&name) {
m.CopyRequestHeaders = append(m.CopyRequestHeaders, name)
}
case "copy-response-header":
name := ""
for d.Args(&name) {
m.CopyResponseHeaders = append(m.CopyResponseHeaders, name)
}
case "set-header":
var key, val string
for d.AllArgs(&key, &val) {
m.SetHeaders[key] = val
}
default:
return d.Errf("%s not a valid extauth option", d.Val())
}
}
if m.Timeout == 0 {
m.Timeout = 1 * time.Second
}
return nil
}
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Middleware
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// Interface guards
var (
_ caddy.Provisioner = (*Middleware)(nil)
_ caddy.Validator = (*Middleware)(nil)
_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
_ caddyfile.Unmarshaler = (*Middleware)(nil)
)