-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_auth_static.go
53 lines (45 loc) · 1.51 KB
/
client_auth_static.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
package grpcclient
import (
"encoding/json"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var (
// StaticAuthClientCreds implements client interface to be able to WithPerRPCCredentials
_ credentials.PerRPCCredentials = (*StaticAuthClientCreds)(nil)
)
// StaticAuthClientCreds holder for client credentials
type StaticAuthClientCreds struct {
Username string
Password string
}
// GetRequestMetadata gets the request metadata as a map from StaticAuthClientCreds
func (c *StaticAuthClientCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{
"username": c.Username,
"password": c.Password,
}, nil
}
// RequireTransportSecurity indicates whether the credentials requires transport security.
// Given that people can use this with or without TLS, at the moment we are not enforcing
// transport security
func (c *StaticAuthClientCreds) RequireTransportSecurity() bool {
return false
}
// AppendStaticAuth optionally appends static auth credentials if provided.
func AppendStaticAuth(opts []grpc.DialOption, grpcAuthStaticPassword []byte) ([]grpc.DialOption, error) {
if len(grpcAuthStaticPassword) == 0 {
return opts, nil
}
clientCreds := &StaticAuthClientCreds{}
if err := json.Unmarshal(grpcAuthStaticPassword, clientCreds); err != nil {
return nil, err
}
creds := grpc.WithPerRPCCredentials(clientCreds)
opts = append(opts, creds)
return opts, nil
}
func init() {
RegisterGRPCDialOptions(AppendStaticAuth)
}