-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add support for token passed Authorization Bearer header #5397
Conversation
045a6db
to
0dac0b0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@uepoch this is great, thank you for doing it! Just a couple of little things.
http/handler.go
Outdated
if v := r.Header.Get("Authorization"); v != "" { | ||
// Reference for Authorization header format: https://tools.ietf.org/html/rfc7236#section-3 | ||
|
||
if !strings.HasPrefix(v, "Bearer") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RFC 6750 gives only one example of value v
: Bearer mF_9.B5f-4.1JqM
. I wonder if it would be safe to check for "Bearer "
here.
http/handler.go
Outdated
if !strings.HasPrefix(v, "Bearer") { | ||
return "", fmt.Errorf("the Authorization header scheme should be Bearer") | ||
} | ||
vals := strings.Split(strings.TrimSpace(strings.TrimPrefix(v, "Bearer")), " ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we checked for the full "Bearer " prefix above with a space, could this just become return v[7:], nil
? https://play.golang.org/p/Nttq9bx15lu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One might send space separated value (not that it would make sense tho)
Still have to split(s, ' ')[0]
but if the aim is to optimize, we can probably check for the presence of a space in the string without allocation, return an error if present, return value if not
https://play.golang.org/p/NgRoBv-8Y1N
This address the comment of Calvin too I believe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the PR with a new version, with less allocations
http/handler_test.go
Outdated
} | ||
|
||
rWithVault, err := http.NewRequest("GET", "v1/test/path", nil) | ||
rWithVault.Header.Set(consts.AuthHeaderName, token) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be moved down to after the above error is checked? In case rWithVault
is nil due to an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
http/handler.go
Outdated
return "", fmt.Errorf("the Authorization header scheme should be Bearer") | ||
} | ||
vals := strings.Split(strings.TrimSpace(strings.TrimPrefix(v, "Bearer")), " ") | ||
if len(vals) < 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking with len(vals) != 1
would also guard against (invalid formatted) tokens that might contain spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making those changes, would you mind adding Docs in a separate PR?
Ok, I'll make the documentation PR this week, I've been busy on another project lately; sorry for the delay! |
Fixes: #5396
Similar to what has been done in hashicorp/consul#4502
Most credits go to @mbag for the consul PR
This let users pass their token as an Authorization: Bearer XXX header RFC-6750, which is an Oauth standard.
Some software only can be configured to use Authorization headers, and don't let users specify arbitrary (i.e X-Vault-Token) header names.
Doc will be further updated by this week in the PR