|
19 | 19 | from arango.connection import Connection |
20 | 20 | from arango.errno import HTTP_NOT_FOUND |
21 | 21 | from arango.exceptions import ( |
| 22 | + AccessTokenCreateError, |
| 23 | + AccessTokenDeleteError, |
| 24 | + AccessTokenListError, |
22 | 25 | AnalyzerCreateError, |
23 | 26 | AnalyzerDeleteError, |
24 | 27 | AnalyzerGetError, |
@@ -1158,6 +1161,89 @@ def response_handler(resp: Response) -> Json: |
1158 | 1161 |
|
1159 | 1162 | return self._execute(request, response_handler) |
1160 | 1163 |
|
| 1164 | + def create_access_token( |
| 1165 | + self, |
| 1166 | + user: str, |
| 1167 | + name: str, |
| 1168 | + valid_until: int, |
| 1169 | + ) -> Result[Json]: |
| 1170 | + """Create an access token for the given user. |
| 1171 | +
|
| 1172 | + :param user: The name of the user. |
| 1173 | + :type user: str |
| 1174 | + :param name: A name for the access token to make identification easier, |
| 1175 | + like a short description. |
| 1176 | + :type name: str |
| 1177 | + :param valid_until: A Unix timestamp in seconds to set the expiration |
| 1178 | + date and time. |
| 1179 | + :type valid_until: int |
| 1180 | +
|
| 1181 | + :return: Information about the created access token, including the token itself. |
| 1182 | + :rtype: dict |
| 1183 | +
|
| 1184 | + :raise arango.exceptions.AccessTokenCreateError: If the operations fails. |
| 1185 | + """ |
| 1186 | + data: Json = { |
| 1187 | + "name": name, |
| 1188 | + "valid_until": valid_until, |
| 1189 | + } |
| 1190 | + |
| 1191 | + request = Request( |
| 1192 | + method="post", |
| 1193 | + endpoint=f"/_api/token/{user}", |
| 1194 | + data=data, |
| 1195 | + ) |
| 1196 | + |
| 1197 | + def response_handler(resp: Response) -> Json: |
| 1198 | + if not resp.is_success: |
| 1199 | + raise AccessTokenCreateError(resp, request) |
| 1200 | + result: Json = resp.body |
| 1201 | + return result |
| 1202 | + |
| 1203 | + return self._executor.execute(request, response_handler) |
| 1204 | + |
| 1205 | + def delete_access_token(self, user: str, token_id: int) -> Result[None]: |
| 1206 | + """Delete an access token for the given user. |
| 1207 | +
|
| 1208 | + :param user: The name of the user. |
| 1209 | + :type user: str |
| 1210 | + :param token_id: The ID of the access token to delete. |
| 1211 | + :type token_id: int |
| 1212 | +
|
| 1213 | + :raise arango.exceptions.AccessTokenDeleteError: If the operation fails. |
| 1214 | + """ |
| 1215 | + request = Request( |
| 1216 | + method="delete", |
| 1217 | + endpoint=f"/_api/token/{user}/{token_id}", |
| 1218 | + ) |
| 1219 | + |
| 1220 | + def response_handler(resp: Response) -> None: |
| 1221 | + if not resp.is_success: |
| 1222 | + raise AccessTokenDeleteError(resp, request) |
| 1223 | + |
| 1224 | + return self._executor.execute(request, response_handler) |
| 1225 | + |
| 1226 | + def list_access_tokens(self, user: str) -> Result[Jsons]: |
| 1227 | + """List all access tokens for the given user. |
| 1228 | +
|
| 1229 | + :param user: The name of the user. |
| 1230 | + :type user: str |
| 1231 | +
|
| 1232 | + :return: List of access tokens for the user. |
| 1233 | + :rtype: list |
| 1234 | +
|
| 1235 | + :raise arango.exceptions.AccessTokenListError: If the operation fails. |
| 1236 | + """ |
| 1237 | + request = Request(method="get", endpoint=f"/_api/token/{user}") |
| 1238 | + |
| 1239 | + def response_handler(resp: Response) -> Jsons: |
| 1240 | + if not resp.is_success: |
| 1241 | + raise AccessTokenListError(resp, request) |
| 1242 | + result: Jsons = resp.body["tokens"] |
| 1243 | + return result |
| 1244 | + |
| 1245 | + return self._executor.execute(request, response_handler) |
| 1246 | + |
1161 | 1247 | def tls(self) -> Result[Json]: |
1162 | 1248 | """Return TLS data (server key, client-auth CA). |
1163 | 1249 |
|
|
0 commit comments