Git Product home page Git Product logo

pairing-unlock's Introduction

pairing-unlock

There are two docker services:

  • mongo
  • app, listens on port 8080

mongo service is supposed to be set up as a cluster and/or sharded database, but it's not done yet.

app service is an express application with API endpoints (see /src/routes.js).

Running

By default the app container is started in development mode, so users fixture is applied.

Three sample users are created:

  • phone = 111-222-333
  • phone = 123-456-789
  • phone = 444-555-666

These phones should be used to make API requests.

To run the service:

$ docker-compose up

1) To generate codes like they were created by a system:

$ curl -X "POST" "http://localhost:8080/user/codegen" \
       -H 'Content-Type: application/json; charset=utf-8' \
       -d $'{
    "otpCode": "111111",
    "phone": "111-222-333",
    "pairingCode": "222222"
  }'

2) To validate the generated OTP

$ curl -X "POST" "http://localhost:8080/user/otp" \
       -H 'Content-Type: application/json; charset=utf-8' \
       -d $'{
    "phone": "111-222-333",
    "code": "111111"
  }'

The response will return uuid param, save it as it will be used in further requests.

3) To validate the generated pairing code

$ curl -X "POST" "http://localhost:8080/user/pair" \
       -H 'Content-Type: application/json; charset=utf-8' \
       -d $'{
    "phone": "111-222-333",
    "uuid": "insert-the-uuid-from-previous-request",
    "code": "222222"
  }'

4) To unlock the vehicle

$ curl -X "POST" "http://localhost:8080/user/unlock" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "phone": "111-222-333",
  "uuid": "insert-the-uuid-from-previous-request"
}'

Production

To make it production-ready there are multiple things remaining to be done:

  • SSL connection to the DB
  • setup DB cluster/shards for scaling
  • setup proper role-based access to the DB, do not use the root user
  • use secure DB passwords, specify them in docker-compose.yml
  • use a more sophisticated system to authenticate customer agents
  • ensure app service can be scaled

Testing

Unfortunately there was no time to write proper E2E and/or unit tests.

But there is a simple test case ./test.js that would make requests from code generation to unlock.
For them to work the app container must run in development env.

I had no time to make a proper Docker setup, so to run them (while docker-compose is running):

$ npm install
$ node test.js

API endpoints

POST /user/otp

OTP code validation. Accepts and responses in JSON.

Expected params:

  • phone, the phone number identifying a user
  • code, the OTP code generated by system

Returns when validated:

  • message
  • uuid, the gadget identifying code that must be stored on the client app. Prevents making unauthorized requests from other gadgets.

After three invalid codes or OTP expiration (5 minutes), user is locked out and requires a customer agent to reset the process using a separate endpoint (see below).

POST /user/pair

Pair code validation. Accepts and responses in JSON.

Expected params:

  • phone, the phone number identifying a user
  • code, the pairing code generated by system and display in the vehicle
  • uuid, the uuid code stored by the client app after the OTP validation step

If a valid code wasn't entered after expiration time of 2 minutes, user is locked out and requires a customer agent to reset the process using a separate endpoint (see below).

POST /user/reset

Reset user pairing state.
Accessible only to customer agents authenticated by an auth token specified as an env variable AGENT_AUTH in docker-composer.yml.

It resets:

  • OTP code
  • pairing code
  • attempts history
  • verification, pairing, requiresReset statuses

Expected params:

  • phone, the phone number identifying a user
  • authToken, the authentication code

POST /user/unlock

Unlocks a vehicle associated with the user.

Expected params:

  • phone, the phone number identifying a user
  • uuid, the uuid code stored by the client app after the OTP validation step

POST /user/codegen only in dev env

Generate OTP and pairing codes for a user with the creation date set to now.

Expected params:

  • phone, the phone number identifying a user
  • otpCode, string, OTP code
  • pairingCode, string, pairing code

DELETE /user only in dev env

Delete and recreate users using fixture in /src/db.js.

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.