Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions documentation/docs/about/usage/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions examples/piwebapi_basic_auth.py
Original file line number Diff line number Diff line change
@@ -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()
27 changes: 26 additions & 1 deletion src/tagreader/web_handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import hashlib
import json
import re
Expand All @@ -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

Expand Down Expand Up @@ -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)


Expand Down
Loading