-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathcaddyfile_authn.go
More file actions
216 lines (208 loc) 路 7.59 KB
/
Copy pathcaddyfile_authn.go
File metadata and controls
216 lines (208 loc) 路 7.59 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2022 Paul Greenberg greenpau@outlook.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package security
import (
"strings"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/greenpau/go-authcrunch/pkg/authn"
"github.com/greenpau/go-authcrunch/pkg/authn/ui"
"github.com/greenpau/go-authcrunch/pkg/authz/options"
"github.com/greenpau/go-authcrunch/pkg/errors"
)
const (
authnPrefix = "security.authentication"
)
// parseCaddyfileAuthentication parses an authentication portal in security.
// See the caddyfile_authn_* helpers for the full grammar of each subdirective.
// Cookie and admin API statements are collected across the complete portal and
// validated by their shared go-authcrunch parsers, including alias collisions.
//
// Syntax:
//
// authentication portal <name> {
// crypto key sign-verify <shared_secret>
// oidc provider { ... }
// token refresh { ... }
// ui { ... }
// transform user { ... }
// cookie prefix <prefix>
// cookie access token name <name>
// validate source address
// enable source ip tracking
// <enable|disable> admin api
// <enable|disable> admin api private key export
// enable identity store <name> [<name>...]
// enable identity provider <name> [<name>...]
// enable sso provider <name> [<name>...]
// trust <login|logout> redirect uri domain [exact|partial|prefix|suffix|regex] <domain> path [exact|partial|prefix|suffix|regex] <path>
// }
//
// Registration is configured with user registration in security and attached to
// an identity store; there is no enable user registration portal directive.
//
// The optional, single oidc provider block is collected by
// readCaddyfileOIDCProvider and attached before AddAuthenticationPortal validates
// the completed portal. The global parser registers all applications first,
// including declarations following this portal or expanded from later imports.
func parseCaddyfileAuthentication(d *caddyfile.Dispenser, app *App) error {
// rootDirective is config key prefix.
var rootDirective string
args := d.RemainingArgs()
if len(args) != 2 {
return d.ArgErr()
}
switch args[0] {
case "portal":
p := &authn.PortalConfig{
Name: args[1],
UI: &ui.Parameters{
Templates: make(map[string]string),
},
TokenValidatorOptions: &options.TokenValidatorOptions{},
TokenGrantorOptions: &options.TokenGrantorOptions{},
API: &authn.APIConfig{
ProfileEnabled: true,
},
}
var cookieStatements []string
var adminStatements []string
var oidcStatements []string
var tokenRefreshStatements []string
nesting := d.Nesting()
for d.NextBlock(nesting) {
k := d.Val()
v := d.RemainingArgs()
rootDirective = mkcp(authnPrefix, args[0], k)
switch k {
case "token":
if tokenRefreshStatements != nil {
return d.Errf("token refresh is already configured for portal %q", p.Name)
}
statements, err := readCaddyfileTokenRefresh(d, v)
if err != nil {
return err
}
tokenRefreshStatements = statements
case "oidc":
if oidcStatements != nil {
return d.Errf("oidc provider is already configured for portal %q", p.Name)
}
statements, err := readCaddyfileOIDCProvider(d, v)
if err != nil {
return err
}
oidcStatements = statements
case "crypto":
if err := parseCaddyfileAuthPortalCrypto(d, p, rootDirective, v); err != nil {
return err
}
case "cookie", "set":
statement, err := encodePortalCookieDirective(k, v, true)
if err != nil {
return d.Errf("%s: %v", rootDirective, err)
}
cookieStatements = append(cookieStatements, statement)
case "ui":
if err := parseCaddyfileAuthPortalUI(d, p, rootDirective); err != nil {
return err
}
case "transform":
if err := parseCaddyfileAuthPortalTransform(d, p, rootDirective, v); err != nil {
return err
}
case "enable", "disable":
if k == "enable" && len(v) > 0 && !strings.HasPrefix(v[0], "admin") {
if err := parseCaddyfileAuthPortalMisc(d, p, rootDirective, k, v); err != nil {
return err
}
continue
}
statement, err := encodePortalAdminAPIDirective(k, v)
if err != nil {
return d.Errf("%s: %v", rootDirective, err)
}
// Admin settings are statements, never nested blocks.
if d.Next() {
hasBlock := d.Val() == "{"
d.Prev()
if hasBlock {
return d.Errf("%s: admin API directives do not accept blocks", rootDirective)
}
}
adminStatements = append(adminStatements, statement)
case "validate", "trust":
if err := parseCaddyfileAuthPortalMisc(d, p, rootDirective, k, v); err != nil {
return err
}
default:
return errors.ErrMalformedDirective.WithArgs(rootDirective, v)
}
}
// NextSegment counts quoted brace-valued arguments as structural tokens.
// A truncated segment must not let a child's closing brace also satisfy
// this portal's boundary merely because NextBlock reached EOF.
if d.Nesting() != nesting {
return d.Errf("unterminated authentication portal block")
}
if err := configurePortalAdminAPI(p, adminStatements); err != nil {
return d.Errf("%s.portal %q admin API: %v", authnPrefix, p.Name, err)
}
if tokenRefreshStatements != nil {
if cookieDirectivesNeedResolution(tokenRefreshStatements) {
if app.PortalTokenRefreshDirectives == nil {
app.PortalTokenRefreshDirectives = make(map[string][]string)
}
if _, exists := app.PortalTokenRefreshDirectives[p.Name]; exists {
return d.Errf("duplicate token refresh portal %q", p.Name)
}
app.PortalTokenRefreshDirectives[p.Name] = tokenRefreshStatements
} else if err := configurePortalTokenRefresh(p, tokenRefreshStatements); err != nil {
return d.Errf("portal %q token refresh: %v", p.Name, err)
}
}
// Refresh may override the shared refresh cookie name. Resolve that choice
// before the shared cookie parser checks the effective names for collisions.
// Preserve both complete snapshots across Caddy JSON when refresh is deferred,
// even if the cookie directives themselves contain no placeholders.
if cookieDirectivesNeedResolution(cookieStatements) || cookieDirectivesNeedResolution(tokenRefreshStatements) {
if app.PortalCookieDirectives == nil {
app.PortalCookieDirectives = make(map[string][]string)
}
if _, exists := app.PortalCookieDirectives[p.Name]; exists {
return d.Errf("duplicate cookie portal %q", p.Name)
}
app.PortalCookieDirectives[p.Name] = cookieStatements
} else if err := configurePortalCookies(p, cookieStatements); err != nil {
return d.Errf("%s.portal %q cookies: %v", authnPrefix, p.Name, err)
}
if oidcStatements != nil {
if err := app.Config.ConfigureOIDCProvider(p, oidcStatements); err != nil {
return d.Errf("%s.portal %q oidc provider: %v", authnPrefix, p.Name, err)
}
if app.OIDCProviderDirectives == nil {
app.OIDCProviderDirectives = make(map[string][]string)
}
app.OIDCProviderDirectives[p.Name] = oidcStatements
}
if err := app.Config.AddAuthenticationPortal(p); err != nil {
return err
}
default:
return errors.ErrMalformedDirective.WithArgs(authnPrefix, args)
}
return nil
}
func mkcp(parts ...string) string {
return strings.Join(parts, ".")
}