Skip to content

Commit

Permalink
linting fix
Browse files Browse the repository at this point in the history
Signed-off-by: chahatsagarmain <chahatsagar2003@gmail.com>
  • Loading branch information
chahatsagarmain committed Oct 16, 2024
1 parent 4611214 commit 05d95d4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pkg/bearertoken/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package bearertoken
import (
"context"
"fmt"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -37,10 +38,10 @@ func getValidBearerToken(ctx context.Context, bearerHeader string) (string, erro
func tokenFromMetadata(md metadata.MD, bearerHeader string) (string, error) {
bearerToken := md.Get(bearerHeader)
if len(bearerToken) < 1 {
return "", fmt.Errorf("malformed token: no token found")
return "", nil
}
if len(bearerToken) > 1 {
return "", fmt.Errorf("malformed token: multiple tokens found")
return "", fmt.Errorf("malformed token: multiple tokens found")

Check warning on line 44 in pkg/bearertoken/grpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/bearertoken/grpc.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}
return bearerToken[0], nil

Check warning on line 46 in pkg/bearertoken/grpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/bearertoken/grpc.go#L46

Added line #L46 was not covered by tests
}
Expand Down Expand Up @@ -84,14 +85,13 @@ func NewUnaryClientInterceptor() grpc.UnaryClientInterceptor {
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {

md, _ := metadata.FromOutgoingContext(ctx)
if len(md[Key]) == 0 {
if len(md[Key]) == 0 {
bearerToken, err := GetBearerToken(ctx)
if err != false {
if err {
return invoker(ctx, method, req, reply, cc, opts...) // Proceed without a token if there's an error

Check warning on line 92 in pkg/bearertoken/grpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/bearertoken/grpc.go#L92

Added line #L92 was not covered by tests
}

if bearerToken != "" {
ctx = metadata.AppendToOutgoingContext(ctx, Key, bearerToken)

Check warning on line 96 in pkg/bearertoken/grpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/bearertoken/grpc.go#L96

Added line #L96 was not covered by tests
}
Expand All @@ -100,6 +100,7 @@ func NewUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return invoker(ctx, method, req, reply, cc, opts...)
})
}

// NewStreamClientInterceptor injects the bearer token header into gRPC request metadata.
func NewStreamClientInterceptor() grpc.StreamClientInterceptor {
return grpc.StreamClientInterceptor(func(
Expand All @@ -113,15 +114,15 @@ func NewStreamClientInterceptor() grpc.StreamClientInterceptor {
md, _ := metadata.FromOutgoingContext(ctx)
if len(md[Key]) == 0 {
bearerToken, err := GetBearerToken(ctx)
if err != false {
return streamer(ctx, desc, cc, method, opts...)
if err {
return streamer(ctx, desc, cc, method, opts...)

Check warning on line 118 in pkg/bearertoken/grpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/bearertoken/grpc.go#L118

Added line #L118 was not covered by tests
}

if bearerToken != "" {
ctx = metadata.AppendToOutgoingContext(ctx, Key, bearerToken)

Check warning on line 122 in pkg/bearertoken/grpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/bearertoken/grpc.go#L122

Added line #L122 was not covered by tests
}
}

return streamer(ctx, desc, cc, method, opts...)
})
}
}

0 comments on commit 05d95d4

Please sign in to comment.