aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/httputil/middleware/tvm/middleware_opts.go
blob: 86df5d4796e1af72046ffd0b1b0e36b09a3750b8 (plain) (blame)
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
package tvm

import (
	"context"
	"net/http"

	"golang.org/x/xerrors"

	"a.yandex-team.ru/library/go/core/log"
	"a.yandex-team.ru/library/go/yandex/tvm"
)

// WithAllowedClients sets list of allowed clients.
func WithAllowedClients(allowedClients []tvm.ClientID) MiddlewareOption {
	return func(m *middleware) {
		m.authClient = func(_ context.Context, cid tvm.ClientID) error {
			for _, allowed := range allowedClients {
				if allowed == cid {
					return nil
				}
			}

			return xerrors.Errorf("client with tvm_id=%d is not whitelisted", cid)
		}
	}
}

// WithClientAuth sets custom function for client authorization.
func WithClientAuth(authClient func(context.Context, tvm.ClientID) error) MiddlewareOption {
	return func(m *middleware) {
		m.authClient = authClient
	}
}

// WithErrorHandler sets http handler invoked for rejected requests.
func WithErrorHandler(h func(w http.ResponseWriter, r *http.Request, err error)) MiddlewareOption {
	return func(m *middleware) {
		m.onError = h
	}
}

// WithLogger sets logger.
func WithLogger(l log.Structured) MiddlewareOption {
	return func(m *middleware) {
		m.l = l
	}
}