Skip to content

Commit 40fb74f

Browse files
committed
2FA fixes
1 parent ad63e05 commit 40fb74f

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/hex/http.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ defmodule Hex.HTTP do
5353
{body, extra_headers} = wrap_body_with_progress(body, progress_callback)
5454
headers = Map.merge(headers, extra_headers)
5555

56+
# Work around httpc bug: disable connection reuse when using Expect: 100-continue
57+
# httpc doesn't properly handle connection state when receiving final status (401)
58+
# instead of 100 Continue response
59+
headers =
60+
if headers["expect"] == "100-continue" do
61+
Map.put(headers, "connection", "close")
62+
else
63+
headers
64+
end
65+
5666
http_opts = build_http_opts(url, timeout)
5767
opts = [body_format: :binary]
5868
request = build_request(url, headers, body)
@@ -397,7 +407,6 @@ defmodule Hex.HTTP do
397407
{content_type, binary_body}
398408
when is_binary(binary_body) and is_function(progress_callback, 1) ->
399409
total_size = byte_size(binary_body)
400-
progress_callback.(0)
401410

402411
body_fn = fn
403412
size when size < total_size ->

lib/mix/tasks/hex.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ defmodule Mix.Tasks.Hex do
455455
end
456456

457457
defp prompt_otp() do
458+
Hex.Shell.info("")
459+
458460
Hex.Shell.prompt("Enter your 2FA code:")
459461
|> String.trim()
460462
end
@@ -588,17 +590,17 @@ defmodule Mix.Tasks.Hex do
588590
def with_otp_retry(auth, fun) when is_function(fun, 1) do
589591
case fun.(auth) do
590592
{:error, :otp_required} ->
591-
# Server requires OTP, prompt for it and retry
592593
otp = prompt_otp()
594+
Hex.State.put(:api_otp, otp)
593595
auth_with_otp = Keyword.put(auth, :otp, otp)
594-
fun.(auth_with_otp)
596+
with_otp_retry(auth_with_otp, fun)
595597

596598
{:error, :invalid_totp} ->
597-
# Invalid OTP code provided, show error and prompt again
598599
Hex.Shell.error("Invalid two-factor authentication code")
599600
otp = prompt_otp()
601+
Hex.State.put(:api_otp, otp)
600602
auth_with_otp = Keyword.put(auth, :otp, otp)
601-
fun.(auth_with_otp)
603+
with_otp_retry(auth_with_otp, fun)
602604

603605
result ->
604606
result
@@ -636,8 +638,6 @@ defmodule Mix.Tasks.Hex do
636638
end
637639

638640
def progress(max) do
639-
put_progress(0, 0)
640-
641641
fn size ->
642642
fraction = size / max
643643
completed = trunc(fraction * @progress_steps)

lib/mix/tasks/hex.publish.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ defmodule Mix.Tasks.Hex.Publish do
150150
case create_release(build, organization, auth, opts) do
151151
:ok ->
152152
Hex.Shell.info("Publishing docs...")
153+
# Refresh auth to pick up cached OTP from package publish
154+
auth = Mix.Tasks.Hex.auth_info(:write)
153155
create_docs(build, organization, auth, opts)
154156
transfer_owner(build, owner, auth, opts)
155157

0 commit comments

Comments
 (0)