Skip to content

Commit 6e2e113

Browse files
committed
fix: redact token secrets in state show methods
1 parent 16d507c commit 6e2e113

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/PkgAuthentication.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ step(state::State) =
1818
struct Success <: State
1919
token::Dict{String, Any}
2020
end
21+
Base.show(io::IO, ::Success) = print(io, "Success(<REDACTED>)")
2122

2223
abstract type Failure <: State end
2324

@@ -146,6 +147,8 @@ or NoAuthentication if not.
146147
struct NeedAuthentication <: State
147148
server::String
148149
end
150+
Base.show(io::IO, s::NeedAuthentication) = print(io, "NeedAuthentication($(s.server))")
151+
149152
function step(state::NeedAuthentication)::Union{HasToken, NoAuthentication}
150153
path = token_path(state.server)
151154
if isfile(path)
@@ -167,6 +170,8 @@ to RequestLogin, or to Failure otherwise.
167170
struct NoAuthentication <: State
168171
server::String
169172
end
173+
Base.show(io::IO, s::NoAuthentication) = print(io, "NoAuthentication($(s.server))")
174+
170175
function step(state::NoAuthentication)::Union{RequestLogin, Failure}
171176
challenge = Random.randstring(32)
172177
output = IOBuffer()
@@ -195,6 +200,8 @@ struct HasToken <: State
195200
mtime::Float64
196201
token::Dict{String, Any}
197202
end
203+
Base.show(io::IO, s::HasToken) = print(io, "HasToken($(s.server), $(s.mtime), <REDACTED>)")
204+
198205
function step(state::HasToken)::Union{NeedRefresh, Success}
199206
expiry = get(state.token, "expires_at", get(state.token, "expires", 0))
200207
expires_in = get(state.token, "expires_in", Inf)
@@ -214,6 +221,8 @@ struct NeedRefresh <: State
214221
server::String
215222
token::Dict{String, Any}
216223
end
224+
Base.show(io::IO, s::NeedRefresh) = print(io, "NeedRefresh($(s.server), <REDACTED>)")
225+
217226
function step(state::NeedRefresh)::Union{HasNewToken, NoAuthentication}
218227
refresh_token = state.token["refresh_token"]
219228
headers = ["Authorization" => "Bearer $refresh_token"]
@@ -267,6 +276,8 @@ struct HasNewToken <: State
267276
token::Dict{String, Any}
268277
tries::Int
269278
end
279+
Base.show(io::IO, s::HasNewToken) = print(io, "HasNewToken($(s.server), <REDACTED>, $(s.tries))")
280+
270281
HasNewToken(server, token) = HasNewToken(server, token, 0)
271282
function step(state::HasNewToken)::Union{HasNewToken, Success, Failure}
272283
if state.tries >= 3
@@ -299,6 +310,8 @@ struct RequestLogin <: State
299310
challenge::String
300311
response::String
301312
end
313+
Base.show(io::IO, s::RequestLogin) = print(io, "RequestLogin($(s.server), <REDACTED>, $(s.response))")
314+
302315
function step(state::RequestLogin)::Union{ClaimToken, Failure}
303316
success = open_browser(string(state.server, "/response?", state.response))
304317
if success
@@ -324,6 +337,8 @@ struct ClaimToken <: State
324337
failures::Int
325338
max_failures::Int
326339
end
340+
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))")
341+
327342
ClaimToken(server, challenge, response, expiry = Inf, failures = 0) =
328343
ClaimToken(server, challenge, response, expiry, time(), 180, 2, failures, 10)
329344

test/tests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ PkgAuthentication.register_open_browser_hook(url -> HTTP.get(url))
4646
@test success isa PkgAuthentication.Success
4747
@test success.token["expires_at"] > time()
4848
@test startswith(success.token["id_token"], "full-")
49+
@test !occursin("id_token", sprint(show, success))
4950

5051
sleeptimer = ceil(Int, success.token["expires_at"] - time() + 1)
5152
@info "sleep for $(sleeptimer)s (until refresh necessary)"
@@ -54,6 +55,7 @@ PkgAuthentication.register_open_browser_hook(url -> HTTP.get(url))
5455
@info "testing auth refresh"
5556
success2 = PkgAuthentication.authenticate(test_pkg_server)
5657
@test success2 isa PkgAuthentication.Success
58+
@test !occursin("id_token", sprint(show, success2))
5759
@test success2.token["expires_at"] > time()
5860
@test success2.token["refresh_token"] !== success.token["refresh_token"]
5961
@test startswith(success2.token["id_token"], "refresh-")

0 commit comments

Comments
 (0)