API: Authentication - QutEcoacoustics/baw-server GitHub Wiki
API: Authentication
There are 4 ways to authenticate with the API:
1. Creating a session.
Posting authentication parameters in a body to POST /security.
The request body should be JSON payload:
{"user":{"email":"[email protected]","password":"iamsosecretyouwillforgetmewhenyoureadme"}}
The response, if successful, will be a JSON payload of the form:
{"meta":{"status":200,"message":"OK"},"data":{"auth_token":"xxxxxxxxxxxxxxxxxxxx","user_name":"reader","message":"Logged in successfully."}}
This process will:
- respond with a
set-cookieheader containing the_baw_sessioncookie - create (or recreate) an authentication token and return it in the response
The cookie's session expires after six hours.
2. Use a cookie
The cookie issued from the create session step can be sent back on each request. This is typically done by the browser.
Send the cookie in the Cookie header.
3. Use an authentication token
- Can be transmitted with the
Authorizationheader in the formatAuthorization: Token token="xxxxxxxxxxxxxxxxxxxx" - Via the
user_tokenquery string parameter on any URL e.gGET /projects/1?user_token=xxxxxxxxxxxxxxxxxxxx - The current token can be retrieved from
GET /security/user - Tokens are regularly refreshed (when a new session is created, or a session is deleted)
4. Using a JWT (JSON Web Token)
- JWTs are not issued by the API, there is no exposed method for generating them.
- JWTs are only currently issued for machine-to-machine service operations
- JWTs must be transmitted in the
Authorizationheader in the formatAuthorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - JWTs emitted by our site will have the following claims:
- A
subsubject claim identifying the user that is authenticated by this token - A
expexpiration claim that defaults to a 24-hour expiry - A
nbfnot before claim that is not set by default - An optional custom
resourceclaim that restricts the token to a particular resource (likeprojects,sites, etc...) - An optional custom
actionclaim that restricts the token to a particular action (likeshow,index,create, etc...)
- A
Note: both the resource and action claims are additional restrictions on access.
They don't bypass any of the normal authorization rules.