Skip to content

switch from HTTP to Downloads #7

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

Merged
merged 1 commit into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ version = "0.3.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
julia = "1"
HTTP = "0.7, 0.8"
Downloads = "1"
JSON = "0.20, 0.21"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
30 changes: 16 additions & 14 deletions src/PkgAuthentication.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module PkgAuthentication

using HTTP, Random, JSON, Pkg, Dates
using Downloads, Random, JSON, Pkg, Dates

const TIMEOUT = 180 # seconds
const MANUAL_TIMEOUT = 30 # seconds
Expand Down Expand Up @@ -58,8 +58,14 @@ end
Fetches the server's response to `challenge` (a string) or `nothing` if the request failed.
"""
function fetch_response(url, challenge)
r = HTTP.post(url, HEADERS, string(challenge), status_exception = false)
r.status == 200 ? String(r.body) : nothing
output = IOBuffer()
r = request(url,
method = "POST",
headers = HEADERS,
input = IOBuffer(string(challenge)),
output = output,
)
r.status == 200 ? String(take!(output)) : nothing
end

"""
Expand All @@ -72,20 +78,16 @@ fetched and written to disk. `failed` is incremented by one and returned if the
with a non-200 status code.
"""
function claim_token(url, challenge, response; failed = 1)
r = HTTP.post(
output = IOBuffer()
data = """{ "challenge": "$challenge", "response": "$response" }"""
r = request(
url,
HEADERS,
"""
{
"challenge": "$(challenge)",
"response": "$(response)"
}
""",
status_exception = false
headers = HEADERS,
input = IOBuffer(data),
output = output,
)

if r.status == 200 # request understood
b = JSON.parse(String(r.body))
b = JSON.parse(String(take!(output)))

if haskey(b, "token") # token returned. success.
token = b["token"]
Expand Down