Skip to content

A guide to available authentication helpers that library provides #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/auth.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Authentication
==============

The library uses requests library for RESTful API. Since majority of calls
requires authentication, xled library provides helpers to open authenticated
session or to attach authenticating handler. Here we discuss various approaches
from the most low level one to the highest level one.


ChallengeResponseAuth
---------------------

First approach uses `ChallengeResponseAuth` which could be used like this::

>>> import requests
>>>
>>> from xled.auth import ChallengeResponseAuth
>>> from xled.response import ApplicationResponse
>>>
>>> session = requests.Session()
>>> session.auth = ChallengeResponseAuth(
>>> login_url="/xled/v1/login",
>>> verify_url="/xled/v1/verify",
>>> hw_address=hw_address,
>>> )
>>>
>>> base_url = "http://{}/xled/v1/".format(hostname)
>>> url = urljoin(base_url, "summary")
>>> response = session.get(url)
>>> ApplicationResponse(response)
<Response [200]>

Class doesn't have knowledge of login and verification endpoints and therefore
they need to be passed when an object is constructed. Passing hardware address
is optional and if it is not passed, `challenge-response` is not validated,
which is also logged as debug message. To request an API endpoint full URL
needs to be passed every time.


BaseUrlChallengeResponseAuthSession
-----------------------------------

Second approach uses `BaseUrlChallengeResponseAuthSession` whose major
advantage is definition of base URL only once at object creation::

>>> from xled.auth import BaseUrlChallengeResponseAuthSession
>>> from xled.response import ApplicationResponse
>>>
>>> base_url = "http://{}/xled/v1/".format(hostname)
>>>
>>> session = BaseUrlChallengeResponseAuthSession(
>>> hw_address=hw_address, base_url=base_url
>>> )
>>>
>>> response = session.get("summary")
>>> ApplicationResponse(response)
<Response [200]>

Class have default API endpoints for login and verification so they don't need
to be passed this time. API endpoints can be specified by relative portion of
an URL. Behaviour of the object without hardware address passed is the same as
in previous example.
12 changes: 10 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ Command Line Interface
cli_guide


Python API Documentation / Guide
--------------------------------
Python API Guide
----------------

.. toctree::
:maxdepth: 2

auth

Python API Documentation
------------------------

.. toctree::
:maxdepth: 2
Expand Down