-
Notifications
You must be signed in to change notification settings - Fork 187
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
WebAuth Client MVP #933
Open
Benjamin-Philip
wants to merge
13
commits into
hexpm:main
Choose a base branch
from
Benjamin-Philip:bp-web-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WebAuth Client MVP #933
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cc0d961
Add web auth flow code
Benjamin-Philip c36bb41
Add API tests
Benjamin-Philip e645e5c
Fix API.WebAuth functions
Benjamin-Philip fcb4f3e
Add working WebAuth task
Benjamin-Philip 30ee784
Get submit url from WebAuth task
Benjamin-Philip f8c77a4
Pattern match user and device codes in get_code
Benjamin-Philip c3f7119
Remove submit_in_browser function from Hex.API.WebAuth
Benjamin-Philip 3943205
Improve task description
Benjamin-Philip 2824fd5
Pattern match user code in get_code
Benjamin-Philip fedb951
Sleep for 1 second between access key requests
Benjamin-Philip 7dae6be
Revert "Sleep for 1 second between access key requests"
Benjamin-Philip e9c73cf
Add timeout for fast access_key_task requests
Benjamin-Philip 7ccbe7b
Use hand written timeout instead of Task.await to access_key
Benjamin-Philip 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 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,39 @@ | ||
defmodule Hex.API.WebAuth do | ||
@moduledoc false | ||
|
||
alias Hex.API | ||
|
||
def get_code(key_name) do | ||
{:ok, {_status, code, _}} = | ||
API.erlang_post_request(nil, "web_auth/code", %{key_name: key_name}) | ||
|
||
code | ||
end | ||
|
||
def access_key(device_code) do | ||
Hex.API.check_write_api() | ||
|
||
now = System.os_time(:millisecond) | ||
access_key(%{device_code: device_code}, now, now + 5 * 60 * 1000) | ||
end | ||
|
||
defp access_key(params, last_request_time, timeout_time) do | ||
case API.erlang_post_request(nil, "web_auth/access_key", params) do | ||
{:ok, {_code, %{"write_key" => write_key, "read_key" => read_key}, _headers}} -> | ||
%{write_key: write_key, read_key: read_key} | ||
|
||
{:ok, {_code, %{"message" => "request to be verified"}, _headers}} -> | ||
diff = System.os_time(:millisecond) - last_request_time | ||
|
||
if diff < 1000 do | ||
Process.sleep(1000 - diff) | ||
end | ||
|
||
access_key(params, System.os_time(:millisecond), timeout_time) | ||
end | ||
end | ||
|
||
defp access_key(_, last_request_time, timeout_time) when timeout_time > last_request_time do | ||
raise "Browser-based authentication has timed out" | ||
end | ||
end |
This file contains 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 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 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 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 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 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
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.
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.
You can use
assert_received {:mix_shell, :info, [message]}
to retrieve the test output.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.
The test output is actually sent to the Task process, so I can't use
assert_received
.The reason I need to run the task in an other process is because, I need to extract the user_code from the test output, and then submit the user_code to hexpm for the web auth task to access the key.
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.
How about submitting the auth request in another process instead. Would that make it easier?
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.
That actually would. I did not think of that.Will implement when free.
There is no real difference between submitting the user code to hexpm in a seperate process and submitting in the test process.
This is because I can't access the task process's mailbox like normal, and I have to use Process.info to access its messages. See the
get_user_code
anonymous function for more info.What would help is some way to "echo" messages sent to the task process to the test process, in which case I can just assert_recieved. However, I think this would bring more complexity than there is already existing.