Skip to content

Commit c03acae

Browse files
committed
Added authentication documentation
1 parent 6ab77cc commit c03acae

File tree

3 files changed

+126
-9
lines changed

3 files changed

+126
-9
lines changed

docs/authentication.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
)

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Contents
6464
:maxdepth: 1
6565

6666
cursor
67+
authentication
6768
compression
6869
errors
6970
errno

tests/test_client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ async def test_client_jwt_auth(url, sys_db_name, basic_auth_root):
102102
async with ArangoClient(hosts=url) as client:
103103
await client.db(sys_db_name, auth_method="jwt", token=token, verify=True)
104104

105-
# successful authentication with both
106-
async with ArangoClient(hosts=url) as client:
107-
await client.db(
108-
sys_db_name,
109-
auth_method="jwt",
110-
auth=basic_auth_root,
111-
token=token,
112-
verify=True,
113-
)
105+
# successful authentication with both
106+
async with ArangoClient(hosts=url) as client:
107+
await client.db(
108+
sys_db_name,
109+
auth_method="jwt",
110+
auth=basic_auth_root,
111+
token=token,
112+
verify=True,
113+
)
114114

115115
# auth and token missing
116116
async with ArangoClient(hosts=url) as client:

0 commit comments

Comments
 (0)