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:
29
client/rpc/auth/auth.go
Normal file
29
client/rpc/auth/auth.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user