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

Bearer authentication support #11

Merged
merged 14 commits into from
Mar 7, 2022

Conversation

duderman
Copy link
Contributor

@duderman duderman commented Feb 23, 2022

Google's CalDav uses Bearer authentication header so I added :bearer support for auth key in client

Closes #11

@boonious boonious mentioned this pull request Mar 1, 2022
Copy link
Collaborator

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

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

Hello! Thanks for your contribution. I've left two closely related comments in the code.

test/caldav_client/tesla_test.exs Outdated Show resolved Hide resolved
lib/caldav_client/tesla.ex Outdated Show resolved Hide resolved
@duderman
Copy link
Contributor Author

duderman commented Mar 2, 2022

@tomekzaw thanks for review! Following your comments I added a new client %BearerClient{} and slightly changed Tesla module

Copy link
Collaborator

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

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

@duderman Thanks for applying the changes so quickly! I've left two suggestions in the code.

I like your idea with adding another type of client for backward-compatibility purposes. However, the naming suggest that BearerClient is a specialization of Client but in fact it's an alternative to Client. Also, these structs share a common field (namely server_url).

Maybe it would be better to have just one %CalDAVClient.Client with two fields: server_url and auth which can be one of: %CalDAVClient.Auth.Basic, %CalDAVClient.Auth.Digest or %CalDAVClient.Auth.Bearer? This API seems to be more intuitive and also supports checking fields in compile-time.

Once we have it, we can use pattern matching to make specific Tesla middleware just like you did.

I know this changes the API a bit but we will have to bump the version anyway. Hopefully the migration is quite easy.

lib/caldav_client/bearer_client.ex Outdated Show resolved Hide resolved
lib/caldav_client/bearer_client.ex Outdated Show resolved Hide resolved
duderman and others added 5 commits March 2, 2022 12:19
Co-authored-by: Tomek Zawadzki <tomekzawadzki98@gmail.com>
Co-authored-by: Tomek Zawadzki <tomekzawadzki98@gmail.com>
@duderman
Copy link
Contributor Author

duderman commented Mar 2, 2022

@tomekzaw My idea was to keep backward compatibility. But you are right. Having different structs for different auth mechanisms feels much more clear.

Just pushed some more changes 👍🏻

config/test.exs Outdated Show resolved Hide resolved
@tomekzaw
Copy link
Collaborator

tomekzaw commented Mar 2, 2022

@duderman Thanks a lot for the changes! I need some time to setup my environment so I can actually test it out. After this I think we are ready for merge and release.

@duderman
Copy link
Contributor Author

duderman commented Mar 4, 2022

@tomekzaw hey mate 😃 did you manage to test it out by any chance? Thanks!

@tomekzaw
Copy link
Collaborator

tomekzaw commented Mar 4, 2022

@duderman I've managed to setup the environment but unfortunately the integration tests failed. Most likely I've messed something up and I need some more time.

BTW Can you please rename tesla_text.exs to tesla_test.exs?

@duderman
Copy link
Contributor Author

duderman commented Mar 4, 2022

@tomekzaw oops. weird typo 😄 thanks!

I've used a baikal image from dockerhub for tests. worked quite well

docker run --rm -it -p 8800:80 ckulka/baikal:nginx

@tomekzaw
Copy link
Collaborator

tomekzaw commented Mar 7, 2022

@duderman I've tried running the full test suite with mix test --include integration and got the following runtime error:

** (UndefinedFunctionError) function CalDAVClient.Auth.Basic.fetch/2 is undefined (CalDAVClient.Auth.Basic does not implement the Access behaviour)

Looks like Map.from_struct/1 call was missing:

-  defp credentials(auth = %{username: _, password: _}), do: auth
+  defp credentials(auth = %{username: _, password: _}), do: Map.from_struct(auth)

Here's a more verbose implementation that also works for me:

  def make_tesla_client(%{server_url: server_url, auth: auth}, middleware \\ []) do
    Tesla.client([
      {Tesla.Middleware.BaseUrl, server_url},
      auth_middleware(auth)
      | middleware
    ])
  end

  defp auth_middleware(%CalDAVClient.Auth.Basic{username: username, password: password}),
    do: {Tesla.Middleware.BasicAuth, %{username: username, password: password}}

  defp auth_middleware(%CalDAVClient.Auth.Digest{username: username, password: password}),
    do: {Tesla.Middleware.DigestAuth, %{username: username, password: password}}

  defp auth_middleware(%CalDAVClient.Auth.Bearer{token: token}),
    do: {Tesla.Middleware.BearerAuth, [token: token]}

Honestly, I like your implementation better due to its simplicity. Can you please submit the fix with Map.from_struct/1?

Also, I'm thinking we can also split auth.ex into auth/{basic,digest,bearer}.ex so the paths are compliant with Elixir module names, what do you think about it?

Finally, thanks for your note about the endpoint URL. I've just tested it out and it still works with cal.php. Can you please to either replace all occurrences with dav.php or revert back to cal.php? We would like to keep the examples and test config consistent.

I've used a baikal image from dockerhub for tests. worked quite well

FYI I plan to submit a PR that adds docker-compose.yml for Baïkal and detailed instructions on how to run integration tests.

README.md Outdated Show resolved Hide resolved
lib/caldav_client/tesla.ex Outdated Show resolved Hide resolved
duderman and others added 4 commits March 7, 2022 10:29
Co-authored-by: Tomek Zawadzki <tomekzawadzki98@gmail.com>
@duderman
Copy link
Contributor Author

duderman commented Mar 7, 2022

@tomekzaw done ;)

Copy link
Collaborator

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

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

Great work! Thanks again for your contribution! 👏

@tomekzaw tomekzaw merged commit d4b439c into software-mansion-labs:master Mar 7, 2022
@duderman duderman deleted the bearer-auth branch March 7, 2022 11:36
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.

None yet

2 participants