Skip to content

Commit

Permalink
Clean up codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Nov 1, 2024
1 parent 035634b commit e68cb71
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
5 changes: 5 additions & 0 deletions client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func NewAuthInterceptor(apiKey, token string) *AuthInterceptor {
}
}

// SetToken sets the token.
func (i *AuthInterceptor) SetToken(token string) {
i.token = token
}

// WrapUnary creates a unary server interceptor for authorization.
func (i *AuthInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return func(
Expand Down
23 changes: 7 additions & 16 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type Client struct {
client v1connect.YorkieServiceClient
options Options
clientOptions []connect.ClientOption
interceptor *AuthInterceptor
logger *zap.Logger

id *time.ActorID
Expand Down Expand Up @@ -149,8 +150,8 @@ func New(opts ...Option) (*Client, error) {
}

var clientOptions []connect.ClientOption

clientOptions = append(clientOptions, connect.WithInterceptors(NewAuthInterceptor(options.APIKey, options.Token)))
interceptor := NewAuthInterceptor(options.APIKey, options.Token)
clientOptions = append(clientOptions, connect.WithInterceptors(interceptor))
if options.MaxCallRecvMsgSize != 0 {
clientOptions = append(clientOptions, connect.WithReadMaxBytes(options.MaxCallRecvMsgSize))
}
Expand All @@ -169,6 +170,7 @@ func New(opts ...Option) (*Client, error) {
clientOptions: clientOptions,
options: options,
logger: logger,
interceptor: interceptor,

key: k,
status: deactivated,
Expand All @@ -183,7 +185,6 @@ func Dial(rpcAddr string, opts ...Option) (*Client, error) {
return nil, err
}

cli.options.RPCAddress = rpcAddr
if err := cli.Dial(rpcAddr); err != nil {
return nil, err
}
Expand All @@ -206,19 +207,9 @@ func (c *Client) Dial(rpcAddr string) error {
return nil
}

// SetToken updates the client's token for reauthentication purposes.
func (c *Client) SetToken(token string) error {
newClientOptions := []connect.ClientOption{
connect.WithInterceptors(NewAuthInterceptor(c.options.APIKey, token)),
}
if c.options.MaxCallRecvMsgSize != 0 {
newClientOptions = append(newClientOptions,
connect.WithReadMaxBytes(c.options.MaxCallRecvMsgSize))
}
c.clientOptions = newClientOptions

c.conn.CloseIdleConnections()
return c.Dial(c.options.RPCAddress)
// SetToken sets the given token of this client.
func (c *Client) SetToken(token string) {
c.interceptor.SetToken(token)
}

// Close closes all resources of this client.
Expand Down
3 changes: 0 additions & 3 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ type Options struct {
// CertFile is the path to the certificate file.
CertFile string

// RPCAddress is the address of the RPC server.
RPCAddress string

// ServerNameOverride is the server name override.
ServerNameOverride string

Expand Down
19 changes: 8 additions & 11 deletions test/integration/auth_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ func TestAuthWebhookCache(t *testing.T) {
}

func TestAuthWebhookNewToken(t *testing.T) {
t.Run("set new token when receiving invalid token test", func(t *testing.T) {
t.Run("set valid token after invalid token test", func(t *testing.T) {
ctx := context.Background()
authServer, validToken := newAuthServer(t)

Expand Down Expand Up @@ -636,15 +636,12 @@ func TestAuthWebhookNewToken(t *testing.T) {
defer func() { assert.NoError(t, cli.Close()) }()

err = cli.Activate(ctx)
// reactivate with new token
if err != nil {
metadata := converter.ErrorMetadataOf(err)
if metadata["reason"] == "invalid token" {
err = cli.SetToken(validToken)
assert.NoError(t, err)
err = cli.Activate(ctx)
assert.NoError(t, err)
}
}
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))

// activate again with valid token
metadata := converter.ErrorMetadataOf(err)
assert.Equal(t, "invalid token", metadata["reason"])
cli.SetToken(validToken)
assert.NoError(t, cli.Activate(ctx))
})
}

0 comments on commit e68cb71

Please sign in to comment.