This quickstart is for developers familiar with the NodeJS Koa framework who are looking for a quick intro into how they can add Approov into an existing project. Therefore this will guide you through the necessary steps for adding Approov to an existing NodeJS Koa API server.
- Why?
- How it Works?
- Requirements
- Approov Setup
- Approov Token Check
- Try the Approov Integration Example
To lock down your API server to your mobile app. Please read the brief summary in the Approov Overview at the root of this repo or visit our website for more details.
For more background, see the Approov Overview at the root of this repo.
The main functionality for the Aroov token check is in the file src/approov-protected-server/token-check/hello-server-protected.js. Take a look at the verifyApproovToken()
function to see the simple code for the check.
To complete this quickstart you will need both the NodeJS and the Approov CLI tool installed.
- NodeJS - Follow the official installation instructions from here
- Approov CLI - Follow our installation instructions and read more about each command and its options in the documentation reference
To use Approov with the NodeJS Koa API server we need a small amount of configuration. First, Approov needs to know the API domain that will be protected. Second, the NodeJS Koa API server needs the Approov Base64 encoded secret that will be used to verify the tokens generated by the Approov cloud service.
Approov needs to know the domain name of the API for which it will issue tokens.
Add it with:
approov api -add your.api.domain.com
NOTE: By default a symmetric key (HS256) is used to sign the Approov token on a valid attestation of the mobile app for each API domain it's added with the Approov CLI, so that all APIs will share the same secret and the backend needs to take care to keep this secret secure.
A more secure alternative is to use asymmetric keys (RS256 or others) that allows for a different keyset to be used on each API domain and for the Approov token to be verified with a public key that can only verify, but not sign, Approov tokens.
To implement the asymmetric key you need to change from using the symmetric HS256 algorithm to an asymmetric algorithm, for example RS256, that requires you to first add a new key, and then specify it when adding each API domain. Please visit Managing Key Sets on the Approov documentation for more details.
Adding the API domain also configures the dynamic certificate pinning setup, out of the box.
NOTE: By default the pin is extracted from the public key of the leaf certificate served by the domain, as visible to the box executing the Approov CLI command and the Approov servers.
Approov tokens are signed with a symmetric secret. To verify tokens, we need to grab the secret using the Approov secret command and plug it into the NodeJS Koa API server environment to check the signatures of the Approov Tokens that it processes.
Retrieve the Approov secret with:
approov secret -get base64
NOTE: The
approov secret
command requires an administration role to execute successfully.
Open the .env
file and add the Approov secret to the var:
APPROOV_BASE64_SECRET=approov_base64_secret_here
To check the Approov token we will use the auth0/node-jsonwebtoken package, but you are free to use another one of your preference.
Add this function to your code:
const jwt = require('jsonwebtoken')
const dotenv = require('dotenv').config()
if (dotenv.error) {
console.debug('FAILED TO PARSE `.env` FILE | ' + dotenv.error)
}
const approovBase64Secret = dotenv.parsed.APPROOV_BASE64_SECRET;
const approovSecret = Buffer.from(approovBase64Secret, 'base64')
const verifyApproovToken = async (ctx, next) => {
const approovToken = ctx.headers['approov-token']
if (!approovToken) {
// You may want to add some logging here.
ctx.status = 401
ctx.body = {}
return
}
// Decode the token with strict verification of the signature (['HS256']) to
// prevent against the `none` algorithm attack.
await jwt.verify(approovToken, approovSecret, { algorithms: ['HS256'] }, function(err, decoded) {
if (err) {
// You may want to add some logging here.
ctx.status = 401
ctx.body = {}
return
}
// The Approov token was successfully verified. We will add the claims to
// the request object to allow further use of them during the request
// processing.
ctx.approovTokenClaims = decoded
next()
})
}
Now you just need to add the function as a middleware for the endpoints you want to protected:
// @IMPORTANT: Always add the `verifyApproovToken` middleware function before
// your endpoints declaration.
//
// Using `["/"]` protects all endpoints in your API. Example to protect only
// specific endpoints: `["/checkout", "/payments", "/etc"]`.
// When adding an endpoint `/example` you are also protecting their child
// endpoints, like `/example/content`, `/example/content/:id`, etc.
router.use(["/"], verifyApproovToken)
NOTE: When the Approov token validation fails we return a
401
with an empty body, because we don't want to give clues to an attacker about the reason the request failed, and you can go even further by returning a400
.
A full working example for a simple Hello World server can be found at src/approov-protected-server/token-check.
The following examples below use cURL, but you can also use the Postman Collection to make the API requests. Just remember that you need to adjust the urls and tokens defined in the collection to match your deployment. Alternatively, the above README also contains instructions for using the preset dummy secret to test your Approov integration.
Generate a valid token example from the Approov Cloud service:
approov token -genExample your.api.domain.com
Then make the request with the generated token:
curl -i --request GET 'https://your.api.domain.com' \
--header 'Approov-Token: APPROOV_TOKEN_EXAMPLE_HERE'
The request should be accepted. For example:
HTTP/2 200
...
{"message": "Hello, World!"}
Generate an invalid token example from the Approov Cloud service:
approov token -type invalid -genExample your.api.domain.com
Then make the request with the generated token:
curl -i --request GET 'https://your.api.domain.com' \
--header 'Approov-Token: APPROOV_INVALID_TOKEN_EXAMPLE_HERE'
The above request should fail with an Unauthorized error. For example:
HTTP/2 401
...
{}