1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-05-17 15:06:47 +08:00

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>
This commit is contained in:
Henrique Dias
2023-11-17 01:29:29 +01:00
committed by GitHub
parent 0770702289
commit 01cc5eab57
12 changed files with 464 additions and 10 deletions

29
client/rpc/auth/auth.go Normal file
View File

@ -0,0 +1,29 @@
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)
}