mirror of https://github.com/grafana/grafana.git
32 lines
788 B
Go
32 lines
788 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
|
|
|
"github.com/grafana/authlib/authn"
|
|
)
|
|
|
|
type authRoundTripper struct {
|
|
tokenExchangeClient *authn.TokenExchangeClient
|
|
transport http.RoundTripper
|
|
}
|
|
|
|
func (t *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
tokenResponse, err := t.tokenExchangeClient.Exchange(req.Context(), authn.TokenExchangeRequest{
|
|
Audiences: []string{"folder.grafana.app"},
|
|
Namespace: "*",
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to exchange token: %w", err)
|
|
}
|
|
|
|
// clone the request as RTs are not expected to mutate the passed request
|
|
req = utilnet.CloneRequest(req)
|
|
|
|
req.Header.Set("X-Access-Token", "Bearer "+tokenResponse.Token)
|
|
return t.transport.RoundTrip(req)
|
|
}
|