Socket
Book a DemoInstallSign in
Socket

hapi-auth-ownership

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-auth-ownership

Ownership-based access control for your routes.

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
12
500%
Maintainers
1
Weekly downloads
 
Created
Source

hapi-auth-ownership

Build Status

Simple authentication scheme to verify resource ownership. Clients must pass the validation rule assigned to a route to be able to access it. The ownership-access scheme takes the following options:

  • rules - (required) an object with rules; each rule is a function with the signature function(request, credentials, callback) where:
    • request - is the Hapi request object of the request which is being authenticated
    • credentials - the credentials object, taken from request.auth.credentials
    • callback - a callback function with the signature function(err, isValid, credentials) where:
      • err - an internal error
      • isValid - true if the client is granted access
      • credentials - a credentials object passed back to the application in request.auth.credentials; if you do not include this, the plugin will pass the previous credentials back to Hapi
  • errorMessage - (optional) the error message that will be sent on invalid requests; set to You do not have access to this resource by default
  • companionStrategy - (required) the strategy that will be used to retrieve credentials; this is required because ownership checks require a credentials object
var users = {
  john: {
    id: '123',
    username: 'john',
    password: 'secret'
  }
};

var validate = function(request, username, password, callback) {
  var user = users[username];

  if (!user) {
    return callback(null, false);
  }

  callback(null, password === user.password, user);
};

server.register(require('hapi-auth-basic'), function(err) {
  server.auth.strategy('simple', 'basic', { validateFunc: validate }); // [1]

  server.register(require('hapi-auth-ownership'), function (err) {
    server.auth.strategy('ownership', 'ownership-access', {
      rules: {
        account: function(request, credentials, callback) {
          callback(null, request.params.id === credentials.account.id); // [2]
        }
      },
      errorMessage: 'OOPS!', // [3]
      companionStrategy: 'simple' // [4]
    });

    server.route({
      method: 'DELETE',
      path: '/account/{id}',
      config: {
        plugins: {
          hapiAuthOwnership: {
            ownershipRule: 'account' // [5]
          }
        }
      }
    });
  });
});
  • Define the companion strategy.
  • The authenticated user only has access to their own account.
  • Custom error message.
  • The credentials will be retrieved from this strategy.
  • Specify the rule to use. This will be taken from the options.rules object. If you don't specify an ownershipRule the request will be validated => the client has access.

Keywords

hapi

FAQs

Package last updated on 30 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.