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

v2/auth: add Basic authentication and refactor #159

Merged
merged 2 commits into from
Jul 9, 2020

Conversation

steveej
Copy link
Contributor

@steveej steveej commented Jun 19, 2020

This adds Basic authentication support.

Along the way, the auth module has been refactored with new enums and
structs to model the supported authentication mechanisms and make the
response header parsing more robust.

Breaking API changes

  • The Client::login and Client::authenticate methods have been
    merged into the latter.
  • Client::is_auth now performs a pure authentication test without
    attempting to authenticate, thus it no longer takes a token.

@steveej steveej force-pushed the pr/auth-refactor-and-add-basic branch from a385d51 to d7ab100 Compare June 19, 2020 11:35
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated
.ok_or_else(|| Error::from(format!("method not found in {}", header)))?
.as_str();

let serialized_content = serde_json::Value::Object(serde_json::Map::from_iter(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat inconvenient. I'm thinking of assembling a JSON string using plain string formatting and skipping the Value::Object route.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 68da0dc on top which implements the plain string route. I like the aspect that we don't need the intermediate Value and I can't spot any weakness in the approach. I'd appreciate thoughts here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucab any opinion on this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[sorry I didn't see this comment in review mode in GH]

The main problem I see with the logic in the fixup commit is when either key or value contains some characters that may need to be escaped in JSON-encoding (e.g. "). serde serialization takes care of that for you and is in general more robust.

But I do agree this code is a bit unwieldy. I think the source of the pain is the round-trip through JSON and serde. As a manual alternative, I think you could shove the key-value pairs into a map, drain the well-known values you expect in order to build the object, and warn if there are any leftover keys in the map.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a compromise by wrapping the strings in a serde_json::Value::String. See this playground. WDYT?

Copy link
Contributor Author

@steveej steveej Jun 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed this suggestion into db88916 and am proposing it as the final solution. It doesn't have the escaping bug you detected with the previous revision, and it doesn't do the round trip. PTAL and let me know if I should squash 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it looks better than the initial version. Feel free to squash in.

src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
@steveej steveej force-pushed the pr/auth-refactor-and-add-basic branch from d7ab100 to e2d187c Compare June 19, 2020 12:01
@steveej steveej force-pushed the pr/auth-refactor-and-add-basic branch from 68da0dc to f73cb89 Compare June 23, 2020 15:18
Copy link
Member

@lucab lucab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I really like this. There are just a couple of spots where the logic could be hammered a bit more into shape.

src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
src/v2/auth.rs Outdated Show resolved Hide resolved
@steveej steveej force-pushed the pr/auth-refactor-and-add-basic branch from f73cb89 to 42e0416 Compare June 25, 2020 13:10
@steveej
Copy link
Contributor Author

steveej commented Jun 25, 2020

Thanks for the review @lucab! I addressed all comments in a fixup commit. I'll think about #159 (comment) a bit more.

@steveej steveej force-pushed the pr/auth-refactor-and-add-basic branch 4 times, most recently from 486bc63 to a4e54b7 Compare June 25, 2020 16:33
@steveej steveej force-pushed the pr/auth-refactor-and-add-basic branch from a4e54b7 to db88916 Compare June 25, 2020 21:31
@steveej steveej requested a review from lucab July 7, 2020 08:37
Copy link
Contributor

@LalatenduMohanty LalatenduMohanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using my limited knowledge of Rust and dkregistry-rs the code changes looks good to me. Also the review comments are mostly fixed now. So we should merge this asap and take the followup comments (if any) in new PRs.

@steveej
Copy link
Contributor Author

steveej commented Jul 8, 2020

Using my limited knowledge of Rust and dkregistry-rs the code changes looks fine to me.

To add some context to this, @LalatenduMohanty and I went over the code and the changes together using screen-sharing for about two hours 😉

@lucab
Copy link
Member

lucab commented Jul 9, 2020

Yes, it also looks good to me. Feel free to squash the fixup and self-merge.

This adds Basic authentication support.

Along the way, the auth module has been refactored with new enums and
structs to model the supported authentication mechanisms and make the
response header parsing more robust.

***Breaking API changes***

* The `Client::login` and `Client::authenticate` methods have been
  merged into the latter.
* `Client::is_auth` now performs a pure authentication test without
  attempting to authenticate, thus it no longer takes a token.
@steveej steveej force-pushed the pr/auth-refactor-and-add-basic branch from db88916 to 16fc318 Compare July 9, 2020 07:58
@steveej steveej merged commit 5a4ec71 into master Jul 9, 2020
@steveej steveej deleted the pr/auth-refactor-and-add-basic branch July 9, 2020 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants