Skip to content

Conversation

@francesconi
Copy link
Member

No description provided.

func OIDC(ctx context.Context, issuer string, opts ...OIDCMiddlewareOption) func(next http.Handler) http.Handler {
provider, err := oidc.NewProvider(ctx, issuer)
if err != nil {
panic(err)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I would either panic or return the middleware as a struct and an error in this case.

Copy link
Member

Choose a reason for hiding this comment

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

According to https://go.dev/doc/effective_go#panic it’s acceptable to panic() in this case because the OIDC middleware can’t set itself up. But this could also be seen from another perspective: What if it isn’t a critical error for the app if the OIDC middleware fails. For example, it could fall back to another authentication mechanism. Just hypothesizing. In that case it would be better to return the error during setup instead of panic(). What’s your take on this?

return
}

ctx := contextWithIDToken(r.Context(), idToken)
Copy link
Member

Choose a reason for hiding this comment

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

Two r.Context() calls could be stored in a variable instead.


return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I would pass that function result directly to validateAuthHeader instead of creating a dedicated variable.

Copy link
Member

@davidspiess davidspiess left a comment

Choose a reason for hiding this comment

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

Should we add a mandatory audience check to avoid potential security issues?

Copy link
Member

@TomOne TomOne left a comment

Choose a reason for hiding this comment

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

Looks good to me. The middleware is well structured and also allows to pass custom HTTP handlers for authorization errors.

@francesconi francesconi merged commit 9ff7e83 into main Jun 24, 2025
1 check passed
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.

5 participants