Skip to content

Commit 2821ba3

Browse files
committed
proxy: add skip invoice creation param
1 parent ae4a241 commit 2821ba3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

proxy/service.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ type Service struct {
103103
// /package_name.ServiceName/MethodName
104104
AuthWhitelistPaths []string `long:"authwhitelistpaths" description:"List of regular expressions for paths that don't require authentication'"`
105105

106+
// AuthSkipInvoiceCreationPaths is an optional list of regular expressions
107+
// that are matched against the path of the URL of a request. If the request
108+
// URL matches any of those regular expressions, the call will not try to
109+
// create an invoice for the request, but still try to do the l402
110+
// authentication.
111+
AuthSkipInvoiceCreationPaths []string `long:"authskipinvoicecreationpaths" description:"List of regular expressions for paths that will skip invoice creation'"`
112+
106113
freebieDB freebie.DB
107114
pricer pricer.Pricer
108115
}
@@ -136,6 +143,21 @@ func (s *Service) AuthRequired(r *http.Request) auth.Level {
136143
return s.Auth
137144
}
138145

146+
// SkipInvoiceCreation determines if an invoice should be created for a
147+
// given request.
148+
func (s *Service) SkipInvoiceCreation(r *http.Request) bool {
149+
for _, pathRegexp := range s.AuthSkipInvoiceCreationPaths {
150+
pathRegexp := regexp.MustCompile(pathRegexp)
151+
if pathRegexp.MatchString(r.URL.Path) {
152+
log.Tracef("Req path [%s] matches skip entry "+
153+
"[%s].", r.URL.Path, pathRegexp)
154+
return true
155+
}
156+
}
157+
158+
return false
159+
}
160+
139161
// prepareServices prepares the backend service configurations to be used by the
140162
// proxy.
141163
func prepareServices(services []*Service) error {
@@ -195,6 +217,17 @@ func prepareServices(services []*Service) error {
195217
}
196218
}
197219

220+
// Make sure all skip invoice creation regular expression entries
221+
// actually compile so we run into an eventual panic during startup
222+
// and not only when the request happens.
223+
for _, entry := range service.AuthSkipInvoiceCreationPaths {
224+
_, err := regexp.Compile(entry)
225+
if err != nil {
226+
return fmt.Errorf("error validating auth "+
227+
"whitelist: %v", err)
228+
}
229+
}
230+
198231
// If dynamic prices are enabled then use the provided
199232
// DynamicPrice options to initialise a gRPC backed
200233
// pricer client.

0 commit comments

Comments
 (0)