|
1 | 1 | from http import HTTPStatus |
| 2 | +from typing import Optional |
2 | 3 |
|
3 | 4 | from privx_api.base import BasePrivXAPI |
4 | 5 | from privx_api.enums import UrlEnum |
5 | 6 | from privx_api.response import PrivXAPIResponse |
| 7 | +from privx_api.utils import get_value |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class AuthAPI(BasePrivXAPI): |
@@ -91,3 +93,105 @@ def regenerate_idp_client_config(self, idp_id: str) -> PrivXAPIResponse: |
91 | 93 | UrlEnum.AUTH.REGENERATE_IDP_CLIENT, path_params={"idp_id": idp_id} |
92 | 94 | ) |
93 | 95 | return PrivXAPIResponse(response_status, HTTPStatus.OK, data) |
| 96 | + |
| 97 | + def get_user_sessions( |
| 98 | + self, |
| 99 | + user_id: str, |
| 100 | + offset: Optional[int] = None, |
| 101 | + limit: Optional[int] = None, |
| 102 | + sort_key: Optional[str] = None, |
| 103 | + sort_dir: Optional[str] = None, |
| 104 | + ) -> PrivXAPIResponse: |
| 105 | + """ |
| 106 | + Fetch valid sessions for the user. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + PrivXAPIResponse |
| 110 | + """ |
| 111 | + response_status, data = self._http_get( |
| 112 | + UrlEnum.AUTH.USER_SESSIONS, |
| 113 | + path_params={"user_id": user_id}, |
| 114 | + ) |
| 115 | + print(response_status, data) |
| 116 | + return PrivXAPIResponse(response_status, HTTPStatus.OK, data) |
| 117 | + |
| 118 | + def get_source_sessions( |
| 119 | + self, |
| 120 | + source_id: str, |
| 121 | + offset: Optional[int] = None, |
| 122 | + limit: Optional[int] = None, |
| 123 | + sort_key: Optional[str] = None, |
| 124 | + sort_dir: Optional[str] = None, |
| 125 | + ) -> PrivXAPIResponse: |
| 126 | + """ |
| 127 | + Fetch valid source sessions. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + PrivXAPIResponse |
| 131 | + """ |
| 132 | + response_status, data = self._http_get( |
| 133 | + UrlEnum.AUTH.SOURCE_SESSIONS, |
| 134 | + path_params={"source_id": source_id}, |
| 135 | + ) |
| 136 | + return PrivXAPIResponse(response_status, HTTPStatus.OK, data) |
| 137 | + |
| 138 | + def search_sessions( |
| 139 | + self, |
| 140 | + offset: Optional[int] = None, |
| 141 | + limit: Optional[int] = None, |
| 142 | + sort_key: Optional[str] = None, |
| 143 | + sort_dir: Optional[str] = None, |
| 144 | + search_payload: Optional[dict] = None, |
| 145 | + ) -> PrivXAPIResponse: |
| 146 | + """ |
| 147 | + Search for sessions. |
| 148 | +
|
| 149 | + Returns: |
| 150 | + PrivXAPIResponse |
| 151 | + """ |
| 152 | + search_params = self._get_search_params( |
| 153 | + offset=offset, limit=limit, sortkey=sort_key, sortdir=sort_dir |
| 154 | + ) |
| 155 | + |
| 156 | + response_status, data = self._http_post( |
| 157 | + UrlEnum.AUTH.SEARCH_SESSIONS, |
| 158 | + query_params=search_params, |
| 159 | + body=get_value(search_payload, dict()), |
| 160 | + ) |
| 161 | + return PrivXAPIResponse(response_status, HTTPStatus.OK, data) |
| 162 | + |
| 163 | + def terminate_session(self, session_id: str) -> PrivXAPIResponse: |
| 164 | + """ |
| 165 | + Terminate single session by ID. |
| 166 | +
|
| 167 | + Returns: |
| 168 | + PrivXAPIResponse |
| 169 | + """ |
| 170 | + response_status, data = self._http_post( |
| 171 | + UrlEnum.AUTH.TERMINATE_SESSION, |
| 172 | + path_params={"session_id": session_id}, |
| 173 | + ) |
| 174 | + return PrivXAPIResponse(response_status, HTTPStatus.OK, data) |
| 175 | + |
| 176 | + def terminate_user_sessions(self, user_id: str) -> PrivXAPIResponse: |
| 177 | + """ |
| 178 | + Terminate all sessions for a user. |
| 179 | +
|
| 180 | + Returns: |
| 181 | + PrivXAPIResponse |
| 182 | + """ |
| 183 | + response_status, data = self._http_post( |
| 184 | + UrlEnum.AUTH.TERMINATE_USER_SESSIONS, |
| 185 | + path_params={"user_id": user_id}, |
| 186 | + ) |
| 187 | + return PrivXAPIResponse(response_status, HTTPStatus.OK, data) |
| 188 | + |
| 189 | + def logout(self) -> PrivXAPIResponse: |
| 190 | + """ |
| 191 | + User logout. |
| 192 | +
|
| 193 | + Returns: |
| 194 | + PrivXAPIResponse |
| 195 | + """ |
| 196 | + response_status, data = self._http_post(UrlEnum.AUTH.LOGOUT) |
| 197 | + return PrivXAPIResponse(response_status, HTTPStatus.OK, data) |
0 commit comments