-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
148 lines (131 loc) · 3.67 KB
/
config.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Shannon Wynter
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package reauth
import (
"fmt"
"github.com/freman/caddy-reauth/backend"
_ "github.com/freman/caddy-reauth/backends"
"github.com/caddyserver/caddy"
)
type Rule struct {
path []string
exceptions []string
backends []backend.Backend
onfail failure
}
func parseConfiguration(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
for c.Next() {
args := c.RemainingArgs()
switch len(args) {
case 0:
r, err := parseBlock(c)
if err != nil {
return nil, err
}
rules = append(rules, r)
default:
// we want only 0 arguments max
return nil, c.ArgErr()
}
}
return rules, nil
}
func parseBlock(c *caddy.Controller) (Rule, error) {
r := Rule{backends: []backend.Backend{}}
for c.NextBlock() {
switch c.Val() {
case "path":
// Path expects just one string argument and only one iteration
if !c.NextArg() {
return r, c.ArgErr()
}
r.path = append(r.path, c.Val())
if c.NextArg() {
return r, c.ArgErr()
}
case "except":
// Except can be specified multiple times with one string argument to
// provide exceptions
if !c.NextArg() {
return r, c.ArgErr()
}
r.exceptions = append(r.exceptions, c.Val())
if c.NextArg() {
return r, c.ArgErr()
}
case "failure":
if r.onfail != nil {
return r, c.ArgErr()
}
if !c.NextArg() {
return r, c.ArgErr()
}
name := c.Val()
args := ""
if c.NextArg() {
args = c.Val()
}
if c.NextArg() {
return r, c.ArgErr()
}
constructor, ok := failureHandlers[name]
if !ok {
return r, c.Errf("unknown failure handler %v: %v", name, args)
}
onfail, err := constructor(args)
if err != nil {
return r, c.Errf("%v for failure %v", err, name)
}
r.onfail = onfail
default:
// Handle backends which should all have just one argument after the plugin name
name := c.Val()
args := c.RemainingArgs()
if len(args) != 1 {
return r, fmt.Errorf("wrong number of arguments for %v: %v (%v:%v)", name, args, c.File(), c.Line())
}
config := args[0]
f, err := backend.Lookup(name)
if err != nil {
return r, fmt.Errorf("%v for %v (%v:%v)", err, name, c.File(), c.Line())
}
b, err := f(config)
if err != nil {
return r, fmt.Errorf("%v for %v (%v:%v)", err, name, c.File(), c.Line())
}
r.backends = append(r.backends, b)
}
}
if len(r.path) == 0 {
return r, fmt.Errorf("at least one path is required")
}
if len(r.backends) == 0 {
return r, fmt.Errorf("at least one backend required")
}
if r.onfail == nil {
r.onfail = &httpBasicOnFailure{}
}
return r, nil
}