Skip to content

Commit

Permalink
feat: add custom dial options to grpc connection (#325)
Browse files Browse the repository at this point in the history
Co-authored-by: Livio Spring <livio.a@gmail.com>
  • Loading branch information
HaimKortovich and livio-a authored Apr 22, 2024
1 parent 891b673 commit b706706
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions pkg/client/zitadel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Connection struct {
insecure bool
unaryInterceptors []grpc.UnaryClientInterceptor
streamInterceptors []grpc.StreamClientInterceptor
dialOptions []grpc.DialOption
*grpc.ClientConn
}

Expand Down Expand Up @@ -50,7 +51,7 @@ func NewConnection(issuer, api string, scopes []string, options ...Option) (*Con
c.streamInterceptors...,
),
}

dialOptions = append(dialOptions, c.dialOptions...)
opt, err := transportOption(c.api, c.insecure)
if err != nil {
return nil, err
Expand Down Expand Up @@ -107,7 +108,7 @@ func transportCredentials(api string) (credentials.TransportCredentials, error)

type Option func(*Connection) error

//WithCustomURL replaces the standard issuer (https://issuer.zitadel.ch) and api endpoint (api.zitadel.ch:443)
// WithCustomURL replaces the standard issuer (https://issuer.zitadel.ch) and api endpoint (api.zitadel.ch:443)
func WithCustomURL(issuer, api string) func(*Connection) error {
return func(client *Connection) error {
client.issuer = issuer
Expand All @@ -116,10 +117,10 @@ func WithCustomURL(issuer, api string) func(*Connection) error {
}
}

//WithKeyPath sets the path to the key.json used for the authentication
//if not set env var ZITADEL_KEY_PATH will be used
// WithKeyPath sets the path to the key.json used for the authentication
// if not set env var ZITADEL_KEY_PATH will be used
//
//Deprecated: use WithJWTProfileTokenSource(middleware.JWTProfileFromPath(keyPath)) instead
// Deprecated: use WithJWTProfileTokenSource(middleware.JWTProfileFromPath(keyPath)) instead
func WithKeyPath(keyPath string) func(*Connection) error {
return func(client *Connection) error {
client.jwtProfileTokenSource = func(issuer string, scopes []string) (oauth2.TokenSource, error) {
Expand All @@ -129,45 +130,53 @@ func WithKeyPath(keyPath string) func(*Connection) error {
}
}

//WithJWTProfileTokenSource sets the provider used for the authentication
//if not set, the key file will be read from the path set in env var ZITADEL_KEY_PATH
// WithJWTProfileTokenSource sets the provider used for the authentication
// if not set, the key file will be read from the path set in env var ZITADEL_KEY_PATH
func WithJWTProfileTokenSource(provider middleware.JWTProfileTokenSource) func(*Connection) error {
return func(client *Connection) error {
client.jwtProfileTokenSource = provider
return nil
}
}

//WithOrgID sets the organization context (where the api calls are executed)
//if not set the resource owner (organisation) of the calling user will be used
// WithOrgID sets the organization context (where the api calls are executed)
// if not set the resource owner (organisation) of the calling user will be used
func WithOrgID(orgID string) func(*Connection) error {
return func(client *Connection) error {
client.orgID = orgID
return nil
}
}

//WithInsecure disables transport security for the client connection
//use only when absolutely necessary (local development)
// WithInsecure disables transport security for the client connection
// use only when absolutely necessary (local development)
func WithInsecure() func(*Connection) error {
return func(client *Connection) error {
client.insecure = true
return nil
}
}

//WithUnaryInterceptors adds non ZITADEL specific interceptors to the connection
// WithUnaryInterceptors adds non ZITADEL specific interceptors to the connection
func WithUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) func(*Connection) error {
return func(client *Connection) error {
client.unaryInterceptors = append(client.unaryInterceptors, interceptors...)
return nil
}
}

//WithStreamInterceptors adds non ZITADEL specific interceptors to the connection
// WithStreamInterceptors adds non ZITADEL specific interceptors to the connection
func WithStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) func(*Connection) error {
return func(client *Connection) error {
client.streamInterceptors = append(client.streamInterceptors, interceptors...)
return nil
}
}

// WithDialOptions adds non ZITADEL specific dial options to the connection
func WithDialOptions(opts ...grpc.DialOption) func(*Connection) error {
return func(client *Connection) error {
client.dialOptions = append(client.dialOptions, opts...)
return nil
}
}

0 comments on commit b706706

Please sign in to comment.