Code Monkey home page Code Monkey logo

hapi-authorized-scope's Introduction

hapi-authorized-scope logo

Determine which scope authorized a user on a route.


InstallationUsage



Build Status hapi-authorized-scope Version

Follow @marcuspoehls for updates!


The Future Studio University supports development of this hapi plugin 馃殌
Join the Future Studio University and Skyrocket in Node.js


Introduction

The hapi-authorized-scope plugin determines and stores the scope that authorized an authenticated request. You'll find the scope authorizing the request in request.auth.authorizedScope.

In hapi, you鈥檒l find all auth-related details in request.auth, that鈥檚 the reason this plugin adds the authorizedScope property there:

{
  isAuthenticated: true,
  isAuthorized: true,
  credentials: { username: 'marcus', scope: ['admin', 'user'] },
  artifacts: null,
  strategy: 'test',
  mode: 'required',
  error: null,
  isInjected: true,
  authorizedScope: 'user'  // <-- added: the authorized scope that let the user access a route
}

Requirements

hapi v19 (or later) and Node.js v12 (or newer)

This plugin requires hapi v19 (or later) and Node.js v12 or newer.

Compatibility

Major Release hapi.js version Node.js version
v2 >=17 hapi >=12
v1 >=17 hapi >=8

Installation

Add hapi-authorized-scope as a dependency to your project:

npm i hapi-authorized-scope

Usage

The usage is pretty straightforward: register the plugin to your hapi server and that鈥檚 it:

await server.register({
  plugin: require('hapi-authorized-scope')
})

// went smooth like chocolate :)

hapi-authorized-scope extends the request lifecycle onPostAuth and finds the first scope in the authenticated credentials that authorizes the request to access the route.

In your route handlers or request lifecycle extension points, you may access the authorized scope like this:

{
  method: 'GET',
  path: '/profile',
  options: {
    handler: async (request, h) {
      const authorizedScope = request.auth.authorizedScope

      Logger.debug(`Scope authorizing the user to access this route: ${authorizedScope}`)

      return h.view('profile')
    }
  }
}

Enjoy!

Links & Resources

Contributing

  1. Create a fork
  2. Create your feature branch: git checkout -b my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request 馃殌

License

MIT 漏 Future Studio


futurestud.io 聽路聽 GitHub @futurestudio 聽路聽 Twitter @futurestud_io

hapi-authorized-scope's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar greenkeeper[bot] avatar marcuspoehls avatar

Stargazers

 avatar  avatar  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.