Skip to content

Commit 2318dea

Browse files
committed
Move auth_suffix under states
1 parent 3a440b2 commit 2318dea

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/PkgAuthentication.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,13 @@ function authenticate(;
115115

116116
server = pkg_server()
117117
server = rstrip(server, '/')
118-
server = string(server, "/", auth_suffix)
119118

120119
local state
121120

122121
for i in 1:tries
123122
initial = force ? NoAuthentication : NeedAuthentication
124123

125-
state = initial(server)
124+
state = initial(server, auth_suffix)
126125
try
127126
while !(isa(state, Success) || isa(state, Failure))
128127
@debug "Calling step(::$(typeof(state)))"
@@ -148,8 +147,9 @@ or NoAuthentication if not.
148147
"""
149148
struct NeedAuthentication <: State
150149
server::String
150+
auth_suffix::String
151151
end
152-
Base.show(io::IO, s::NeedAuthentication) = print(io, "NeedAuthentication($(s.server))")
152+
Base.show(io::IO, s::NeedAuthentication) = print(io, "NeedAuthentication($(s.server), $(s.auth_suffix))")
153153

154154
function step(state::NeedAuthentication)::Union{HasToken, NoAuthentication}
155155
path = token_path(state.server)
@@ -171,8 +171,9 @@ to RequestLogin, or to Failure otherwise.
171171
"""
172172
struct NoAuthentication <: State
173173
server::String
174+
auth_suffix::String
174175
end
175-
Base.show(io::IO, s::NoAuthentication) = print(io, "NoAuthentication($(s.server))")
176+
Base.show(io::IO, s::NoAuthentication) = print(io, "NoAuthentication($(s.server), $(s.auth_suffix))")
176177

177178
function get_openid_configuration(state::NoAuthentication)
178179
output = IOBuffer()
@@ -246,7 +247,7 @@ function initiate_browser_challenge(state::NoAuthentication)
246247
output = IOBuffer()
247248
challenge = Random.randstring(32)
248249
response = Downloads.request(
249-
string(state.server, "/challenge"),
250+
joinpath(state.server, state.auth_suffix, "challenge"),
250251
method = "POST",
251252
input = IOBuffer(challenge),
252253
output = output,
@@ -414,6 +415,7 @@ token, or to Failure if the polling times out, or there is an unexpected error.
414415
"""
415416
struct ClaimToken <: State
416417
server::String
418+
auth_suffix::String
417419
challenge::Union{Nothing, String}
418420
response::Union{String, Dict{String, Any}}
419421
expiry::Float64
@@ -424,10 +426,10 @@ struct ClaimToken <: State
424426
max_failures::Int
425427
token_endpoint::String
426428
end
427-
Base.show(io::IO, s::ClaimToken) = print(io, "ClaimToken($(s.server), <REDACTED>, $(s.response), $(s.expiry), $(s.start_time), $(s.timeout), $(s.poll_interval), $(s.failures), $(s.max_failures), $(s.token_endpoint))")
429+
Base.show(io::IO, s::ClaimToken) = print(io, "ClaimToken($(s.server), $(s.auth_suffix), <REDACTED>, $(s.response), $(s.expiry), $(s.start_time), $(s.timeout), $(s.poll_interval), $(s.failures), $(s.max_failures), $(s.token_endpoint))")
428430

429-
ClaimToken(server, challenge, response, token_endpoint, expiry = Inf, failures = 0) =
430-
ClaimToken(server, challenge, response, expiry, time(), 180, 2, failures, 10, token_endpoint)
431+
ClaimToken(server, auth_suffix, challenge, response, token_endpoint, expiry = Inf, failures = 0) =
432+
ClaimToken(server, auth_suffix, challenge, response, expiry, time(), 180, 2, failures, 10, token_endpoint)
431433

432434
function step(state::ClaimToken)::Union{ClaimToken, HasNewToken, Failure}
433435
if time() > state.expiry || (time() - state.start_time)/1e6 > state.timeout # server-side or client-side timeout
@@ -458,7 +460,7 @@ function step(state::ClaimToken)::Union{ClaimToken, HasNewToken, Failure}
458460
"response" => state.response,
459461
))
460462
response = Downloads.request(
461-
string(state.server, "/claimtoken"),
463+
joinpath(state.server, state.auth_suffix, "claimtoken"),
462464
method = "POST",
463465
input = IOBuffer(data),
464466
output = output,
@@ -470,15 +472,15 @@ function step(state::ClaimToken)::Union{ClaimToken, HasNewToken, Failure}
470472
body = try
471473
JSON.parse(String(take!(output)))
472474
catch err
473-
return ClaimToken(state.server, state.challenge, state.response, state.expiry, state.start_time, state.timeout, state.poll_interval, state.failures + 1, state.max_failures, state.token_endpoint)
475+
return ClaimToken(state.server, state.auth_suffix, state.challenge, state.response, state.expiry, state.start_time, state.timeout, state.poll_interval, state.failures + 1, state.max_failures, state.token_endpoint)
474476
end
475477

476478
if haskey(body, "token")
477479
return HasNewToken(state.server, body["token"])
478480
elseif haskey(body, "expiry") # time at which the response/challenge pair will expire on the server
479-
return ClaimToken(state.server, state.challenge, state.response, body["expiry"], state.start_time, state.timeout, state.poll_interval, state.failures, state.max_failures, state.token_endpoint)
481+
return ClaimToken(state.server, state.auth_suffix, state.challenge, state.response, body["expiry"], state.start_time, state.timeout, state.poll_interval, state.failures, state.max_failures, state.token_endpoint)
480482
else
481-
return ClaimToken(state.server, state.challenge, state.response, state.expiry, state.start_time, state.timeout, state.poll_interval, state.failures + 1, state.max_failures, state.token_endpoint)
483+
return ClaimToken(state.server, state.auth_suffix, state.challenge, state.response, state.expiry, state.start_time, state.timeout, state.poll_interval, state.failures + 1, state.max_failures, state.token_endpoint)
482484
end
483485
elseif response isa Downloads.Response && response.status == 200
484486
body = JSON.parse(String(take!(output)))
@@ -487,7 +489,7 @@ function step(state::ClaimToken)::Union{ClaimToken, HasNewToken, Failure}
487489
body["refresh_url"] = joinpath(state.server, "auth/renew/token.toml/device/") # Need to be careful with auth suffix, if set
488490
return HasNewToken(state.server, body)
489491
elseif response isa Downloads.Response && response.status in [401, 400] && is_device
490-
return ClaimToken(state.server, state.challenge, state.response, state.expiry, state.start_time, state.timeout, state.poll_interval, state.failures + 1, state.max_failures, state.token_endpoint)
492+
return ClaimToken(state.server, state.auth_suffix, state.challenge, state.response, state.expiry, state.start_time, state.timeout, state.poll_interval, state.failures + 1, state.max_failures, state.token_endpoint)
491493
else
492494
return HttpError(response)
493495
end

0 commit comments

Comments
 (0)