Skip to content

Commit

Permalink
Added support for self-signed certificates
Browse files Browse the repository at this point in the history
Support added for self-signed certificates. This addresses issue 1Password#95
  • Loading branch information
kwilsonmg committed Mar 5, 2024
1 parent cdefdd1 commit 351c846
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/onepasswordconnectsdk/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
class AsyncClient:
"""Python Async Client Class"""

def __init__(self, url: str, token: str) -> None:
def __init__(self, url: str, token: str, cert: str = None) -> None:
"""Initialize async client"""
self.url = url
self.token = token
self.session = self.create_session(url, token)
self.session = self.create_session(url, token, cert)
self.serializer = Serializer()

def create_session(self, url: str, token: str) -> httpx.AsyncClient:
def create_session(self, url: str, token: str, cert: str = None) -> httpx.AsyncClient:
if cert:
return httpx.AsyncClient(base_url=url, headers=self.build_headers(token), verify=cert)
return httpx.AsyncClient(base_url=url, headers=self.build_headers(token))

def build_headers(self, token: str) -> Dict[str, str]:
Expand Down
20 changes: 14 additions & 6 deletions src/onepasswordconnectsdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
class Client:
"""Python Client Class"""

def __init__(self, url: str, token: str) -> None:
def __init__(self, url: str, token: str, cert: str = None) -> None:
"""Initialize client"""
self.url = url
self.token = token
self.session = self.create_session(url, token)
self.session = self.create_session(url, token, cert)
self.serializer = Serializer()

def create_session(self, url: str, token: str) -> httpx.Client:
def create_session(self, url: str, token: str, cert: str = None) -> httpx.Client:
if cert:
return httpx.Client(base_url=url, headers=self.build_headers(token), verify=cert)
return httpx.Client(base_url=url, headers=self.build_headers(token))

def build_headers(self, token: str) -> Dict[str, str]:
Expand Down Expand Up @@ -381,19 +383,25 @@ def sanitize_for_serialization(self, obj):
return self.serializer.sanitize_for_serialization(obj)


def new_client(url: str, token: str, is_async: bool = False) -> Union[AsyncClient, Client]:
def new_client(url: str, token: str, is_async: bool = False, certificate: str = None) -> Union[AsyncClient, Client]:
"""Builds a new client for interacting with 1Password Connect
Parameters:
url: The url of the 1Password Connect API
token: The 1Password Service Account token
is_async: Initialize async or sync client
certificate: Optional path to custom certificate bundle for verification
Returns:
Client: The 1Password Connect client
"""
if is_async:
if is_async and certificate:
return AsyncClient(url, token, certificate)
elif is_async:
return AsyncClient(url, token)
return Client(url, token)
elif certificate:
return Client(url, token, certificate)
else:
return Client(url, token)


def new_client_from_environment(url: str = None) -> Union[AsyncClient, Client]:
Expand Down

0 comments on commit 351c846

Please sign in to comment.