Git Product home page Git Product logo

authorization-middleware's Introduction

ExpressJS Authorization Middleware

Project Specifications:

  • Complete the middleware.js module. The module's default export is an Express JS middleware function. This function should parse the 'x-role' header and check whether the role has access to the route or not.

    • The scope of the route is passed to the middleware as a string in the format <SCOPE_NAME>.<ACTION_NAME>
    • Example: To check if the user has access to the create a task (in the tasks route), the scope passed to the middleware will be "tasks.create", where "tasks" is the scope name and "create" is the action name separated by a "." dot.
  • The roles-scopes mapping is stored in a file named roles.txt. The data is stored as a JSON string. The structure of each role-scope mapping object is as follows:

    {
        "role": "admin",
        "scopes": {
          "tasks": [
            "create",
            "getById"
          ]
        }
    }
    
    • role: This is the property which has to match with the x-role header value passed with the request. [STRING]
    • scopes: An object containing key-value pairs corresponding to each SCOPE_NAME of the system. If the passed scope name does not match with any of the keys defined in the object, the access is invalid and the server should respond back with the status code 403 while not passing the control to the next function. [OBJECT]
    • [scopeName]: Each dynamic key in the scopes object is a list of valid actions the user has access to in the current scope. [STRING ARRAY]
    • If the role passed in the x-role header is not present in the roles JSON, it does not have access and the server should respond back with the status code 403 while not passing the control to the next function.
    • If the role has access to the route, pass the control to the next function in the middleware stack.
  • Complete the tasks.js module to include all the routes with the appropriate middleware mapping mentioned below.

  • HINT: Make sure to trim the extra whitespaces from the JSON string before parsing it.

Test Requirements

  • The middleware function should be written in the file middleware.js, and the function should be the default export of the file.
  • The function must call the next() function once authorization is successful.

Environment

  • Node Version: ^12.18.2
  • Default Port: 8000

Read Only Files

  • test/*

Commands

  • run:
bash bin/env_setup && . $HOME/.nvm/nvm.sh && npm start
  • install:
bash bin/env_setup && . $HOME/.nvm/nvm.sh && npm install
  • test:
bash bin/env_setup && . $HOME/.nvm/nvm.sh && npm test

authorization-middleware's People

Contributors

yanakovtundev avatar

Watchers

 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.