/
convenience.go
136 lines (120 loc) · 4.03 KB
/
convenience.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
// @author Couchbase <info@couchbase.com>
// @copyright 2014 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cbauth
import (
"crypto/tls"
"encoding/json"
"net/http"
)
// SetRequestAuthVia sets basic auth header in given http request
// according to given authenticator. It will extract target
// hostname/port from request and figure out right service credentials
// for that endpoint. If nil authenticator is passed, Default
// authenticator is used.
func SetRequestAuthVia(req *http.Request, a Authenticator) error {
return WithAuthenticator(a, func(a Authenticator) (err error) {
user, pwd, err := a.GetHTTPServiceAuth(req.URL.Host)
if err != nil {
return
}
req.SetBasicAuth(user, pwd)
return
})
}
// SetRequestAuth sets basic auth header in given http request
// according to default authenticator. Simply calls SetRequestAuthVia
// with nil authenticator.
func SetRequestAuth(req *http.Request) error {
return SetRequestAuthVia(req, nil)
}
func duplicateStringsSlice(in []string) []string {
return append([]string{}, in...)
}
func dupHeaders(h http.Header) http.Header {
rv := make(http.Header)
for k, v := range h {
rv[k] = duplicateStringsSlice(v)
}
return rv
}
func dupRequest(req *http.Request) *http.Request {
rv := *req
rv.Header = dupHeaders(req.Header)
rv.Trailer = dupHeaders(req.Trailer)
return &rv
}
type cbauthRoundTripper struct {
slave http.RoundTripper
a Authenticator
}
func (rt *cbauthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = dupRequest(req)
if err := SetRequestAuthVia(req, rt.a); err != nil {
return nil, err
}
return rt.slave.RoundTrip(req)
}
// WrapHTTPTransport constructs http transport that automatically does
// SetRequestAuthVia for requests it sends. As usual, if nil
// authenticator is passed, default authenticator is used.
func WrapHTTPTransport(transport http.RoundTripper, a Authenticator) http.RoundTripper {
return &cbauthRoundTripper{
slave: transport,
a: a,
}
}
// SendUnauthorized sends 401 Unauthorized response on given response writer.
func SendUnauthorized(w http.ResponseWriter) {
w.Header().Set("WWW-Authenticate", "Basic realm=\"Couchbase\"")
http.Error(w, "Authentication Failure.", http.StatusUnauthorized)
}
// ForbiddenJSON returns json 403 response for given permission
func ForbiddenJSON(permission string) ([]byte, error) {
jsonStruct := map[string]interface{}{
"message": "Forbidden. User needs one of the following permissions",
"permissions": [...]string{permission},
}
return json.Marshal(jsonStruct)
}
// SendForbidden sends 403 Forbidden with json payload that contains list
// of required permissions to response on given response writer.
func SendForbidden(w http.ResponseWriter, permission string) error {
b, err := ForbiddenJSON(permission)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
w.Write(b)
return nil
}
// CipherSuites function is deprecated. Use cbauth.GetTLSConfig() instead
func CipherSuites() []uint16 {
return []uint16{
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}
}
// CipherOrder function is deprecated. Use cbauth.GetTLSConfig() instead
func CipherOrder() bool {
return true
}
// MinTLSVersion function is deprecated. Use cbauth.GetTLSConfig() instead
func MinTLSVersion() uint16 {
return tls.VersionTLS11
}