Doing RESTful APIs is indeed a little bit different than regular web interactions. Instead of doing credentials in a cookies and sessions, people are increasingly doing them as tokens sent in HTTP headers across domain boundaries. I’ll describe that below, JSON Web Tokens (JWT):
Here’s how the interactions work:
- You send a request to the API with no credentials. It replies back with with an HTTP forbidden.
- Your API client gets the forbidden and knows it needs to authenticate. It also knows the URL to authenticate against.
- It sends a POST to that URL with the login information. The server gets that information — perhaps a username, password, and your “organization” — and validates it.
- If valid, the server returns a response body containing the token, which is a payload wrapped in a secure envelope.
- The client gets the token and (here’s the first important difference) stores it. In memory, on disk…somewhere.
- The client then issues the first request again, this time sending the token in an HTTP header.
- The server gets the token and cryptographically unpacks it, then validates if the token is still valid (age, etc.)
Read the article for details on tokens.
In Pyramid, you can use the nice pyramid_jwtauth package to handle the JWT part, but it presumes you understand setting up Pyramid authentication. It also doesn’t automate things such as handling expired tokens and re-issue. I did a sample applicatino with a pyramid_jwtauth backend and an Angular frontend:
—Paul