@@ -157,23 +157,34 @@ function _authheaders(token::Secret; hasura=false)
157
157
end
158
158
159
159
"""
160
- JuliaHub.authenticate(server = Pkg.pkg_server(); force::Bool = false, maxcount::Integer = $(_DEFAULT_authenticate_maxcount) , [hook::Base.Callable])
160
+ JuliaHub.authenticate(
161
+ server::AbstractString = Pkg.pkg_server();
162
+ force::Bool = false,
163
+ maxcount::Integer = $(_DEFAULT_authenticate_maxcount) ,
164
+ [hook::Base.Callable]
165
+ ) -> JuliaHub.Authentication
166
+ JuliaHub.authenticate(server::AbstractString, token::Union{AbstractString, JuliaHub.Secret}) -> JuliaHub.Authentication
167
+
168
+ Authenticates with a JuliaHub server, returning a [`JuliaHub.Authentication`](@ref) object and
169
+ setting the global authentication session (see [`JuliaHub.current_authentication`](@ref)).
170
+ May throw an [`AuthenticationError`](@ref) if the authentication fails (e.g. expired token).
171
+
172
+ The zero- and one-argument methods will attempt to read the token from the current Julia depot.
173
+ If a valid authentication token does not exist in the Julia depot, a new token is acquired via an
174
+ interactive browser based prompt. By default, it attemps to connect to the currently configured Julia
175
+ package server URL (configured e.g. via the `JULIA_PKG_SERVER` environment variable), but this
176
+ can be overridden by passing the `server` argument.
177
+
178
+ The two-argument method can be used when you do not want to read the token from the `auth.toml`
179
+ file (e.g. when using a long-term token via an environment variable). In this case, you also have
180
+ to explicitly set the server URL and `JULIA_PKG_SERVER` is ignored.
161
181
162
- Authenticates with a JuliaHub server. If a valid authentication token does not exist in
163
- the Julia depot, a new token is acquired via an interactive browser based prompt.
164
- Returns an [`Authentication`](@ref) object if the authentication was successful, or throws an
165
- [`AuthenticationError`](@ref) if authentication fails.
182
+ # Extended help
166
183
167
184
The interactive prompts tries to authenticate for a maximum of `maxcount` times.
168
185
If `force` is set to `true`, an existing authentication token is first deleted. This can be
169
186
useful when the existing authentication token is causing the authentication to fail.
170
187
171
- # Extended help
172
-
173
- By default, it attemps to connect to the currently configured Julia package server URL
174
- (configured e.g. via the `JULIA_PKG_SERVER` environment variable). However, this can
175
- be overridden by passing the `server` argument.
176
-
177
188
`hook` can be set to a function taking a single string-type argument, and will be passed the
178
189
authorization URL the user should interact with in the browser. This can be used to override the default
179
190
behavior coming from [PkgAuthentication](https://github.com/JuliaComputing/PkgAuthentication.jl).
@@ -183,6 +194,17 @@ cached authentications), making it unnecessary to pass the returned object manua
183
194
function calls. This is useful for interactive use, but should not be used in library code,
184
195
as different authentication calls may clash.
185
196
"""
197
+ function authenticate end
198
+
199
+ function authenticate (server:: AbstractString , token:: Union{AbstractString, Secret} )
200
+ auth = _authentication (
201
+ _juliahub_uri (server);
202
+ token= isa (token, Secret) ? token : Secret (token),
203
+ )
204
+ global __AUTH__[] = auth
205
+ return auth
206
+ end
207
+
186
208
function authenticate (
187
209
server:: Union{AbstractString, Nothing} = nothing ;
188
210
force:: Bool = false ,
@@ -197,6 +219,13 @@ function authenticate(
197
219
),
198
220
)
199
221
end
222
+ server_uri = _juliahub_uri (server)
223
+ auth = Mocking. @mock _authenticate (server_uri; force, maxcount, hook)
224
+ global __AUTH__[] = auth
225
+ return auth
226
+ end
227
+
228
+ function _juliahub_uri (server:: Union{AbstractString, Nothing} )
200
229
# PkgAuthentication.token_path can not handle server values that do not
201
230
# prepend `https://`, so we use Pkg.pkg_server() to normalize it, just in case.
202
231
server_uri_string = if isnothing (server)
@@ -217,9 +246,8 @@ function authenticate(
217
246
isnothing (server) ? (" Pkg.pkg_server()" , Pkg. pkg_server ()) : (" server" , server)
218
247
throw (AuthenticationError (" Invalid $name value '$value ' ($error_msg )" ))
219
248
end
220
- auth = Mocking. @mock _authenticate (server_uri; force, maxcount, hook)
221
- global __AUTH__[] = auth
222
- return auth
249
+
250
+ return server_uri
223
251
end
224
252
225
253
function _authenticate (
0 commit comments