mirror of
https://github.com/grafana/grafana.git
synced 2024-11-27 03:11:01 -06:00
24 lines
759 B
Go
24 lines
759 B
Go
package aztokenprovider
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
)
|
|
|
|
const authenticationMiddlewareName = "AzureAuthentication"
|
|
|
|
func AuthMiddleware(tokenProvider AzureTokenProvider) httpclient.Middleware {
|
|
return httpclient.NamedMiddlewareFunc(authenticationMiddlewareName, func(opts httpclient.Options, next http.RoundTripper) http.RoundTripper {
|
|
return httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
token, err := tokenProvider.GetAccessToken(req.Context())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve Azure access token: %w", err)
|
|
}
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
return next.RoundTrip(req)
|
|
})
|
|
})
|
|
}
|