|
17 | 17 | from arangoasync.connection import Connection |
18 | 18 | from arangoasync.errno import HTTP_FORBIDDEN, HTTP_NOT_FOUND |
19 | 19 | from arangoasync.exceptions import ( |
| 20 | + AccessTokenCreateError, |
| 21 | + AccessTokenDeleteError, |
| 22 | + AccessTokenListError, |
20 | 23 | AnalyzerCreateError, |
21 | 24 | AnalyzerDeleteError, |
22 | 25 | AnalyzerGetError, |
|
107 | 110 | from arangoasync.result import Result |
108 | 111 | from arangoasync.serialization import Deserializer, Serializer |
109 | 112 | from arangoasync.typings import ( |
| 113 | + AccessToken, |
110 | 114 | CollectionInfo, |
111 | 115 | CollectionType, |
112 | 116 | DatabaseProperties, |
@@ -2130,6 +2134,96 @@ def response_handler(resp: Response) -> Json: |
2130 | 2134 |
|
2131 | 2135 | return await self._executor.execute(request, response_handler) |
2132 | 2136 |
|
| 2137 | + async def create_access_token( |
| 2138 | + self, |
| 2139 | + user: str, |
| 2140 | + name: str, |
| 2141 | + valid_until: int, |
| 2142 | + ) -> Result[AccessToken]: |
| 2143 | + """Create an access token for the given user. |
| 2144 | +
|
| 2145 | + Args: |
| 2146 | + user (str): The name of the user. |
| 2147 | + name (str): A name for the access token to make identification easier, |
| 2148 | + like a short description. |
| 2149 | + valid_until (int): A Unix timestamp in seconds to set the expiration date and time. |
| 2150 | +
|
| 2151 | + Returns: |
| 2152 | + AccessToken: Information about the created access token, including the token itself. |
| 2153 | +
|
| 2154 | + Raises: |
| 2155 | + AccessTokenCreateError: If the operation fails. |
| 2156 | +
|
| 2157 | + References: |
| 2158 | + - `create-an-access-token <https://docs.arango.ai/arangodb/stable/develop/http-api/authentication/#create-an-access-token>`__ |
| 2159 | + """ # noqa: E501 |
| 2160 | + data: Json = { |
| 2161 | + "name": name, |
| 2162 | + "valid_until": valid_until, |
| 2163 | + } |
| 2164 | + |
| 2165 | + request = Request( |
| 2166 | + method=Method.POST, |
| 2167 | + endpoint=f"/_api/token/{user}", |
| 2168 | + data=self.serializer.dumps(data), |
| 2169 | + ) |
| 2170 | + |
| 2171 | + def response_handler(resp: Response) -> AccessToken: |
| 2172 | + if not resp.is_success: |
| 2173 | + raise AccessTokenCreateError(resp, request) |
| 2174 | + result: Json = self.deserializer.loads(resp.raw_body) |
| 2175 | + return AccessToken(result) |
| 2176 | + |
| 2177 | + return await self._executor.execute(request, response_handler) |
| 2178 | + |
| 2179 | + async def delete_access_token(self, user: str, token_id: int) -> None: |
| 2180 | + """List all access tokens for the given user. |
| 2181 | +
|
| 2182 | + Args: |
| 2183 | + user (str): The name of the user. |
| 2184 | + token_id (int): The ID of the access token to delete. |
| 2185 | +
|
| 2186 | + Raises: |
| 2187 | + AccessTokenDeleteError: If the operation fails. |
| 2188 | +
|
| 2189 | + References: |
| 2190 | + - `delete-an-access-token <https://docs.arango.ai/arangodb/stable/develop/http-api/authentication/#delete-an-access-token>`__ |
| 2191 | + """ # noqa: E501 |
| 2192 | + request = Request( |
| 2193 | + method=Method.DELETE, endpoint=f"/_api/token/{user}/{token_id}" |
| 2194 | + ) |
| 2195 | + |
| 2196 | + def response_handler(resp: Response) -> None: |
| 2197 | + if not resp.is_success: |
| 2198 | + raise AccessTokenDeleteError(resp, request) |
| 2199 | + |
| 2200 | + await self._executor.execute(request, response_handler) |
| 2201 | + |
| 2202 | + async def list_access_tokens(self, user: str) -> Result[Jsons]: |
| 2203 | + """List all access tokens for the given user. |
| 2204 | +
|
| 2205 | + Args: |
| 2206 | + user (str): The name of the user. |
| 2207 | +
|
| 2208 | + Returns: |
| 2209 | + list: List of access tokens for the user. |
| 2210 | +
|
| 2211 | + Raises: |
| 2212 | + AccessTokenListError: If the operation fails. |
| 2213 | +
|
| 2214 | + References: |
| 2215 | + - `list-all-access-tokens <https://docs.arango.ai/arangodb/stable/develop/http-api/authentication/#list-all-access-tokens>`__ |
| 2216 | + """ # noqa: E501 |
| 2217 | + request = Request(method=Method.GET, endpoint=f"/_api/token/{user}") |
| 2218 | + |
| 2219 | + def response_handler(resp: Response) -> Jsons: |
| 2220 | + if not resp.is_success: |
| 2221 | + raise AccessTokenListError(resp, request) |
| 2222 | + result: Json = self.deserializer.loads(resp.raw_body) |
| 2223 | + return cast(Jsons, result["tokens"]) |
| 2224 | + |
| 2225 | + return await self._executor.execute(request, response_handler) |
| 2226 | + |
2133 | 2227 | async def tls(self) -> Result[Json]: |
2134 | 2228 | """Return TLS data (keyfile, clientCA). |
2135 | 2229 |
|
|
0 commit comments