coreos / go-oidc

A Go OpenID Connect client.
Apache License 2.0
1.99k stars 400 forks source link

Allow Header Injection #425

Closed spjmurray closed 7 months ago

spjmurray commented 8 months ago

Use case is for tracing/performance profiling through a distributed system, in particular the W3C trace context standard used by open-telemetry.

spjmurray commented 8 months ago

Open for discussion as always! Using it is as simple as

import (
       "go.opentelemetry.io/otel"
       "go.opentelemetry.io/otel/propagation"
)

func foo(ctx context.Context) {
       ctx = oidc.HeaderInjectClientContext(ctx, func(h http.Header) {
               otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(h))
       })
       // Provider() or Userinfo() call
}

And it works quite nicely!

Screenshot from 2024-04-04 14-25-57

spjmurray commented 7 months ago

Bump. Someone, anyone? I ask not much, I promise thee! I even offer beer....

ericchiang commented 7 months ago

Thanks for the ping, sorry about that!

In my mind this is what accepting a custom http.Client with a custom transport is for. Go's oauth2 package does something similar to set the bearer token auth header.

Does that work for you? I imagine that strategy would avoid having to make change to other libraries as well.

Do you have examples of how other packages are supporting this?

spjmurray commented 7 months ago

Ah, that's how I missed it, it's not a callback but a receiver, my bad! Does make the code somewhat less legible, but that's just Go for you 😿

spjmurray commented 7 months ago

Okay, finally got a chance to test it out, and it works! Cheers