Skip to content
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

401 authorization UI then restart request/save successful auth creds #10328

Merged
merged 1 commit into from Apr 15, 2016

Conversation

@DDEFISHER
Copy link
Contributor

DDEFISHER commented Apr 1, 2016

Step 7 of the NCSU student project Implement HTTP authorization UI

make an authorization UI appear when a 401 HTTP response is received (StatusCode::Unauthorized) - in load in http_loader.rs, right before trying to process an HTTP redirection, use the new tinyfiledialogs library to make two prompts appear (username and password), then restart the request with the new authorization value present applied. If an authorization value was present and the response is successful, add the credentials to the authorization cache.


This change is Reviewable

@highfive
Copy link

highfive commented Apr 1, 2016

Heads up! This PR modifies the following files:

  • @KiChjang: components/net/Cargo.toml, components/net/lib.rs, components/net/http_loader.rs
@highfive
Copy link

highfive commented Apr 1, 2016

warning Warning warning

  • These commits modify net code, but no tests are modified. Please consider adding a test!
@jdm jdm added the S-needs-rebase label Apr 1, 2016
@jdm jdm assigned jdm and unassigned nox Apr 1, 2016
@jdm
Copy link
Member

jdm commented Apr 1, 2016

Great! I've suggested some ways to restructure the code to make it testable as well as avoid potentially including authorization headers when they aren't necessary.
-S-awaiting-review +S-needs-code-changes +S-tests-failed


Reviewed 5 of 5 files at r1.
Review status: all files reviewed at latest revision, 11 unresolved discussions, some commit checks failed.


vi, line 0 [r1] (raw file):
Remove this file, please.


components/net/http_loader.rs, line 690 [r1] (raw file):
Let's add a UIProvider trait that has a single method that duplicates the API for tinyfiledialogs::input_box. Then we can write:

fn load<A,B>(mut load_data: LoadData,
             ui_provider: &B,
             ...)
             -> ... where B: UIProvider

and write a unit test that verifies that this code works. load_for_consumer can use load::<WrappedHttpRequest, TFDProvider> where TFDProvider is an empty struct that implements the UIProvider trait.


components/net/http_loader.rs, line 768 [r1] (raw file):
Why the iters == 1? This prevents us from authenticating to any page that followed an HTTP redirection.


components/net/http_loader.rs, line 773 [r1] (raw file):
If no username is returned we shouldn't try to provide any credentials at all.


components/net/http_loader.rs, line 779 [r1] (raw file):
Instead of choosing a default (which would prevent us from accessing sites that have no password), we should use the Option that's returned already, since the Basic password field is already an Option.


components/net/http_loader.rs, line 782 [r1] (raw file):
Rather than directly modifying the preserved_headers, let's create a local variable in this function of type Option<Authorization>. Right before making the request, we can set the header appropriately if the variable is a Some value, and we can clear the variable right before this if block.


components/net/http_loader.rs, line 788 [r1] (raw file):
nit: let's remove this newline.


components/net/http_loader.rs, line 792 [r1] (raw file):
nit: these lines should only be indented four spaces past the let.


components/net/http_loader.rs, line 793 [r1] (raw file):
nit: this should be aligned with the let.


components/net/http_loader.rs, line 798 [r1] (raw file):
nit: add a newline above this line.


components/servo/Cargo.lock, line 2085 [r1] (raw file):
You'll need to run ./mach cargo-update -p net to ensure the other Cargo.lock files are updated as well.


Comments from the review on Reviewable.io

@DDEFISHER
Copy link
Contributor Author

DDEFISHER commented Apr 12, 2016

I am having trouble coming up with the unit test. I can make a mock request with a 401, however I do not know how to check the headers in the request create function and change the mock request to a non 401 after the headers have been set. What I have started for the unit test is in this pr.

@DDEFISHER
Copy link
Contributor Author

DDEFISHER commented Apr 12, 2016

I just realized I should not be checking the response headers in the unit test but instead maybe the response status code

@jdm
Copy link
Member

jdm commented Apr 12, 2016

That is correct.

@jdm
Copy link
Member

jdm commented Apr 12, 2016

Looking good! I've laid out how I think the unit test can be implmented; let me know if anything's unclear!
-S-awaiting-review +S-needs-code-changes


Reviewed 5 of 5 files at r2.
Review status: all files reviewed at latest revision, 12 unresolved discussions, some commit checks failed.


components/net/http_loader.rs, line 145 [r2] (raw file):
nit: no need for the {};


components/net/http_loader.rs, line 694 [r2] (raw file):
Let's combine these into a single input_username_and_password that returns (Option<String>, Option<String>).


components/net/http_loader.rs, line 715 [r2] (raw file):
This structure belongs in the unit test file instead.


components/net/http_loader.rs, line 719 [r2] (raw file):
This can be just struct TFDProvider;


components/net/http_loader.rs, line 724 [r2] (raw file):
nit: let's indent the rest of these arguments to match the first one.


components/net/http_loader.rs, line 800 [r2] (raw file):

if let Some(ref auth_header) = new_auth_header {
    request_headers.set(auth_header.clone());
}

components/net/http_loader.rs, line 815 [r2] (raw file):
Instead of retrying (ie. returning to the start of the loop), we should allow the regular code path to continue here without trying to gather any further authorization information.


components/net/http_loader.rs, line 829 [r2] (raw file):
We still need to set new_auth_header back to None after the previous if block.


tests/unit/net/http_loader.rs, line 102 [r2] (raw file):
We shouldn't be relying on a Location header for the test, since real servers don't provide one.


tests/unit/net/http_loader.rs, line 1576 [r2] (raw file):
This is a good start, but we'll need to do something a bit more complex for this test. We'll need to create a type which implements HttpRequest and stores a Headers member (see AssertRequestMustHaveHeaders for an example). In its send implementation, it should check for the presence of the Authorization header containing the appropriate username and password; if it's present, it should return a successful MockResponse value, and if not, it should return the 401 response. This factory should return an instance of the new type. Does that make sense?


tests/unit/net/http_loader.rs, line 1608 [r2] (raw file):
The goal of this test is to verify that when a server replies with a 401 response, our "user" (ie. the TestProvider) provides the right credentials that it's expecting, and the actual response that we get from load is a 200 success response.


Comments from Reviewable

@DDEFISHER
Copy link
Contributor Author

DDEFISHER commented Apr 14, 2016

Should I wait for #10555 before going further with the unit test stuff?

@jdm
Copy link
Member

jdm commented Apr 14, 2016

No, I was intending to hold off merging that until after these changes.

@DDEFISHER
Copy link
Contributor Author

DDEFISHER commented Apr 14, 2016

This new unit test makes sure a auth header is set with the ui on a 401 response. It does not explicitly check if the response was a 200, however the new testing type returns a 200 response once the auth header is set with the correct credentials. I am not sure if that is sufficient or not.

@jdm
Copy link
Member

jdm commented Apr 14, 2016

This looks great! Please do check that the response we end up with in the test is the one we expect, but otherwise this is ready to merge after a rebase and squash.
+S-needs-squash -S-needs-rebase +S-needs-code-changes


Reviewed 2 of 2 files at r4.
Review status: all files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


components/net/http_loader.rs, line 796 [r4] (raw file):
This isn't necessary any more; we can use name directly inside the match.


tests/unit/net/http_loader.rs, line 1652 [r4] (raw file):
We should assert that the response's status is a success code.


Comments from Reviewable

@DDEFISHER
Copy link
Contributor Author

DDEFISHER commented Apr 14, 2016

rebasing now

@KiChjang
Copy link
Member

KiChjang commented Apr 15, 2016

@bors-servo r=jdm

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

📌 Commit c73edcb has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

Testing commit c73edcb with merge 91597b6...

bors-servo added a commit that referenced this pull request Apr 15, 2016
401 authorization UI then restart request/save successful auth creds

Step 7 of the NCSU student project Implement HTTP authorization UI

> make an authorization UI appear when a 401 HTTP response is received (StatusCode::Unauthorized) - in load in http_loader.rs, right before trying to process an HTTP redirection, use the new tinyfiledialogs library to make two prompts appear (username and password), then restart the request with the new authorization value present applied. If an authorization value was present and the response is successful, add the credentials to the authorization cache.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10328)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

💔 Test failed - mac-dev-unit

@DDEFISHER
Copy link
Contributor Author

DDEFISHER commented Apr 15, 2016

ran cargo-update -p net

@KiChjang
Copy link
Member

KiChjang commented Apr 15, 2016

@bors-servo r=jdm

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

📌 Commit b0e1f10 has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

Testing commit b0e1f10 with merge 7bd2381...

bors-servo added a commit that referenced this pull request Apr 15, 2016
401 authorization UI then restart request/save successful auth creds

Step 7 of the NCSU student project Implement HTTP authorization UI

> make an authorization UI appear when a 401 HTTP response is received (StatusCode::Unauthorized) - in load in http_loader.rs, right before trying to process an HTTP redirection, use the new tinyfiledialogs library to make two prompts appear (username and password), then restart the request with the new authorization value present applied. If an authorization value was present and the response is successful, add the credentials to the authorization cache.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10328)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

💔 Test failed - linux-rel

@jdm
Copy link
Member

jdm commented Apr 15, 2016

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

Previous build results for android, arm32, arm64, linux-dev, mac-dev-unit, mac-rel-css, mac-rel-wpt are reusable. Rebuilding only linux-rel...

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

@bors-servo bors-servo merged commit b0e1f10 into servo:master Apr 15, 2016
2 of 3 checks passed
2 of 3 checks passed
continuous-integration/appveyor/pr AppVeyor build failed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants
You can’t perform that action at this time.