Skip to content

Commit bf248f9

Browse files
committed
fix: terminate after success for force auth
1 parent c18c118 commit bf248f9

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/PkgAuthentication.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function authenticate(;
118118

119119
local state
120120

121-
for i in 1:tries
121+
for _ in 1:tries
122122
initial = force ? NoAuthentication : NeedAuthentication
123123

124124
state = initial(server, auth_suffix)
@@ -131,7 +131,7 @@ function authenticate(;
131131
state = GenericError((err, catch_backtrace()))
132132
end
133133
if state isa Success
134-
continue
134+
break
135135
end
136136
end
137137

@@ -766,7 +766,7 @@ function get_server_dir(
766766
server_dir_pkg = Pkg.PlatformEngines.get_server_dir(url, server)
767767
if server_dir_pkgauth != server_dir_pkg
768768
msg =
769-
"The PkgAuthentication server directory is not equal to the Pkg server directory." *
769+
"The PkgAuthentication server directory is not equal to the Pkg server directory. " *
770770
"Unexpected behavior may occur."
771771
@warn msg server_dir_pkgauth server_dir_pkg
772772
end

test/authserver.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ const CHALLENGE_EXPIRY = 10
66
const PORT = 8888
77
@enum AuthFlowMode CLASSIC_MODE DEVICE_FLOW_MODE DEVICE_FLOW_NO_SCOPE_MODE
88

9-
const ID_TOKEN = Random.randstring(100)
109
const TOKEN = Ref(Dict())
1110
const MODE = Ref(CLASSIC_MODE)
1211

12+
const TOKEN_COUNTER = Ref(0)
13+
function id_token()
14+
return string(TOKEN_COUNTER[] += 1)
15+
end
16+
1317
challenge_response_map = Dict()
1418
challenge_timeout = Dict()
1519
response_challenge_map = Dict()
@@ -42,8 +46,8 @@ function response_handler(req)
4246
TOKEN[] = Dict(
4347
"user_name" => "firstname lastname",
4448
"user_email" => "[email protected]",
45-
"id_token" => "full-" * ID_TOKEN,
46-
"access_token" => "full-" * ID_TOKEN,
49+
"id_token" => "full-" * id_token(),
50+
"access_token" => "full-" * id_token(),
4751
"refresh_token" => refresh_token,
4852
"refresh_url" => "http://localhost:$(PORT)/auth/renew/token.toml/v2/",
4953
"expires_in" => EXPIRY,
@@ -89,8 +93,8 @@ function renew_handler(req)
8993

9094
TOKEN[]["refresh_token"] = Random.randstring(10)
9195
TOKEN[]["expires_at"] = ceil(Int, time() + EXPIRY)
92-
TOKEN[]["id_token"] = "refresh-" * ID_TOKEN
93-
TOKEN[]["access_token"] = "refresh-" * ID_TOKEN
96+
TOKEN[]["id_token"] = "refresh-" * id_token()
97+
TOKEN[]["access_token"] = "refresh-" * id_token()
9498

9599
return HTTP.Response(200, sprint(TOML.print, TOKEN[]))
96100
end
@@ -167,11 +171,11 @@ function auth_device(req)
167171
end
168172
authenticated[device_code] = true
169173
refresh_token = Random.randstring(10)
170-
TOKEN[]["access_token"] = "device-$ID_TOKEN"
174+
TOKEN[]["access_token"] = "device-$(id_token())"
171175
TOKEN[]["token_type"] = "bearer"
172176
TOKEN[]["expires_in"] = EXPIRY
173177
TOKEN[]["refresh_token"] = refresh_token
174-
TOKEN[]["id_token"] = "device-$ID_TOKEN"
178+
TOKEN[]["id_token"] = "device-$(id_token())"
175179
return HTTP.Response(200)
176180
end
177181

test/tests.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,24 @@ end
175175
@test result == (true, true)
176176
end
177177

178+
@testset "no retries" begin
179+
delete_token()
180+
181+
success = PkgAuthentication.authenticate(test_pkg_server)
182+
@test success isa PkgAuthentication.Success
183+
m = match(r"^\w+\-(\d+)$", success.token["id_token"])
184+
@test !isnothing(m)
185+
id1 = tryparse(Int, m.captures[1])
186+
@test id1 !== nothing
187+
188+
success2 = PkgAuthentication.authenticate(test_pkg_server)
189+
@test success2 isa PkgAuthentication.Success
190+
m2 = match(r"^\w+\-(\d+)$", success2.token["id_token"])
191+
@test !isnothing(m2)
192+
id2 = tryparse(Int, m2.captures[1])
193+
194+
@test id2 !== nothing
195+
@test id2 == id1 + 1
196+
end
197+
178198
kill(p)

0 commit comments

Comments
 (0)