-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoauth.go
143 lines (105 loc) · 2.98 KB
/
oauth.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
package ngroklistener
import (
"errors"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"golang.ngrok.com/ngrok/config"
)
type oauth struct {
opts []config.OAuthOption
opt config.HTTPEndpointOption
Provider string `json:"provider,omitempty"`
AllowEmails []string `json:"allow_emails,omitempty"`
AllowDomains []string `json:"allow_domains,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
func (o *oauth) Provision(caddy.Context) error {
o.doReplace()
if len(o.AllowEmails) > 0 {
o.opts = append(o.opts, config.WithAllowOAuthEmail(o.AllowEmails...))
}
if len(o.AllowDomains) > 0 {
o.opts = append(o.opts, config.WithAllowOAuthDomain(o.AllowDomains...))
}
if len(o.Scopes) > 0 {
o.opts = append(o.opts, config.WithOAuthScope(o.Scopes...))
}
if strings.TrimSpace(o.Provider) == "" {
return errors.New("oauth `provider` cannot be empty string")
}
o.opt = config.WithOAuth(o.Provider, o.opts...)
return nil
}
func (o *oauth) doReplace() {
repl := caddy.NewReplacer()
replacedAllowEmails := make([]string, len(o.AllowEmails))
for index, email := range o.AllowEmails {
actual := repl.ReplaceKnown(email, "")
replacedAllowEmails[index] = actual
}
o.AllowEmails = replacedAllowEmails
replacedAllowDomains := make([]string, len(o.AllowDomains))
for index, domain := range o.AllowDomains {
actual := repl.ReplaceKnown(domain, "")
replacedAllowDomains[index] = actual
}
o.AllowDomains = replacedAllowDomains
replacedScopes := make([]string, len(o.Scopes))
for index, scope := range o.Scopes {
actual := repl.ReplaceKnown(scope, "")
replacedScopes[index] = actual
}
o.Scopes = replacedScopes
o.Provider = repl.ReplaceKnown(o.Provider, "")
}
func (o *oauth) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if d.NextArg() {
return d.ArgErr()
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
subdirective := d.Val()
switch subdirective {
case "provider":
if !d.AllArgs(&o.Provider) {
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 *oauth) unmarshalScopes(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
o.Scopes = append(o.Scopes, d.RemainingArgs()...)
return nil
}
func (o *oauth) unmarshalAllowDomains(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
o.AllowDomains = append(o.AllowDomains, d.RemainingArgs()...)
return nil
}
func (o *oauth) unmarshalAllowEmails(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 0 {
return d.ArgErr()
}
o.AllowEmails = append(o.AllowEmails, d.RemainingArgs()...)
return nil
}