-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat(rpc): Opt-in HTTP RPC API Authorization #10218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8164c72
feat(rpc): optionally protect with Authorization header
hacdias 7ff3dd9
refactor: use more flexible secrets
hacdias 9fff012
refactor: return error if invalid
hacdias 9321394
feat: add two auth tests
hacdias d2e7435
feat: whitelist version, add cli test
hacdias 74ad40c
docs: ipfs daemon informs about API.Authorizations
lidel a8966e1
chore: feedback, tests, changelog, docs
hacdias 082fde3
changelog: add missing header
hacdias 8f24bb7
docs: API.Authorizations
lidel 6acfaf7
test: negative tests for API.Authorizations
lidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 [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) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,63 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
) | ||
|
||
const ( | ||
APITag = "API" | ||
AuthorizationTag = "Authorizations" | ||
) | ||
|
||
type RPCAuthScope struct { | ||
// AuthSecret is the secret that will be compared to the HTTP "Authorization". | ||
// header. A secret is in the format "type:value". Check the documentation for | ||
// supported types. | ||
AuthSecret string | ||
|
||
// AllowedPaths is an explicit list of RPC path prefixes to allow. | ||
// By default, none are allowed. ["/api/v0"] exposes all RPCs. | ||
AllowedPaths []string | ||
} | ||
|
||
type API struct { | ||
HTTPHeaders map[string][]string // HTTP headers to return with the API. | ||
// HTTPHeaders are the HTTP headers to return with the API. | ||
HTTPHeaders map[string][]string | ||
|
||
// Authorization is a map of authorizations used to authenticate in the API. | ||
// If the map is empty, then the RPC API is exposed to everyone. Check the | ||
// documentation for more details. | ||
Authorizations map[string]*RPCAuthScope `json:",omitempty"` | ||
} | ||
|
||
// ConvertAuthSecret converts the given secret in the format "type:value" into an | ||
// HTTP Authorization header value. It can handle 'bearer' and 'basic' as type. | ||
// If type exists and is not known, an empty string is returned. If type does not | ||
// exist, 'bearer' type is assumed. | ||
func ConvertAuthSecret(secret string) string { | ||
if secret == "" { | ||
return secret | ||
} | ||
|
||
split := strings.SplitN(secret, ":", 2) | ||
if len(split) < 2 { | ||
// No prefix: assume bearer token. | ||
return "Bearer " + secret | ||
} | ||
|
||
if strings.HasPrefix(secret, "basic:") { | ||
if strings.Contains(split[1], ":") { | ||
// Assume basic:user:password | ||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(split[1])) | ||
} else { | ||
// Assume already base64 encoded. | ||
return "Basic " + split[1] | ||
} | ||
} else if strings.HasPrefix(secret, "bearer:") { | ||
return "Bearer " + split[1] | ||
} | ||
|
||
// Unknown. Type is present, but we can't handle it. | ||
return "" | ||
} |
This file contains hidden or 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,22 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertAuthSecret(t *testing.T) { | ||
for _, testCase := range []struct { | ||
input string | ||
output string | ||
}{ | ||
{"", ""}, | ||
{"someToken", "Bearer someToken"}, | ||
{"bearer:someToken", "Bearer someToken"}, | ||
{"basic:user:pass", "Basic dXNlcjpwYXNz"}, | ||
{"basic:dXNlcjpwYXNz", "Basic dXNlcjpwYXNz"}, | ||
} { | ||
assert.Equal(t, testCase.output, ConvertAuthSecret(testCase.input)) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.