Giter VIP home page Giter VIP logo

quickstart-nodejs-koa-token-check's Introduction

Approov QuickStart - NodeJS Koa Token Check

Approov is an API security solution used to verify that requests received by your backend services originate from trusted versions of your mobile apps.

This repo implements the Approov server-side request verification code for the NodeJS Koa framework, which performs the Approov token check before allowing valid traffic to be processed by the API endpoint.

Approov Integration Quickstart

The quickstart was tested with the following Operating Systems:

  • Ubuntu 20.04
  • MacOS Big Sur
  • Windows 10 WSL2 - Ubuntu 20.04

First, setup the Approov CLI.

Now, register the API domain for which Approov will issues tokens:

approov api -add api.example.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.

Next, enable your Approov admin role with:

eval `approov role admin`

For the Windows powershell:

set APPROOV_ROLE=admin:___YOUR_APPROOV_ACCOUNT_NAME_HERE___

Now, get your Approov Secret with the Approov CLI:

approov secret -get base64

Next, add the Approov secret to your project .env file:

APPROOV_BASE64_SECRET=approov_base64_secret_here

Now, add to your package.json file the JWT dependency:

"jsonwebtoken": "^8.5.1"

Next, in your code require the JWT dependency:

const jwt = require('jsonwebtoken')

Now, read the Approov secret from the environment and put it into a constant:

const dotenv = require('dotenv').config()
const approovBase64Secret = dotenv.parsed.APPROOV_BASE64_SECRET;
const approovSecret = Buffer.from(approovBase64Secret, 'base64')

Next, verify the Approov token:

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()
  })
}

Finally, add the function as a middleware to protect all your API endpoints with the Approov token check:

// @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)

Not enough details in the bare bones quickstart? No worries, check the detailed quickstarts that contain a more comprehensive set of instructions, including how to test the Approov integration.

More Information

System Clock

In order to correctly check for the expiration times of the Approov tokens is very important that the backend server is synchronizing automatically the system clock over the network with an authoritative time source. In Linux this is usually done with a NTP server.

Issues

If you find any issue while following our instructions then just report it here, with the steps to reproduce it, and we will sort it out and/or guide you to the correct path.

Useful Links

If you wish to explore the Approov solution in more depth, then why not try one of the following links as a jumping off point:

quickstart-nodejs-koa-token-check's People

Contributors

exadra37 avatar richardmtaylor avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.