-
Notifications
You must be signed in to change notification settings - Fork 2
/
oidc.go
148 lines (117 loc) · 3.3 KB
/
oidc.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
package ngroklistener
import (
"errors"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"golang.ngrok.com/ngrok/config"
)
type oidc struct {
opts []config.OIDCOption
opt config.HTTPEndpointOption
IssuerURL string `json:"issuer_url,omitempty"`
ClientID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
AllowEmails []string `json:"allow_emails,omitempty"`
AllowDomains []string `json:"allow_domains,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
func (o *oidc) Provision(caddy.Context) error {
o.doReplace()
if len(o.AllowEmails) > 0 {
o.opts = append(o.opts, config.WithAllowOIDCEmail(o.AllowEmails...))
}
if len(o.AllowDomains) > 0 {
o.opts = append(o.opts, config.WithAllowOIDCDomain(o.AllowDomains...))
}
if len(o.Scopes) > 0 {
o.opts = append(o.opts, config.WithOIDCScope(o.Scopes...))
}
if strings.TrimSpace(o.IssuerURL) == "" {
return errors.New("oidc `issuer_url` cannot be empty string")
}
if strings.TrimSpace(o.ClientID) == "" {
return errors.New("oidc `client_id` cannot be empty string")
}
if strings.TrimSpace(o.ClientSecret) == "" {
return errors.New("oidc `client_secret` cannot be empty string")
}
o.opt = config.WithOIDC(o.IssuerURL, o.ClientID, o.ClientSecret, o.opts...)
return nil
}
func (o *oidc) doReplace() {
repl := caddy.NewReplacer()
for index, email := range o.AllowEmails {
actual := repl.ReplaceKnown(email, "")
o.AllowEmails[index] = actual
}
for index, domain := range o.AllowDomains {
actual := repl.ReplaceKnown(domain, "")
o.AllowDomains[index] = actual
}
for index, scopes := range o.Scopes {
actual := repl.ReplaceKnown(scopes, "")
o.Scopes[index] = actual
}
o.IssuerURL = repl.ReplaceKnown(o.IssuerURL, "")
o.ClientID = repl.ReplaceKnown(o.ClientID, "")
o.ClientSecret = repl.ReplaceKnown(o.ClientSecret, "")
}
func (o *oidc) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if d.NextArg() {
return d.ArgErr()
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
subdirective := d.Val()
switch subdirective {
case "issuer_url":
if !d.AllArgs(&o.IssuerURL) {
return d.ArgErr()
}
case "client_id":
if !d.AllArgs(&o.ClientID) {
return d.ArgErr()
}
case "client_secret":
if !d.AllArgs(&o.ClientSecret) {
return d.ArgErr()
}
case "scopes":
if err := o.unmarshalScopes(d); err != nil {
return err
}
case "allow_domains":
if err := o.unmarshalAllowDomains(d); err != nil {
return err
}
case "allow_emails":
if err := o.unmarshalAllowEmails(d); err != nil {
return err
}
default:
return d.Errf("unrecognized subdirective %s", subdirective)
}
}
return nil
}
func (o *oidc) unmarshalScopes(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
o.Scopes = append(o.Scopes, d.RemainingArgs()...)
return nil
}
func (o *oidc) unmarshalAllowDomains(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
o.AllowDomains = append(o.AllowDomains, d.RemainingArgs()...)
return nil
}
func (o *oidc) unmarshalAllowEmails(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
o.AllowEmails = append(o.AllowEmails, d.RemainingArgs()...)
return nil
}