Skip to content

Commit 35b5312

Browse files
committed
use env var to activate device auth; change up dex config query
1 parent 49fbb75 commit 35b5312

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/PkgAuthentication.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,18 @@ struct NoAuthentication <: State
175175
end
176176
Base.show(io::IO, s::NoAuthentication) = print(io, "NoAuthentication($(s.server), $(s.auth_suffix))")
177177

178+
function get_device_auth_client_id()
179+
return get(ENV, "JULIA_PKG_AUTHENTICATION_DEVICE_CLIENT_ID", "")
180+
end
181+
182+
function should_use_device_auth()
183+
return !isempty(get_device_auth_client_id())
184+
end
185+
178186
function get_openid_configuration(state::NoAuthentication)
179187
output = IOBuffer()
180188
response = Downloads.request(
181-
joinpath(state.server, "dex/.well-known/openid-configuration"),
189+
joinpath(state.server, ".well-known/openid-configuration"),
182190
method = "GET",
183191
output = output,
184192
throw = false,
@@ -195,15 +203,22 @@ function get_openid_configuration(state::NoAuthentication)
195203
return false, "", ""
196204
end
197205

198-
return body !== nothing && haskey(body, "device_authorization_endpoint") && haskey(body, "grant_types_supported") && "urn:ietf:params:oauth:grant-type:device_code" in body["grant_types_supported"], body["device_authorization_endpoint"], body["token_endpoint"]
206+
if body !== nothing
207+
return true, body["device_authorization_endpoint"], body["token_endpoint"]
208+
end
199209
end
200210

201211
return false, "", ""
202212
end
203213

204214
function step(state::NoAuthentication)::Union{RequestLogin, Failure}
205-
device_tokens_supported, device_endpoint, token_endpoint = get_openid_configuration(state)
206-
success, challenge, body_or_response = if device_tokens_supported
215+
token_endpoint = ""
216+
device_endpoint = ""
217+
if should_use_device_auth()
218+
s, device_endpoint, token_endpoint = get_openid_configuration(state)
219+
s || GenericError("Unable to get device and token endpoints")
220+
end
221+
success, challenge, body_or_response = if should_use_device_auth()
207222
fetch_device_code(state, device_endpoint)
208223
else
209224
initiate_browser_challenge(state)

0 commit comments

Comments
 (0)