Coretex
network_manager.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from typing import Optional
19 
20 from .network_response import NetworkResponse
21 from .network_manager_base import NetworkManagerBase
22 from ..configuration import UserConfiguration
23 
24 
25 class NetworkManager(NetworkManagerBase):
26 
27  """
28  Subclass of NetworkManagerBase intended to be used
29  by a normal user. Contains functionality related to user
30  and authenticating as a normal user.
31  """
32 
33  def __init__(self) -> None:
34  super().__init__()
35 
36  self._userApiToken_userApiToken: Optional[str] = None
37  self._userRefreshToken_userRefreshToken: Optional[str] = None
38  self._username_username: Optional[str] = None
39  self._password_password: Optional[str] = None
40 
41  try:
42  userConfig = UserConfiguration.load()
43  self._userApiToken_userApiToken = userConfig.token
44  self._userRefreshToken_userRefreshToken = userConfig.refreshToken
45  self._username_username = userConfig.username
46  self._password_password = userConfig.password
47  except:
48  pass
49 
50  @property
51  def _apiToken(self) -> Optional[str]:
52  return self._userApiToken_userApiToken
53 
54  @_apiToken.setter
55  def _apiToken(self, value: Optional[str]) -> None:
56  self._userApiToken_userApiToken = value
57 
58  @property
59  def _refreshToken(self) -> Optional[str]:
60  return self._userRefreshToken_userRefreshToken
61 
62  @_refreshToken.setter
63  def _refreshToken(self, value: Optional[str]) -> None:
64  self._userRefreshToken_userRefreshToken = value
65 
66  @property
67  def hasStoredCredentials(self) -> bool:
68  return self._username_username is not None and self._password_password is not None
69 
70  def authenticate(self, username: str, password: str, storeCredentials: bool = True) -> NetworkResponse:
71  """
72  Authenticates user with provided credentials
73 
74  Parameters
75  ----------
76  username : str
77  Coretex.ai username
78  password : str
79  Coretex.ai password
80  storeCredentials : bool
81  If true credentials will be stored in User object for reuse
82 
83  Returns
84  -------
85  NetworkResponse -> NetworkResponse object containing the full response info
86 
87  Example
88  -------
89  >>> from coretex.networking import networkManager
90  \b
91  >>> response = networkManager.authenticate(username = "dummy@coretex.ai", password = "123456")
92  >>> if response.hasFailed():
93  print("Failed to authenticate")
94  """
95 
96  if storeCredentials:
97  self._username_username = username
98  self._password_password = password
99 
100  # authenticate using credentials stored in requests.Session.auth
101  return super().authenticate(username, password, storeCredentials)
102 
103  def authenticateWithStoredCredentials(self) -> NetworkResponse:
104  """
105  Authenticates user with credentials stored inside User object
106 
107  Returns
108  -------
109  NetworkResponse -> NetworkResponse object containing the full response info
110 
111  Raises
112  ------
113  ValueError -> if credentials are not found
114  """
115 
116  if self._username_username is None or self._password_password is None:
117  raise ValueError(">> [Coretex] Credentials not stored")
118 
119  return self.authenticateauthenticate(self._username_username, self._password_password)
120 
121 
122 networkManager: NetworkManagerBase = NetworkManager()
NetworkResponse authenticate(self, str username, str password, bool storeCredentials=True)