Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add ability to provide grpc dial options like client interceptors #358

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,31 @@ import (
"github.com/zitadel/zitadel-go/v3/pkg/zitadel"
)

type Client struct {
type clientOptions struct {
initTokenSource TokenSourceInitializer
connection *grpc.ClientConn
grpcDialOptions []grpc.DialOption
}

type Option func(*clientOptions)

// WithAuth allows to set a token source as authorization, e.g. [PAT], resp. provide an authentication mechanism,
// such as JWT Profile ([JWTAuthentication]) or Password ([PasswordAuthentication]) for service users.
func WithAuth(initTokenSource TokenSourceInitializer) Option {
return func(c *clientOptions) {
c.initTokenSource = initTokenSource
}
}

// WithGRPCDialOptions allows to use custom grpc dial options when establishing connection with Zitadel.
// Multiple calls to WithGRPCDialOptions is allowed, options will be appended.
func WithGRPCDialOptions(opts ...grpc.DialOption) Option {
return func(c *clientOptions) {
c.grpcDialOptions = append(c.grpcDialOptions, opts...)
}
}

type Client struct {
connection *grpc.ClientConn

systemService system.SystemServiceClient
adminService admin.AdminServiceClient
Expand All @@ -33,46 +55,49 @@ type Client struct {
oidcService oidc_pb.OIDCServiceClient
}

type Option func(*Client)

// WithAuth allows to set a token source as authorization, e.g. [PAT], resp. provide an authentication mechanism,
// such as JWT Profile ([JWTAuthentication]) or Password ([PasswordAuthentication]) for service users.
func WithAuth(initTokenSource TokenSourceInitializer) Option {
return func(c *Client) {
c.initTokenSource = initTokenSource
func New(ctx context.Context, zitadel *zitadel.Zitadel, opts ...Option) (*Client, error) {
var options clientOptions
for _, o := range opts {
o(&options)
}
}

func New(ctx context.Context, zitadel *zitadel.Zitadel, options ...Option) (_ *Client, err error) {
c := &Client{}
for _, option := range options {
option(c)
}
var source oauth2.TokenSource
if c.initTokenSource != nil {
source, err = c.initTokenSource(ctx, zitadel.Origin())
if options.initTokenSource != nil {
var err error
source, err = options.initTokenSource(ctx, zitadel.Origin())
if err != nil {
return nil, err
}
}
err = c.newConnection(ctx, zitadel, source)

conn, err := newConnection(ctx, zitadel, source, options.grpcDialOptions...)
if err != nil {
return nil, err
}
return c, nil

return &Client{
connection: conn,
}, nil
}

func (c *Client) newConnection(ctx context.Context, zitadel *zitadel.Zitadel, tokenSource oauth2.TokenSource) error {
func newConnection(
ctx context.Context,
zitadel *zitadel.Zitadel,
tokenSource oauth2.TokenSource,
opts ...grpc.DialOption,
) (*grpc.ClientConn, error) {
transportCreds, err := transportCredentials(zitadel.Domain(), zitadel.IsTLS())
if err != nil {
return err
return nil, err
}

dialOptions := []grpc.DialOption{
grpc.WithTransportCredentials(transportCreds),
grpc.WithPerRPCCredentials(&cred{tls: zitadel.IsTLS(), tokenSource: tokenSource}),
}
c.connection, err = grpc.DialContext(ctx, zitadel.Host(), dialOptions...)
return err
dialOptions = append(dialOptions, opts...)

return grpc.DialContext(ctx, zitadel.Host(), dialOptions...)
}

func (c *Client) SystemService() system.SystemServiceClient {
Expand Down
Loading