Giter VIP home page Giter VIP logo

hapi-auth-ip-whitelist's Introduction

hapi-auth-ip-whitelist

npm

Usage

Localhost

Only accept calls from localhost:

server.auth.strategy('localhost', 'ip-whitelist', ['127.0.0.1']);

NOTE: Third parameter of server.auth.strategy is options which must be an object.

To be used like

server.route({ 
  method: 'GET', 
  path: '/',
  handler(request, h) { return "That was from localhost!" }, 
  options: { auth: 'localhost' }
});

In the route receives a request from a different IP, it will respond a 401 unauthorized error with the message 192.168.0.102 is not a valid IP, where 192.168.0.102 is the IP of the request.

MercadoPago

You can also specify several IPs by passing a list instead. For example, consider the IPs to expect requests from, as specified by MercadoPago.

server.auth.strategy(
  'mercado-pago-webhook',
  'ip-whitelist',
  _.flatMap(
    ['209.225.49.*', '216.33.197.*', '216.33.196.*', '63.128.82.*', '63.128.83.*', '63.128.94.*'],
    (part) => _.times(256, (n) => _.replace(part, '*', _.toString(n)))
  )
);

Behind proxy

In case you are behind a proxy, use Hapi plugin therealyou. It will find the "real" IP in X-Forward headers and modify the request.info.remoteAddress.

server.register([
  {
    plugin: require('therealyou')
  },
  {
    plugin: require('hapi-auth-ip-whitelist')
  }
])

Example server

Start local example server with

npm start

then visit http://localhost:3000.

Successfully authenticated request http://localhost:3000/authenticated. Unauthenticated request http://localhost:3000/unauthenticated.

hapi-auth-ip-whitelist's People

Contributors

chamini2 avatar robman87 avatar

Watchers

 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.