Skip to content

Commit 0a804f7

Browse files
authored
reduce code duplication and configure _session auth after other config (#1623)
1 parent a69da77 commit 0a804f7

File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

jira/client.py

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -527,24 +527,9 @@ def __init__(
527527
self._try_magic()
528528

529529
assert isinstance(self._options["headers"], dict) # for mypy benefit
530-
self._session: ResilientSession # for mypy benefit
531-
if oauth:
532-
self._create_oauth_session(oauth, timeout)
533-
elif basic_auth:
534-
self._create_http_basic_session(*basic_auth, timeout=timeout)
535-
elif jwt:
536-
self._create_jwt_session(jwt, timeout)
537-
elif token_auth:
538-
self._create_token_session(token_auth, timeout)
539-
elif kerberos:
540-
self._create_kerberos_session(timeout, kerberos_options=kerberos_options)
541-
elif auth:
542-
self._create_cookie_auth(auth, timeout)
543-
# always log in for cookie based auth, as we need a first request to be logged in
544-
validate = True
545-
else:
546-
self._session = ResilientSession(timeout=timeout)
547530

531+
# Create Session object and update with config options first
532+
self._session = ResilientSession(timeout=timeout)
548533
# Add the client authentication certificate to the request if configured
549534
self._add_client_cert_to_session()
550535
# Add the SSL Cert to the request if configured
@@ -560,6 +545,23 @@ def __init__(
560545
if proxies:
561546
self._session.proxies = proxies
562547

548+
# Setup the Auth last,
549+
# so that if any handlers take a copy of the session obj it will be ready
550+
if oauth:
551+
self._create_oauth_session(oauth)
552+
elif basic_auth:
553+
self._create_http_basic_session(*basic_auth)
554+
elif jwt:
555+
self._create_jwt_session(jwt)
556+
elif token_auth:
557+
self._create_token_session(token_auth)
558+
elif kerberos:
559+
self._create_kerberos_session(kerberos_options=kerberos_options)
560+
elif auth:
561+
self._create_cookie_auth(auth)
562+
# always log in for cookie based auth, as we need a first request to be logged in
563+
validate = True
564+
563565
self.auth = auth
564566
if validate:
565567
# This will raise an Exception if you are not allowed to login.
@@ -619,17 +621,12 @@ def _is_cloud(self) -> bool:
619621
"""Return whether we are on a Cloud based Jira instance."""
620622
return self.deploymentType in ("Cloud",)
621623

622-
def _create_cookie_auth(
623-
self,
624-
auth: tuple[str, str],
625-
timeout: None | float | tuple[float, float] | tuple[float, None] | None = None,
626-
):
624+
def _create_cookie_auth(self, auth: tuple[str, str]):
627625
warnings.warn(
628626
"Use OAuth or Token based authentication "
629627
+ "instead of Cookie based Authentication.",
630628
DeprecationWarning,
631629
)
632-
self._session = ResilientSession(timeout=timeout)
633630
self._session.auth = JiraCookieAuth(
634631
session=self._session,
635632
session_api_url="{server}{auth_url}".format(**self._options),
@@ -3678,28 +3675,19 @@ def kill_websudo(self) -> Response | None:
36783675
return None
36793676

36803677
# Utilities
3681-
def _create_http_basic_session(
3682-
self,
3683-
username: str,
3684-
password: str,
3685-
timeout: None | float | tuple[float, float] | tuple[float, None] | None = None,
3686-
):
3678+
def _create_http_basic_session(self, username: str, password: str):
36873679
"""Creates a basic http session.
36883680
36893681
Args:
36903682
username (str): Username for the session
36913683
password (str): Password for the username
3692-
timeout (Optional[int]): If set determines the connection/read timeout delay for the Session.
36933684
36943685
Returns:
36953686
ResilientSession
36963687
"""
3697-
self._session = ResilientSession(timeout=timeout)
36983688
self._session.auth = (username, password)
36993689

3700-
def _create_oauth_session(
3701-
self, oauth, timeout: float | int | tuple[float, float] | None
3702-
):
3690+
def _create_oauth_session(self, oauth: dict[str, Any]):
37033691
from oauthlib.oauth1 import SIGNATURE_HMAC_SHA1
37043692
from requests_oauthlib import OAuth1
37053693

@@ -3710,13 +3698,11 @@ def _create_oauth_session(
37103698
resource_owner_key=oauth["access_token"],
37113699
resource_owner_secret=oauth["access_token_secret"],
37123700
)
3713-
self._session = ResilientSession(timeout)
37143701
self._session.auth = oauth_instance
37153702

37163703
def _create_kerberos_session(
37173704
self,
3718-
timeout: None | float | tuple[float, float] | tuple[float, None] | None,
3719-
kerberos_options=None,
3705+
kerberos_options: dict[str, Any] = None,
37203706
):
37213707
if kerberos_options is None:
37223708
kerberos_options = {}
@@ -3733,7 +3719,6 @@ def _create_kerberos_session(
37333719
% kerberos_options["mutual_authentication"]
37343720
)
37353721

3736-
self._session = ResilientSession(timeout=timeout)
37373722
self._session.auth = HTTPKerberosAuth(
37383723
mutual_authentication=mutual_authentication
37393724
)
@@ -3769,9 +3754,7 @@ def _timestamp(dt: datetime.timedelta = None):
37693754
t += dt
37703755
return calendar.timegm(t.timetuple())
37713756

3772-
def _create_jwt_session(
3773-
self, jwt, timeout: float | int | tuple[float, float] | None
3774-
):
3757+
def _create_jwt_session(self, jwt: dict[str, Any]):
37753758
try:
37763759
jwt_auth = JWTAuth(jwt["secret"], alg="HS256")
37773760
except NameError as e:
@@ -3786,19 +3769,13 @@ def _create_jwt_session(
37863769
jwt_auth.add_field("qsh", QshGenerator(self._options["context_path"]))
37873770
for f in jwt["payload"].items():
37883771
jwt_auth.add_field(f[0], f[1])
3789-
self._session = ResilientSession(timeout=timeout)
37903772
self._session.auth = jwt_auth
37913773

3792-
def _create_token_session(
3793-
self,
3794-
token_auth: str,
3795-
timeout: None | float | tuple[float, float] | tuple[float, None] | None = None,
3796-
):
3774+
def _create_token_session(self, token_auth: str):
37973775
"""Creates token-based session.
37983776
37993777
Header structure: "authorization": "Bearer <token_auth>".
38003778
"""
3801-
self._session = ResilientSession(timeout=timeout)
38023779
self._session.auth = TokenAuth(token_auth)
38033780

38043781
def _set_avatar(self, params, url, avatar):

0 commit comments

Comments
 (0)