|
| 1 | +Authentication |
| 2 | +-------------- |
| 3 | + |
| 4 | +Two HTTP authentication methods are supported out of the box: |
| 5 | +- basic username and password authentication |
| 6 | +- JSON Web Tokens (JWT) |
| 7 | + |
| 8 | +Basic Authentication |
| 9 | +==================== |
| 10 | + |
| 11 | +This is the default authentication method. |
| 12 | + |
| 13 | +**Example:** |
| 14 | + |
| 15 | +.. code-block:: python |
| 16 | +
|
| 17 | + from arangoasync import ArangoClient |
| 18 | + from arangoasync.auth import Auth |
| 19 | +
|
| 20 | + # Initialize the client for ArangoDB. |
| 21 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 22 | + auth = Auth( |
| 23 | + username="root", |
| 24 | + password="passwd", |
| 25 | + encoding="utf-8" # Encoding for the password, default is utf-8. |
| 26 | + ) |
| 27 | +
|
| 28 | + # Connect to "test" database as root user. |
| 29 | + db = await client.db( |
| 30 | + "test", # database name |
| 31 | + auth_method="basic", # use basic authentication (default) |
| 32 | + auth=auth, # authentication details |
| 33 | + verify=True, # verify the connection (optional) |
| 34 | + ) |
| 35 | +
|
| 36 | +JSON Web Tokens (JWT) |
| 37 | +===================== |
| 38 | + |
| 39 | +You can obtain the JWT token from the use server using username and password. |
| 40 | +Upon expiration, the token gets refreshed automatically and requests are retried. |
| 41 | +The client and server clocks must be synchronized for the automatic refresh |
| 42 | +to work correctly. |
| 43 | + |
| 44 | +**Example:** |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + from arangoasync import ArangoClient |
| 49 | + from arangoasync.auth import Auth |
| 50 | +
|
| 51 | + # Initialize the client for ArangoDB. |
| 52 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 53 | + auth = Auth(username="root", password="passwd") |
| 54 | +
|
| 55 | + # Successful authentication with auth only |
| 56 | + db = await client.db( |
| 57 | + "test", |
| 58 | + auth_method="jwt", |
| 59 | + auth=auth, |
| 60 | + verify=True, |
| 61 | + ) |
| 62 | +
|
| 63 | + # Now you have the token on hand. |
| 64 | + token = db.connection.token |
| 65 | +
|
| 66 | + # You can use the token directly. |
| 67 | + db = await client.db("test", auth_method="jwt", token=token, verify=True) |
| 68 | +
|
| 69 | + # In order to allow the token to be automatically refreshed, you should use both auth and token. |
| 70 | + db = await client.db( |
| 71 | + "test", |
| 72 | + auth_method="jwt", |
| 73 | + auth=auth, |
| 74 | + token=token, |
| 75 | + verify=True, |
| 76 | + ) |
| 77 | +
|
| 78 | + # Force a token refresh. |
| 79 | + await db.connection.refresh_token() |
| 80 | + new_token = db.connection.token |
| 81 | +
|
| 82 | + # Log in with the first token. |
| 83 | + db2 = await client.db( |
| 84 | + "test", |
| 85 | + auth_method="jwt", |
| 86 | + token=token, |
| 87 | + verify=True, |
| 88 | + ) |
| 89 | +
|
| 90 | + # You can manually set tokens. |
| 91 | + db2.connection.token = new_token |
| 92 | + await db2.connection.ping() |
| 93 | +
|
| 94 | +
|
| 95 | +If you configured a superuser token, you don't need to provide any credentials. |
| 96 | + |
| 97 | +**Example:** |
| 98 | + |
| 99 | +.. code-block:: python |
| 100 | +
|
| 101 | + from arangoasync import ArangoClient |
| 102 | + from arangoasync.auth import JwtToken |
| 103 | +
|
| 104 | + # Initialize the client for ArangoDB. |
| 105 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 106 | +
|
| 107 | + # Generate a JWT token for authentication. You must know the "secret". |
| 108 | + token = JwtToken.generate_token("secret") |
| 109 | +
|
| 110 | + # Superuser authentication, no need for the auth parameter. |
| 111 | + db = await client.db( |
| 112 | + "test", |
| 113 | + auth_method="superuser", |
| 114 | + token=token, |
| 115 | + verify=True |
| 116 | + ) |
0 commit comments