Skip to content

Commit cc30806

Browse files
authored
Export TokenAuth secure so it can be used elsewhere (textileio#541)
* export secure so it can be used elsewhere Signed-off-by: Aaron Sutula <hi@asutula.com> * export method to create client conn Signed-off-by: Aaron Sutula <hi@asutula.com>
1 parent f49dc03 commit cc30806

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

api/client/client.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const AuthKey = ctxKey("ffstoken")
3737

3838
// TokenAuth provides token based auth.
3939
type TokenAuth struct {
40-
secure bool
40+
Secure bool
4141
}
4242

4343
// GetRequestMetadata returns request metadata that includes the auth token.
@@ -52,11 +52,11 @@ func (t TokenAuth) GetRequestMetadata(ctx context.Context, _ ...string) (map[str
5252

5353
// RequireTransportSecurity specifies if the connection should be secure.
5454
func (t TokenAuth) RequireTransportSecurity() bool {
55-
return t.secure
55+
return t.Secure
5656
}
5757

58-
// NewClient creates a client.
59-
func NewClient(target string, optsOverrides ...grpc.DialOption) (*Client, error) {
58+
// CreateClientConn creates a gRPC connection with sensible defaults and the provided overrides.
59+
func CreateClientConn(target string, optsOverrides ...grpc.DialOption) (*grpc.ClientConn, error) {
6060
var creds credentials.TransportCredentials
6161
if strings.Contains(target, "443") {
6262
creds = credentials.NewTLS(&tls.Config{})
@@ -66,7 +66,7 @@ func NewClient(target string, optsOverrides ...grpc.DialOption) (*Client, error)
6666
var opts []grpc.DialOption
6767
if creds != nil {
6868
opts = append(opts, grpc.WithTransportCredentials(creds))
69-
auth.secure = true
69+
auth.Secure = true
7070
} else {
7171
opts = append(opts, grpc.WithInsecure())
7272
}
@@ -78,6 +78,15 @@ func NewClient(target string, optsOverrides ...grpc.DialOption) (*Client, error)
7878
if err != nil {
7979
return nil, err
8080
}
81+
return conn, nil
82+
}
83+
84+
// NewClient creates a client.
85+
func NewClient(target string, optsOverrides ...grpc.DialOption) (*Client, error) {
86+
conn, err := CreateClientConn(target, optsOverrides...)
87+
if err != nil {
88+
return nil, err
89+
}
8190
client := &Client{
8291
Asks: &Asks{client: askRpc.NewRPCServiceClient(conn)},
8392
Miners: &Miners{client: minerRpc.NewRPCServiceClient(conn)},

0 commit comments

Comments
 (0)