@@ -110,8 +110,11 @@ def login(
110110 * **"interactive"**: Enter a username and password.
111111 * **"netrc"**: (default) Retrieve a username and password from ~/.netrc.
112112 * **"environment"**:
113- Retrieve a username and password from $EARTHDATA_USERNAME and $EARTHDATA_PASSWORD.
114- persist: Will persist credentials in a `.netrc` file.
113+ Retrieve either a username and password pair from the
114+ `EARTHDATA_USERNAME` and `EARTHDATA_PASSWORD` environment variables,
115+ or an Earthdata login token from the `EARTHDATA_TOKEN` environment
116+ variable.
117+ persist: Will persist username and password credentials in a `.netrc` file.
115118 system: the EDL endpoint to log in to Earthdata, defaults to PROD
116119
117120 Returns:
@@ -245,7 +248,7 @@ def _interactive(
245248 ) -> bool :
246249 username = input ("Enter your Earthdata Login username: " )
247250 password = getpass .getpass (prompt = "Enter your Earthdata password: " )
248- authenticated = self ._get_credentials (username , password )
251+ authenticated = self ._get_credentials (username , password , None )
249252 if authenticated :
250253 logger .debug ("Using user provided credentials for EDL" )
251254 if persist_credentials :
@@ -282,7 +285,7 @@ def _netrc(self) -> bool:
282285 f"Password not found in .netrc file { netrc_loc } "
283286 )
284287
285- authenticated = self ._get_credentials (username , password )
288+ authenticated = self ._get_credentials (username , password , None )
286289
287290 if authenticated :
288291 logger .debug ("Using .netrc file for EDL" )
@@ -292,21 +295,27 @@ def _netrc(self) -> bool:
292295 def _environment (self ) -> bool :
293296 username = os .getenv ("EARTHDATA_USERNAME" )
294297 password = os .getenv ("EARTHDATA_PASSWORD" )
298+ token = os .getenv ("EARTHDATA_TOKEN" )
295299
296- if not username or not password :
300+ if ( not username or not password ) and not token :
297301 raise LoginStrategyUnavailable (
298- "EARTHDATA_USERNAME and EARTHDATA_PASSWORD are not set in the current environment, try "
299- "setting them or use a different strategy (netrc, interactive)"
302+ "Either the environment variables EARTHDATA_USERNAME and "
303+ "EARTHDATA_PASSWORD must both be set, or EARTHDATA_TOKEN must be set for "
304+ "the 'environment' login strategy."
300305 )
301306
302307 logger .debug ("Using environment variables for EDL" )
303- return self ._get_credentials (username , password )
308+ return self ._get_credentials (username , password , token )
304309
305310 def _get_credentials (
306311 self ,
307312 username : Optional [str ],
308313 password : Optional [str ],
314+ user_token : Optional [str ],
309315 ) -> bool :
316+ if user_token is not None :
317+ self .token = {"access_token" : user_token }
318+ self .authenticated = True
310319 if username is not None and password is not None :
311320 token_resp = self ._find_or_create_token (username , password )
312321
0 commit comments