-
Notifications
You must be signed in to change notification settings - Fork 4
/
reauth.go
79 lines (66 loc) · 1.76 KB
/
reauth.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
package reauth
import (
"fmt"
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/caddyauth"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(Reauth{})
}
// Reauth module
type Reauth struct {
Backends []Backend `json:"backends,omitempty"`
Failure Failure `json:"failure,omitempty"`
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (Reauth) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.authentication.providers.reauth",
New: func() caddy.Module { return new(Reauth) },
}
}
// Provision implements caddy.Provisioner.
func (r *Reauth) Provision(ctx caddy.Context) error {
r.logger = ctx.Logger(r)
r.logger.Info("provisioning plugin instance")
return nil
}
// Validate implements caddy.Validator.
func (r Reauth) Validate() error {
for i, be := range r.Backends {
if err := be.Validate(); err != nil {
return fmt.Errorf("backends[%d] (%s) failed validation: %s", i, be.Type, err)
}
}
if err := r.Failure.Validate(); err != nil {
return fmt.Errorf("failure mode %s failed validation: %s", r.Failure.Mode, err)
}
return nil
}
// Authenticate the request
func (r Reauth) Authenticate(w http.ResponseWriter, req *http.Request) (caddyauth.User, bool, error) {
for _, b := range r.Backends {
user, err := b.Authenticate(req)
if err != nil {
return caddyauth.User{}, false, err
}
if user != "" {
return caddyauth.User{
ID: user,
Metadata: map[string]string{
"reauth_backend": b.Type,
},
}, true, nil
}
}
return caddyauth.User{}, false, r.Failure.Handle(w, req)
}
// Interface guards
var (
_ caddy.Provisioner = (*Reauth)(nil)
_ caddy.Validator = (*Reauth)(nil)
_ caddyauth.Authenticator = (*Reauth)(nil)
)