-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 [secret]. The given [roundTripper] is | ||
// the base [http.RoundTripper]. If it is nil, [http.DefaultTransport] is used. | ||
func NewAuthorizedRoundTripper(secret string, roundTripper http.RoundTripper) http.RoundTripper { | ||
if roundTripper == nil { | ||
roundTripper = http.DefaultTransport | ||
} | ||
|
||
return &AuthorizedRoundTripper{ | ||
authorization: secret, | ||
roundTripper: roundTripper, | ||
} | ||
} | ||
|
||
func (tp *AuthorizedRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { | ||
r.Header.Set("Authorization", tp.authorization) | ||
return tp.roundTripper.RoundTrip(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ipfs/kubo/client/rpc/auth" | ||
"github.com/ipfs/kubo/config" | ||
"github.com/ipfs/kubo/test/cli/harness" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAuth(t *testing.T) { | ||
t.Parallel() | ||
|
||
makeAndStartProtectedNode := func(t *testing.T, authorizations map[string]*config.RPCAuthScope) *harness.Node { | ||
authorizations["test-node-starter"] = &config.RPCAuthScope{ | ||
HTTPAuthSecret: "bearer:test-node-starter", | ||
AllowedPaths: []string{"/api/v0"}, | ||
} | ||
|
||
node := harness.NewT(t).NewNode().Init() | ||
node.UpdateConfig(func(cfg *config.Config) { | ||
cfg.API.Authorizations = authorizations | ||
}) | ||
node.StartDaemonWithAuthorization("Bearer test-node-starter") | ||
return node | ||
} | ||
|
||
t.Run("Follows Allowed Paths", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
node := makeAndStartProtectedNode(t, map[string]*config.RPCAuthScope{ | ||
"userA": { | ||
HTTPAuthSecret: "bearer:userAToken", | ||
AllowedPaths: []string{"/api/v0/id"}, | ||
}, | ||
}) | ||
|
||
apiClient := node.APIClient() | ||
apiClient.Client.Transport = auth.NewAuthorizedRoundTripper("Bearer userAToken", apiClient.Client.Transport) | ||
|
||
// Can access ID. | ||
resp := apiClient.Post("/api/v0/id", nil) | ||
assert.Equal(t, 200, resp.StatusCode) | ||
|
||
// But not Ping. | ||
resp = apiClient.Post("/api/v0/ping", nil) | ||
assert.Equal(t, 403, resp.StatusCode) | ||
|
||
node.StopDaemon() | ||
}) | ||
|
||
t.Run("Generic Allowed Path Gives Full Access", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
node := makeAndStartProtectedNode(t, map[string]*config.RPCAuthScope{ | ||
"userA": { | ||
HTTPAuthSecret: "bearer:userAToken", | ||
AllowedPaths: []string{"/api/v0"}, | ||
}, | ||
}) | ||
|
||
apiClient := node.APIClient() | ||
apiClient.Client.Transport = auth.NewAuthorizedRoundTripper("Bearer userAToken", apiClient.Client.Transport) | ||
|
||
resp := apiClient.Post("/api/v0/id", nil) | ||
assert.Equal(t, 200, resp.StatusCode) | ||
|
||
node.StopDaemon() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters