|
| 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. |
0 commit comments