1313import requests
1414from requests .auth import HTTPBasicAuth
1515from requests .exceptions import Timeout
16+ from . import oAuthClient as oauth
1617
1718logger = logging .getLogger ('inventree' )
1819
@@ -45,6 +46,9 @@ def __init__(self, host=None, **kwargs):
4546 token - Authentication token (if provided, username/password are ignored)
4647 token-name - Name of the token to use (default = 'inventree-python-client')
4748 use_token_auth - Use token authentication? (default = True)
49+ use_oidc_auth - Use OIDC authentication? (default = False)
50+ oidc_client_id - OIDC client ID (defaults to InvenTree public client)
51+ oidc_scopes - OIDC scopes (default = ['openid', 'g:read'])
4852 verbose - Print extra debug messages (default = False)
4953 strict - Enforce strict HTTPS certificate checking (default = True)
5054 timeout - Set timeout to use (in seconds). Default: 10
@@ -56,6 +60,9 @@ def __init__(self, host=None, **kwargs):
5660 INVENTREE_API_PASSWORD - Password
5761 INVENTREE_API_TOKEN - User access token
5862 INVENTREE_API_TIMEOUT - Timeout value, in seconds
63+ INVENTREE_API_OIDC - Use OIDC
64+ INVENTREE_API_OIDC_CLIENT_ID - OIDC client ID
65+ INVENTREE_API_OIDC_SCOPES - OIDC scopes
5966 """
6067
6168 self .setHostName (host or os .environ .get ('INVENTREE_API_HOST' , None ))
@@ -68,8 +75,11 @@ def __init__(self, host=None, **kwargs):
6875 self .timeout = kwargs .get ('timeout' , os .environ .get ('INVENTREE_API_TIMEOUT' , 10 ))
6976 self .proxies = kwargs .get ('proxies' , dict ())
7077 self .strict = bool (kwargs .get ('strict' , True ))
78+ self .oidc_client_id = kwargs .get ('oidc_client_id' , os .environ .get ('INVENTREE_API_OIDC_CLIENT_ID' , 'zDFnsiRheJIOKNx6aCQ0quBxECg1QBHtVFDPloJ6' ))
79+ self .oidc_scopes = kwargs .get ('oidc_scopes' , os .environ .get ('INVENTREE_API_OIDC_SCOPES' , ['openid' , 'g:read' ]))
7180
7281 self .use_token_auth = kwargs .get ('use_token_auth' , True )
82+ self .use_oidc_auth = kwargs .get ('use_oidc_auth' , os .environ .get ('INVENTREE_API_OIDC' , None ))
7383 self .verbose = kwargs .get ('verbose' , False )
7484
7585 self .auth = None
@@ -132,9 +142,10 @@ def connect(self):
132142 if not self .testAuth ():
133143 raise ConnectionError ("Authentication at InvenTree server failed" )
134144
135- if self .use_token_auth :
136- if not self .token :
137- self .requestToken ()
145+ if self .use_token_auth and not self .token :
146+ self .requestToken ()
147+ elif self .use_oidc_auth and not self .token :
148+ self .requestOidcToken ()
138149
139150 def constructApiUrl (self , endpoint_url ):
140151 """Construct an API endpoint URL based on the provided API URL.
@@ -273,6 +284,14 @@ def requestToken(self):
273284
274285 return self .token
275286
287+ def requestOidcToken (self ):
288+ """Return authentication token from the server using OIDC."""
289+ client = oauth .OAuthClient (self .base_url , self .oidc_client_id , self .oidc_scopes )
290+ self .token = client ._access_token
291+
292+ return self .token
293+
294+
276295 def request (self , api_url , ** kwargs ):
277296 """ Perform a URL request to the Inventree API """
278297
@@ -316,7 +335,7 @@ def request(self, api_url, **kwargs):
316335 'timeout' : kwargs .get ('timeout' , self .timeout ),
317336 }
318337
319- if self .use_token_auth and self .token :
338+ if ( self .use_token_auth or self . use_oidc_auth ) and self .token :
320339 headers ['AUTHORIZATION' ] = f'Token { self .token } '
321340 auth = None
322341 else :
0 commit comments