Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- '1.8'
- '1.9'
- '1.10'
- '1.11'
- '1' # automatically expands to the latest stable 1.x release
- 'nightly'
os:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PkgAuthentication"
uuid = "4722fa14-9d28-45f9-a1e2-a38605bd88f0"
authors = ["Sebastian Pfitzner", "contributors"]
version = "2.3.0"
version = "2.3.1"

[deps]
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Expand Down
6 changes: 3 additions & 3 deletions src/PkgAuthentication.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function authenticate(;

local state

for i in 1:tries
for _ in 1:tries
initial = force ? NoAuthentication : NeedAuthentication

state = initial(server, auth_suffix)
Expand All @@ -131,7 +131,7 @@ function authenticate(;
state = GenericError((err, catch_backtrace()))
end
if state isa Success
continue
break
end
end

Expand Down Expand Up @@ -766,7 +766,7 @@ function get_server_dir(
server_dir_pkg = Pkg.PlatformEngines.get_server_dir(url, server)
if server_dir_pkgauth != server_dir_pkg
msg =
"The PkgAuthentication server directory is not equal to the Pkg server directory." *
"The PkgAuthentication server directory is not equal to the Pkg server directory. " *
"Unexpected behavior may occur."
@warn msg server_dir_pkgauth server_dir_pkg
end
Expand Down
21 changes: 14 additions & 7 deletions test/authserver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ const CHALLENGE_EXPIRY = 10
const PORT = 8888
@enum AuthFlowMode CLASSIC_MODE DEVICE_FLOW_MODE DEVICE_FLOW_NO_SCOPE_MODE

const ID_TOKEN = Random.randstring(100)
const TOKEN = Ref(Dict())
const MODE = Ref(CLASSIC_MODE)

const REQUEST_SET = Set()
# this counts the number of distinct authentication requests made against the server
function id_token(key)
push!(REQUEST_SET, key)
token = length(REQUEST_SET)
return string(token)
end

challenge_response_map = Dict()
challenge_timeout = Dict()
response_challenge_map = Dict()
Expand Down Expand Up @@ -42,8 +49,8 @@ function response_handler(req)
TOKEN[] = Dict(
"user_name" => "firstname lastname",
"user_email" => "[email protected]",
"id_token" => "full-" * ID_TOKEN,
"access_token" => "full-" * ID_TOKEN,
"id_token" => "full-" * id_token(response),
"access_token" => "full-" * id_token(response),
"refresh_token" => refresh_token,
"refresh_url" => "http://localhost:$(PORT)/auth/renew/token.toml/v2/",
"expires_in" => EXPIRY,
Expand Down Expand Up @@ -89,8 +96,8 @@ function renew_handler(req)

TOKEN[]["refresh_token"] = Random.randstring(10)
TOKEN[]["expires_at"] = ceil(Int, time() + EXPIRY)
TOKEN[]["id_token"] = "refresh-" * ID_TOKEN
TOKEN[]["access_token"] = "refresh-" * ID_TOKEN
TOKEN[]["id_token"] = "refresh-" * id_token(auth)
TOKEN[]["access_token"] = "refresh-" * id_token(auth)

return HTTP.Response(200, sprint(TOML.print, TOKEN[]))
end
Expand Down Expand Up @@ -167,11 +174,11 @@ function auth_device(req)
end
authenticated[device_code] = true
refresh_token = Random.randstring(10)
TOKEN[]["access_token"] = "device-$ID_TOKEN"
TOKEN[]["access_token"] = "device-$(id_token(user_code))"
TOKEN[]["token_type"] = "bearer"
TOKEN[]["expires_in"] = EXPIRY
TOKEN[]["refresh_token"] = refresh_token
TOKEN[]["id_token"] = "device-$ID_TOKEN"
TOKEN[]["id_token"] = "device-$(id_token(user_code))"
return HTTP.Response(200)
end

Expand Down
20 changes: 20 additions & 0 deletions test/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,24 @@ end
@test result == (true, true)
end

@testset "no retries" begin
delete_token()

success = PkgAuthentication.authenticate(test_pkg_server; force=true, tries=2)
@test success isa PkgAuthentication.Success
m = match(r"^\w+\-(\d+)$", success.token["id_token"])
@test !isnothing(m)
id1 = tryparse(Int, m.captures[1])
@test id1 !== nothing

success2 = PkgAuthentication.authenticate(test_pkg_server; force=true, tries=2)
@test success2 isa PkgAuthentication.Success
m2 = match(r"^\w+\-(\d+)$", success2.token["id_token"])
@test !isnothing(m2)
id2 = tryparse(Int, m2.captures[1])

@test id2 !== nothing
@test id2 == id1 + 1
end

kill(p)
Loading