-
Notifications
You must be signed in to change notification settings - Fork 151
/
basic_auth.go
45 lines (37 loc) · 978 Bytes
/
basic_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
package http
import (
"context"
"encoding/base64"
"fmt"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
func init() {
runtime.RegisterPackageValue("http", "basicAuth", basicAuthFunc)
}
const (
basicAuthUsernameArg = "u"
basicAuthPasswordArg = "p"
)
var basicAuthFunc = values.NewFunction(
"basicAuth",
runtime.MustLookupBuiltinType("http", "basicAuth"),
func(ctx context.Context, args values.Object) (values.Value, error) {
return interpreter.DoFunctionCall(BasicAuth, args)
},
false,
)
func BasicAuth(args interpreter.Arguments) (values.Value, error) {
u, err := args.GetRequiredString(basicAuthUsernameArg)
if err != nil {
return nil, err
}
p, err := args.GetRequiredString(basicAuthPasswordArg)
if err != nil {
return nil, err
}
combined := fmt.Sprintf("%s:%s", u, p)
v := base64.StdEncoding.EncodeToString([]byte(combined))
return values.NewString("Basic " + v), nil
}