Skip to content

Commit

Permalink
feat: options to add grpc interceptors (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerhurst authored Apr 22, 2022
1 parent 4649a89 commit 2ddcb26
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions pkg/client/zitadel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Connection struct {
scopes []string
orgID string
insecure bool
unaryInterceptors []grpc.UnaryClientInterceptor
streamInterceptors []grpc.StreamClientInterceptor
*grpc.ClientConn
}

Expand All @@ -37,18 +39,19 @@ func NewConnection(scopes []string, options ...Option) (*Connection, error) {
}
}

unaryInterceptors, streamInterceptors, err := interceptors(c.issuer, c.orgID, c.scopes, c.jwtProfileTokenSource)
err := c.setInterceptors(c.issuer, c.orgID, c.scopes, c.jwtProfileTokenSource)
if err != nil {
return nil, err
}
dialOptions := []grpc.DialOption{
grpc.WithChainUnaryInterceptor(
unaryInterceptors...,
c.unaryInterceptors...,
),
grpc.WithChainStreamInterceptor(
streamInterceptors...,
c.streamInterceptors...,
),
}

opt, err := transportOption(c.api, c.insecure)
if err != nil {
return nil, err
Expand All @@ -63,19 +66,20 @@ func NewConnection(scopes []string, options ...Option) (*Connection, error) {
return c, nil
}

func interceptors(issuer, orgID string, scopes []string, jwtProfileTokenSource middleware.JWTProfileTokenSource) ([]grpc.UnaryClientInterceptor, []grpc.StreamClientInterceptor, error) {
func (c *Connection) setInterceptors(issuer, orgID string, scopes []string, jwtProfileTokenSource middleware.JWTProfileTokenSource) error {
auth, err := middleware.NewAuthenticator(issuer, jwtProfileTokenSource, scopes...)
if err != nil {
return nil, nil, err
return err
}
unaryInterceptors := []grpc.UnaryClientInterceptor{auth.Unary()}
streamInterceptors := []grpc.StreamClientInterceptor{auth.Stream()}

c.unaryInterceptors = append(c.unaryInterceptors, auth.Unary())
c.streamInterceptors = append(c.streamInterceptors, auth.Stream())
if orgID != "" {
org := middleware.NewOrgInterceptor(orgID)
unaryInterceptors = append(unaryInterceptors, org.Unary())
streamInterceptors = append(streamInterceptors, org.Stream())
c.unaryInterceptors = append(c.unaryInterceptors, org.Unary())
c.streamInterceptors = append(c.streamInterceptors, org.Stream())
}
return unaryInterceptors, streamInterceptors, nil
return nil
}

func transportOption(api string, insecure bool) (grpc.DialOption, error) {
Expand Down Expand Up @@ -152,3 +156,19 @@ func WithInsecure() func(*Connection) error {
return nil
}
}

//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
func WithStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) func(*Connection) error {
return func(client *Connection) error {
client.streamInterceptors = append(client.streamInterceptors, interceptors...)
return nil
}
}

0 comments on commit 2ddcb26

Please sign in to comment.