Skip to content

Support connect-go v2 - #32

Open
emcfarlane wants to merge 3 commits into
mainfrom
ed/connect-v2-interceptor
Open

emcfarlane wants to merge 3 commits into
mainfrom
ed/connect-v2-interceptor

Conversation

@emcfarlane

@emcfarlane emcfarlane commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

This adds support for connect-go v2. Updates the API to return a connect.ServerInterceptor, with an InterceptorAuthFunc to replace AuthFunc for the interceptor, and helper for getting the BearerTokenFromHeader.

type InterceptorAuthFunc func(ctx context.Context, info *connect.CallInfo) (any, error)
func NewServerInterceptor(auth InterceptorAuthFunc) connect.ServerInterceptor
func BearerTokenFromHeader(header interface{ Get(key string) string }) (string, bool)

The module now depends on connectrpc.com/connect/v2 as well as v1. This aligns with the pre v1 libraries. Examples are updated to use the new v2 APIs.

Closes #31

Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
Signed-off-by: Edward McFarlane <emcfarlane@buf.build>

@jferrl jferrl left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read this against our migration. NewServerInterceptor matches the interceptor we wrote, and CallInfo has everything our auth funcs read (procedure and headers).

I haven't run it yet. The comments below come from reading this branch next to connect v2.0.2. The one on NewMiddleware is what keeps #31 open for us.

Comment thread interceptor.go Outdated
}
authInfo, err := auth(ctx, callInfo)
if err != nil {
return err

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An auth func that still calls authn.Errorf compiles here and answers unknown (HTTP 500) with no message. The v1 *connect.Error isn't a v2 one, so scrubHandlerError in connecthttp treats it as an uncoded error.

The call is still rejected, but a client waiting for a 401 won't refresh its token. Could the interceptor convert a v1 error to v2 before returning it? The module imports both now.

Comment thread interceptor.go
// is attached to the context and may be retrieved with [GetInfo].
//
// For connect-go v1, use [NewMiddleware].
func NewServerInterceptor(auth InterceptorAuthFunc) connect.ServerInterceptor {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things change for someone moving from Wrap(mux). I think the doc should say both.

  1. Only registered procedures are authenticated. An unknown procedure is answered unimplemented without running the chain, unless SetUnknownHandler is set. connecthttp also answers 405, 415 and a bad timeout or compression before the interceptor runs. With the middleware an unauthenticated caller got 401 for all of these. Now they can tell which methods exist.
  2. It only protects what comes after it in NewServer. An interceptor placed earlier runs unauthenticated. If that one reads the message, like validation, decode-before-auth is back.

Comment thread README.md
complex, but [pkg.go.dev][godoc] includes a complete example.
Cookie and basic authentication is similar. Mutual TLS is a bit more
complex, but [pkg.go.dev][godoc] includes a complete example. For connect-go
v1, wrap the HTTP handler with `authn.NewMiddleware` instead.

@jferrl jferrl Nov 12, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A v2 server still needs NewMiddleware for what the interceptor doesn't cover: plain HTTP routes on the same mux, or a 401 before routing.

It still writes errors with the v1 ErrorWriter, so an AuthFunc that returns a v2 error answers unknown (500). That was the first point in #31. Could Wrap use connecthttp.NewErrorWriter when it gets a v2 error?

Comment thread interceptor_test.go
if authorization != "" {
info.RequestHeader().Set("Authorization", authorization)
}
return server.Call(t.Context(), procedure, info, nil)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This calls server.Call in process, so it doesn't cover the HTTP status, or that auth runs before the body is read. A case over httptest for the 401, one streaming RPC and one unknown procedure would pin that down.

The examples were also the only end-to-end tests of the v1 middleware with a real Connect handler. They are v2 only now.

Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support connect-go v2

2 participants