-
Notifications
You must be signed in to change notification settings - Fork 8
feat: interactive authentication for invalid token #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -198,10 +198,15 @@ as different authentication calls may clash. | |
| function authenticate end | ||
|
|
||
| function authenticate(server::AbstractString, token::Union{AbstractString, Secret}) | ||
| auth = _authentication( | ||
| _juliahub_uri(server); | ||
| token=isa(token, Secret) ? token : Secret(token), | ||
| ) | ||
| auth = try | ||
| _authentication( | ||
| _juliahub_uri(server); | ||
| token=isa(token, Secret) ? token : Secret(token), | ||
| ) | ||
| catch e | ||
| isa(e, InvalidAuthentication) || rethrow() | ||
| throw(AuthenticationError("The authentication token is invalid")) | ||
| end | ||
| global __AUTH__[] = auth | ||
| return auth | ||
| end | ||
|
|
@@ -259,7 +264,44 @@ function _authenticate( | |
| # _authenticate either returns a valid token, or throws | ||
| auth_toml = _authenticate_retry(string(server_uri), 1; force, maxcount) | ||
| # Note: _authentication may throw, which gets passed on to the user | ||
| _authentication(server_uri; auth_toml...) | ||
| try | ||
| _authentication(server_uri; auth_toml...) | ||
| catch e | ||
| # If the token in auth.toml is invalid, but it hasn't expired, | ||
| # PkgAuthentication won't catch that, and we attempt to use it (to get the | ||
| # API version etc). If the token is invalid, that fails with a 401 and | ||
| # _authentication() throws. In this case, we will go ahead and remove the token | ||
| # and try again (which should lead to the interactive authentication flow). | ||
| if !isa(e, InvalidAuthentication) || (maxcount <= 1) | ||
| rethrow() | ||
| end | ||
| # We'll back up the old auth.toml though, because the user did not ask | ||
| # us to remove it, so we don't want to delete the token for them either. | ||
| # To avoid overwriting an existing backup, we generate a unique name | ||
| # by hashing the file contents. | ||
|
Comment on lines
+278
to
+281
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We know the token is invalid though, so why do we want to keep it around?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to delete user data if they didn't explicitly ask for it. And right now we delete iff |
||
| backup_path = string( | ||
| auth_toml.tokenpath, | ||
| ".", | ||
| bytes2hex(open(SHA.sha1, "CHANGELOG.md"))[1:8], | ||
mortenpi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ".backup", | ||
| ) | ||
| mv(auth_toml.tokenpath, backup_path; force=true) | ||
| @warn """ | ||
| Existing token appears invalid, retrying with `force=true`. | ||
| Existing auth.toml backed up in: $(backup_path) | ||
mortenpi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| # We assume that _authenticate_retry immediately returned the token, | ||
| # and didn't retry multiple times. So we just bump `count` by one here. | ||
| auth_toml = _authenticate_retry(string(server_uri), 2; force=true, maxcount) | ||
| try | ||
| _authentication(server_uri; auth_toml...) | ||
| catch e | ||
| # If it again fails with InvalidAuthentication, we give up. But we | ||
| # need to throw AuthenticationError. | ||
| isa(e, InvalidAuthentication) || rethrow() | ||
| throw(AuthenticationError("JuliaHub returned an invalid authentication token")) | ||
| end | ||
| end | ||
| finally | ||
| isnothing(hook) || PkgAuthentication.clear_open_browser_hook() | ||
| end | ||
|
|
@@ -340,6 +382,7 @@ function _authentication( | |
| api = try | ||
| _get_api_information(string(server), token) | ||
| catch e | ||
| isa(e, InvalidAuthentication) && rethrow() | ||
| errmsg = """ | ||
| Unable to determine JuliaHub API version. | ||
| _get_api_information failed with an exception: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.