Skip to content

Commit 0631067

Browse files
committed
A guide to available authentication helpers that library provides
1 parent 34f5822 commit 0631067

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

docs/auth.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Authentication
2+
==============
3+
4+
The library uses requests library for RESTful API. Since majority of calls
5+
requires authentication, xled library provides helpers to open authenticated
6+
session or to attach authenticating handler. Here we discuss various approaches
7+
from the most low level one to the highest level one.
8+
9+
10+
ChallengeResponseAuth
11+
---------------------
12+
13+
First approach uses `ChallengeResponseAuth` which could be used like this::
14+
15+
>>> import requests
16+
>>>
17+
>>> from xled.auth import ChallengeResponseAuth
18+
>>> from xled.response import ApplicationResponse
19+
>>>
20+
>>> session = requests.Session()
21+
>>> session.auth = ChallengeResponseAuth(
22+
>>> login_url="/xled/v1/login",
23+
>>> verify_url="/xled/v1/verify",
24+
>>> hw_address=hw_address,
25+
>>> )
26+
>>>
27+
>>> base_url = "http://{}/xled/v1/".format(hostname)
28+
>>> url = urljoin(base_url, "summary")
29+
>>> response = session.get(url)
30+
>>> ApplicationResponse(response)
31+
<Response [200]>
32+
33+
Class doesn't have knowledge of login and verification endpoints and therefore
34+
they need to be passed when an object is constructed. Passing hardware address
35+
is optional and if it is not passed, `challenge-response` is not validated,
36+
which is also logged as debug message. To request an API endpoint full URL
37+
needs to be passed every time.
38+
39+
40+
BaseUrlChallengeResponseAuthSession
41+
-----------------------------------
42+
43+
Second approach uses `BaseUrlChallengeResponseAuthSession` whose major
44+
advantage is definition of base URL only once at object creation::
45+
46+
>>> from xled.auth import BaseUrlChallengeResponseAuthSession
47+
>>> from xled.response import ApplicationResponse
48+
>>>
49+
>>> base_url = "http://{}/xled/v1/".format(hostname)
50+
>>>
51+
>>> session = BaseUrlChallengeResponseAuthSession(
52+
>>> hw_address=hw_address, base_url=base_url
53+
>>> )
54+
>>>
55+
>>> response = session.get("summary")
56+
>>> ApplicationResponse(response)
57+
<Response [200]>
58+
59+
Class have default API endpoints for login and verification so they don't need
60+
to be passed this time. API endpoints can be specified by relative portion of
61+
an URL. Behaviour of the object without hardware address passed is the same as
62+
in previous example.

docs/index.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ Command Line Interface
2929
cli_guide
3030

3131

32-
Python API Documentation / Guide
33-
--------------------------------
32+
Python API Guide
33+
----------------
34+
35+
.. toctree::
36+
:maxdepth: 2
37+
38+
auth
39+
40+
Python API Documentation
41+
------------------------
3442

3543
.. toctree::
3644
:maxdepth: 2

0 commit comments

Comments
 (0)