1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-24 02:20:52 +08:00
Files
kubo/client/rpc/auth/auth.go
Henrique Dias 01cc5eab57 feat(rpc): Opt-in HTTP RPC API Authorization (#10218)
Context: https://github.com/ipfs/kubo/issues/10187
Co-authored-by: Marcin Rataj <lidel@lidel.org>
2023-11-17 01:29:29 +01:00

30 lines
885 B
Go

package auth
import "net/http"
var _ http.RoundTripper = &AuthorizedRoundTripper{}
type AuthorizedRoundTripper struct {
authorization string
roundTripper http.RoundTripper
}
// NewAuthorizedRoundTripper creates a new [http.RoundTripper] that will set the
// Authorization HTTP header with the value of [authorization]. The given [roundTripper] is
// the base [http.RoundTripper]. If it is nil, [http.DefaultTransport] is used.
func NewAuthorizedRoundTripper(authorization string, roundTripper http.RoundTripper) http.RoundTripper {
if roundTripper == nil {
roundTripper = http.DefaultTransport
}
return &AuthorizedRoundTripper{
authorization: authorization,
roundTripper: roundTripper,
}
}
func (tp *AuthorizedRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("Authorization", tp.authorization)
return tp.roundTripper.RoundTrip(r)
}