Authentication
You must authenticate API calls using JSON Web Token (JWT), an open standard that defines a way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed, using either a secret (with the HMACalgorithm) or a public/private key pair using RSA or ECDSA.
Once the user is logged in, each subsequent request includes the JWT, which grants access to routes, services, and resources.
Parts of the token
JSON Web Tokens consist of three parts separated by dots (.) in the format xxxxx.yyyyy.zzzzz.
These parts are as follows.
Header
The header consists of the type of the token (always JWT), and the hashing algorithm being used, such as HMAC SHA256 or RSA.
For example:
{
"alg": "HS256",
"typ": "JWT"
}
This JSON is then Base64Url encoded.
Payload
The payload contains the claims, which are statements about the user and additional data. The JWT specification defines seven claims that can be included in a token. These are registered claim names, and they are:
- iss (issuer)
- sub (subject)
- aud (audience)
- exp
- nbf
- iat
- jti
You might then use public claim names, such as:
- auth_time
- acr
- nonce
There are also private claim names, which you can use to convey identity-related information, such as name or department.
Because public and private claims are not registered, you must avoid name collisions.
For example:
{
"sub": "1234567890",
"name": "Isaac Newton",
"admin": true
}
The payload is then Base64Url encoded.
Signature
To create the signature, take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.
For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
The signature is used to verify that the sender of the JWT is who it says it is.