@@ -264,6 +264,19 @@ def start_session(self):
264
264
self ._get_session (self .__auth )
265
265
266
266
267
+ class TokenAuth (AuthBase ):
268
+ """Bearer Token Authentication"""
269
+
270
+ def __init__ (self , token : str ):
271
+ # setup any auth-related data here
272
+ self ._token = token
273
+
274
+ def __call__ (self , r : requests .PreparedRequest ):
275
+ # modify and return the request
276
+ r .headers ["authorization" ] = f"Bearer { self ._token } "
277
+ return r
278
+
279
+
267
280
class JIRA :
268
281
"""User interface to Jira.
269
282
@@ -325,7 +338,8 @@ def __init__(
325
338
self ,
326
339
server : str = None ,
327
340
options : Dict [str , Union [str , bool , Any ]] = None ,
328
- basic_auth : Union [None , Tuple [str , str ]] = None ,
341
+ basic_auth : Optional [Tuple [str , str ]] = None ,
342
+ token_auth : Optional [str ] = None ,
329
343
oauth : Dict [str , Any ] = None ,
330
344
jwt : Dict [str , Any ] = None ,
331
345
kerberos = False ,
@@ -347,8 +361,8 @@ def __init__(
347
361
or ``atlas-run-standalone`` commands. By default, this instance runs at
348
362
``http://localhost:2990/jira``. The ``options`` argument can be used to set the Jira instance to use.
349
363
350
- Authentication is handled with the ``basic_auth`` argument. If authentication is supplied (and is
351
- accepted by Jira), the client will remember it for subsequent requests.
364
+ Authentication is handled with the ``basic_auth`` or ``token_auth`` argument.
365
+ If authentication is supplied (and is accepted by Jira), the client will remember it for subsequent requests.
352
366
353
367
For quick command line access to a server, see the ``jirashell`` script included with this distribution.
354
368
@@ -369,8 +383,11 @@ def __init__(
369
383
* check_update -- Check whether using the newest python-jira library version.
370
384
* headers -- a dict to update the default headers the session uses for all API requests.
371
385
372
- basic_auth (Union[None, Tuple[str, str]]): A tuple of username and password to use when
386
+ basic_auth (Optional[ Tuple[str, str]]): A tuple of username and password to use when
373
387
establishing a session via HTTP BASIC authentication.
388
+
389
+ token_auth (Optional[str]): A string containing the token necessary for (PAT) bearer token authorization.
390
+
374
391
oauth (Optional[Any]): A dict of properties for OAuth authentication. The following properties are required:
375
392
376
393
* access_token -- OAuth access token for the user
@@ -466,6 +483,8 @@ def __init__(
466
483
self ._session .headers .update (self ._options ["headers" ])
467
484
elif jwt :
468
485
self ._create_jwt_session (jwt , timeout )
486
+ elif token_auth :
487
+ self ._create_token_session (token_auth , timeout )
469
488
elif kerberos :
470
489
self ._create_kerberos_session (timeout , kerberos_options = kerberos_options )
471
490
elif auth :
@@ -3412,6 +3431,20 @@ def _create_jwt_session(
3412
3431
self ._session .verify = bool (self ._options ["verify" ])
3413
3432
self ._session .auth = jwt_auth
3414
3433
3434
+ def _create_token_session (
3435
+ self ,
3436
+ token_auth : str ,
3437
+ timeout : Optional [Union [Union [float , int ], Tuple [float , float ]]],
3438
+ ):
3439
+ """
3440
+ Creates token-based session.
3441
+ Header structure: "authorization": "Bearer <token_auth>"
3442
+ """
3443
+ verify = self ._options ["verify" ]
3444
+ self ._session = ResilientSession (timeout = timeout )
3445
+ self ._session .verify = verify
3446
+ self ._session .auth = TokenAuth (token_auth )
3447
+
3415
3448
def _set_avatar (self , params , url , avatar ):
3416
3449
data = {"id" : avatar }
3417
3450
return self ._session .put (url , params = params , data = json .dumps (data ))
0 commit comments