http/client: provide initial support for (machine-to-machine) oauth support

PKCE might be useful too.
2 files changed, 21 insertions(+), 1 deletions(-)

M setup.py
M tshistory/http/client.py
M setup.py +2 -1
@@ 37,7 37,8 @@ setup(name='tshistory',
           'requests',
           'version-parser == 1.0.1',
           'importlib-metadata == 6.8',
-          'deprecation'
+          'deprecation',
+          'requests_auth'
       ],
       package_data={'tshistory': [
           'schema.sql',

          
M tshistory/http/client.py +19 -0
@@ 6,6 6,7 @@ import inireader
 import requests
 import pandas as pd
 import numpy as np
+from requests_auth import OAuth2ClientCredentials
 
 from tshistory.tsio import timeseries
 from tshistory.util import (

          
@@ 41,6 42,22 @@ def strft(dt):
     return dt.isoformat()
 
 
+def oauth2_auth(auth):
+    domain = auth['domain']
+    meta = requests.get(
+        f'https://{domain}/.well-known/openid-configuration'
+    ).json()
+    tokenurl = meta['token_endpoint']
+    clientid = auth['client_id']
+    clientsecret = auth['client_secret']
+    return OAuth2ClientCredentials(
+        audience=auth['uri'],
+        token_url=tokenurl,
+        client_id=clientid,
+        client_secret=clientsecret
+    )
+
+
 def unwraperror(func):
     """Method decorator to transform HTTP 418 errors into proper
     exceptions

          
@@ 81,6 98,8 @@ class httpclient:
         )
         if 'login' in auth:
             self.session.auth = auth['login'], auth['password']
+        elif 'client_id' in auth:
+            self.session.auth = oauth2_auth(auth)
 
     def __repr__(self):
         return f"tshistory-http-client(uri='{self.uri}')"