Skip to content

Commit 4326b89

Browse files
authored
fix: terminate after success for force auth (#55)
* fix: terminate after success for force auth * chore: also test on 1.11 * fix: test with force=true and tries=2 * fix: correct mock server counting impl * chore: bump version to v2.3.1
1 parent c18c118 commit 4326b89

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- '1.8'
3535
- '1.9'
3636
- '1.10'
37+
- '1.11'
3738
- '1' # automatically expands to the latest stable 1.x release
3839
- 'nightly'
3940
os:

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PkgAuthentication"
22
uuid = "4722fa14-9d28-45f9-a1e2-a38605bd88f0"
33
authors = ["Sebastian Pfitzner", "contributors"]
4-
version = "2.3.0"
4+
version = "2.3.1"
55

66
[deps]
77
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ 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 REQUEST_SET = Set()
13+
# this counts the number of distinct authentication requests made against the server
14+
function id_token(key)
15+
push!(REQUEST_SET, key)
16+
token = length(REQUEST_SET)
17+
return string(token)
18+
end
19+
1320
challenge_response_map = Dict()
1421
challenge_timeout = Dict()
1522
response_challenge_map = Dict()
@@ -42,8 +49,8 @@ function response_handler(req)
4249
TOKEN[] = Dict(
4350
"user_name" => "firstname lastname",
4451
"user_email" => "[email protected]",
45-
"id_token" => "full-" * ID_TOKEN,
46-
"access_token" => "full-" * ID_TOKEN,
52+
"id_token" => "full-" * id_token(response),
53+
"access_token" => "full-" * id_token(response),
4754
"refresh_token" => refresh_token,
4855
"refresh_url" => "http://localhost:$(PORT)/auth/renew/token.toml/v2/",
4956
"expires_in" => EXPIRY,
@@ -89,8 +96,8 @@ function renew_handler(req)
8996

9097
TOKEN[]["refresh_token"] = Random.randstring(10)
9198
TOKEN[]["expires_at"] = ceil(Int, time() + EXPIRY)
92-
TOKEN[]["id_token"] = "refresh-" * ID_TOKEN
93-
TOKEN[]["access_token"] = "refresh-" * ID_TOKEN
99+
TOKEN[]["id_token"] = "refresh-" * id_token(auth)
100+
TOKEN[]["access_token"] = "refresh-" * id_token(auth)
94101

95102
return HTTP.Response(200, sprint(TOML.print, TOKEN[]))
96103
end
@@ -167,11 +174,11 @@ function auth_device(req)
167174
end
168175
authenticated[device_code] = true
169176
refresh_token = Random.randstring(10)
170-
TOKEN[]["access_token"] = "device-$ID_TOKEN"
177+
TOKEN[]["access_token"] = "device-$(id_token(user_code))"
171178
TOKEN[]["token_type"] = "bearer"
172179
TOKEN[]["expires_in"] = EXPIRY
173180
TOKEN[]["refresh_token"] = refresh_token
174-
TOKEN[]["id_token"] = "device-$ID_TOKEN"
181+
TOKEN[]["id_token"] = "device-$(id_token(user_code))"
175182
return HTTP.Response(200)
176183
end
177184

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; force=true, tries=2)
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; force=true, tries=2)
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)