forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
54 lines (46 loc) · 1.33 KB
/
auth.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
package couchdb
import (
"context"
"net/http"
"github.com/flimzy/kivik"
"github.com/flimzy/kivik/errors"
"github.com/go-kivik/couchdb/chttp"
)
func (c *client) Authenticate(ctx context.Context, a interface{}) error {
if auth, ok := a.(chttp.Authenticator); ok {
return auth.Authenticate(ctx, c.Client)
}
if auth, ok := a.(Authenticator); ok {
return auth.auth(ctx, c)
}
return errors.Status(kivik.StatusUnknownError, "kivik: invalid authenticator")
}
// Authenticator is a CouchDB authenticator.
type Authenticator interface {
auth(context.Context, *client) error
}
type xportAuth struct {
http.RoundTripper
}
var _ Authenticator = &xportAuth{}
func (a *xportAuth) auth(_ context.Context, c *client) error {
if c.Client.Client.Transport != nil {
return errors.New("kivik: HTTP client transport already set")
}
c.Client.Client.Transport = a.RoundTripper
return nil
}
// SetTransport returns an authenticator that can be used to set a client
// connection's HTTP Transport. This can be used to control proxies, TLS
// configuration, keep-alives, compression, etc.
//
// Example:
//
// setXport := couchdb.SetTransport(&http.Transport{
// // .. custom config
// })
// client, _ := kivik.New( ... )
// client.Authenticate(setXport)
func SetTransport(t http.RoundTripper) Authenticator {
return &xportAuth{t}
}