Skip to content

Commit 38e5381

Browse files
committed
add client support for pre-shared authorization tokens
BROKER_AUTH_TOKEN and CONSUMER_AUTH_TOKEN, when present, are used as pre-shared bearer authorization tokens submitted with client requests. This enables `gazctl` and other client applications to make use of pre-shared authorization tokens which are negotiated as part of a larger authorization context.
1 parent 6afdb8f commit 38e5381

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

auth/auth.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ func (k *KeyedAuth) Verify(ctx context.Context, require pb.Capability) (context.
7373
}
7474
}
7575

76+
// NewBearerAuth returns a BearerAuth that implements Authorizer
77+
// using a pre-shared authorization token.
78+
func NewBearerAuth(token string) *BearerAuth {
79+
return &BearerAuth{token: token}
80+
81+
}
82+
83+
// BearerAuth implements the pb.Authorizer interface.
84+
type BearerAuth struct {
85+
token string
86+
}
87+
88+
func (b *BearerAuth) Authorize(ctx context.Context, _claims pb.Claims, _exp time.Duration) (context.Context, error) {
89+
return metadata.AppendToOutgoingContext(ctx, "authorization", fmt.Sprintf("Bearer %s", b.token)), nil
90+
}
91+
7692
// NewNoopAuth returns an Authorizer and Verifier which does nothing.
7793
func NewNoopAuth() interface {
7894
pb.Authorizer

mainboilerplate/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AddressConfig struct {
2323
CertKeyFile string `long:"cert-key-file" env:"CERT_KEY_FILE" default:"" description:"Path to the client TLS private key"`
2424
TrustedCAFile string `long:"trusted-ca-file" env:"TRUSTED_CA_FILE" default:"" description:"Path to the trusted CA for client verification of server certificates"`
2525
AuthKeys string `long:"auth-keys" env:"AUTH_KEYS" description:"Whitespace or comma separated, base64-encoded keys. The first key is used to sign Authorization tokens." json:"-"`
26+
AuthToken string `long:"auth-token" env:"AUTH_TOKEN" description:"Bearer token to use for authentication." json:"-"`
2627
}
2728

2829
// MustDial dials the server address using a protocol.Dispatcher balancer, and panics on error.
@@ -65,6 +66,8 @@ func (c *AddressConfig) MustJournalClient(ctx context.Context) pb.JournalClient
6566
if c.AuthKeys != "" {
6667
authorizer, err = auth.NewKeyedAuth(c.AuthKeys)
6768
Must(err, "parsing authorization keys")
69+
} else if c.AuthToken != "" {
70+
authorizer = auth.NewBearerAuth(c.AuthToken)
6871
} else {
6972
authorizer = auth.NewNoopAuth()
7073
}
@@ -87,6 +90,8 @@ func (c *AddressConfig) MustShardClient(ctx context.Context) pc.ShardClient {
8790
if c.AuthKeys != "" {
8891
authorizer, err = auth.NewKeyedAuth(c.AuthKeys)
8992
Must(err, "parsing authorization keys")
93+
} else if c.AuthToken != "" {
94+
authorizer = auth.NewBearerAuth(c.AuthToken)
9095
} else {
9196
authorizer = auth.NewNoopAuth()
9297
}

0 commit comments

Comments
 (0)