aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/httputil/middleware/tvm/middleware_opts.go
blob: 4e33b4ee59333ea9b848d3c08b499c068bbfaa1d (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
package tvm

import (
	"context"
	"net/http"

	"github.com/ydb-platform/ydb/library/go/core/log"
	"github.com/ydb-platform/ydb/library/go/yandex/tvm"
	"golang.org/x/xerrors"
)

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

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

// WithClientAuth sets custom function for client authorization.
func WithClientAuth(authClient func(ctx context.Context, src tvm.ClientID, dst 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
	}
}