diff --git a/documentation/docs/about/usage/basic-usage.md b/documentation/docs/about/usage/basic-usage.md index 7a49418d..c30455dd 100644 --- a/documentation/docs/about/usage/basic-usage.md +++ b/documentation/docs/about/usage/basic-usage.md @@ -56,6 +56,23 @@ or _"https://piwebapi/piwebapi"_. **Default**: Path to Equinor server correspond that works with Equinor servers. * `cache` (optional): [Cache](caching.md) data locally in order to avoid re-reading the same data multiple times. +For a PI Web API server that requires Basic Auth, set it once before creating the client. This setting applies only +to PI Web API connections in the current Python process and is kept in memory; it is not persisted to a file. +Basic Auth does not encrypt credentials, so use HTTPS and keep certificate verification enabled: + +``` python +import getpass +import tagreader +from tagreader.web_handlers import set_basic_auth + +set_basic_auth(username="myuser", password=getpass.getpass()) +c = tagreader.IMSClient( + datasource="myplant", + imstype="piwebapi", +) +c.connect() +``` + ## Connecting to data source After creating the client as described above, connect to the server with the `connect()` method. diff --git a/examples/piwebapi_basic_auth.py b/examples/piwebapi_basic_auth.py new file mode 100644 index 00000000..661bd33a --- /dev/null +++ b/examples/piwebapi_basic_auth.py @@ -0,0 +1,28 @@ +import getpass + +from tagreader.clients import IMSClient +from tagreader.utils import ReaderType +from tagreader.web_handlers import set_basic_auth + + +def main() -> None: + datasource = "JSV" # input("PI Web API data source: ").strip() + username = input("PI Web API username: ").strip() + if "\\" not in username and "@" not in username: + username = f"statoil.net\\{username}" + password = getpass.getpass("PI Web API password: ") + tag = "1901-A-20LIC1473A.CA_Y" + + set_basic_auth(username, password) + client = IMSClient(datasource=datasource, imstype="piwebapi", cache=None) + try: + # client.connect() + result = client.read(tags=tag, read_type=ReaderType.SNAPSHOT) + finally: + client.handler.session.close() + + print(result) + + +if __name__ == "__main__": + main() diff --git a/src/tagreader/web_handlers.py b/src/tagreader/web_handlers.py index 7c854eea..6ff1b9ea 100644 --- a/src/tagreader/web_handlers.py +++ b/src/tagreader/web_handlers.py @@ -1,3 +1,4 @@ +import base64 import hashlib import json import re @@ -13,6 +14,7 @@ import requests import urllib3 from Crypto.Hash import MD4 as _MD4 +from requests.auth import AuthBase from requests_kerberos import OPTIONAL, HTTPKerberosAuth from urllib3.exceptions import InsecureRequestWarning @@ -54,7 +56,30 @@ def get_verify_ssl() -> Union[bool, str]: return "/etc/ssl/certs/ca-bundle.trust.crt" -def get_auth_pi() -> HTTPKerberosAuth: +class _CachedBasicAuth(AuthBase): + __slots__ = ("_authorization",) + + def __init__(self, authorization: str): + self._authorization = authorization + + def __call__(self, request: requests.PreparedRequest) -> requests.PreparedRequest: + request.headers["Authorization"] = self._authorization + return request + + +_pi_basic_auth: Optional[_CachedBasicAuth] = None + + +def set_basic_auth(username: str, password: str) -> None: + global _pi_basic_auth + credentials = username.encode("latin-1") + b":" + password.encode("latin-1") + authorization = "Basic " + base64.b64encode(credentials).decode("ascii") + _pi_basic_auth = _CachedBasicAuth(authorization) + + +def get_auth_pi() -> Union[_CachedBasicAuth, HTTPKerberosAuth]: + if _pi_basic_auth is not None: + return _pi_basic_auth return HTTPKerberosAuth(mutual_authentication=OPTIONAL)