Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

adonis-imperium

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adonis-imperium

This package is an **authorization provider** built on top of [imperium](https://github.com/mono-js/imperium).

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Adonis Imperium

This package is an authorization provider built on top of imperium and inspired by adonis-guard.

Getting Started

Install the package using the adonis CLI.

> adonis install adonis-imperium

Follow instruction that are displayed (or read them here).

Defining your authorization

Authorization must be defined inside the start/acl.js file. This file will be loaded only once when the server is launch.

Roles

Define the different roles of your applications.

Use Imperium.role('...', (ctx) => {}) to create a role.

The function will be used to determine if your user has the role (it can be asynchronous by returning a Promise).

For example, you can get your user from your database and return:

  • a Boolean (true if user has the corresponding role, otherwise false)
  • an Object to compare against route actions
  • an Array of objects
const Imperium = use('Imperium')

Imperium.role('Admin', ({ auth }) => {
  return auth.user.role === 'admin'
})

Imperium.role('Moderator', async () => {
  const posts = await Post.query().fetch()

  return posts.toJSON().map((post) => ({ post: post.id }))
})

Imperium.role('User', async ({ auth }) => {
  return { user: auth.user.id }
})

When returning an object, the keys will be compared against user actions params.

Actions

Use imperium.role('...') to get a role, and use can or is methods to give actions or inheritance from another role.

can(actionName, [params])

Define a user action with its params to match against.

Imperium.role('User')
  .can('updateUser', { user: '@' })

is(roleName, [params])

Inherit role's actions and overwrite its params.

Imperium.role('Admin')
  .is('User', { user: '*' }) // '*' means all, so admin can see and manage all users

Usage

Adonis Imperium automaticaly share an instance of the imperium instance in the context of each request. To validate the authorization of a user you simply need to extract it from the context.

// Controller
async show ({ imperium, params }) {
  const post = await Post.find(params.id)

  const can = await imperium.can('showPost', { post: params.id })

  if (!can) {
    // abort 401
  }

  // ...
}
// RouteValidator
async authorize () {
  const { imperium, params } = this.ctx

  const can = await imperium.can('showPost', { post: params.id })

  if (!can) {
    // abort 401
  }

  // ...
}

Middleware

You can also use the middlewares is and can in your routes.

Route.get('/admin/posts', 'Admin/PostController.index')
  .middleware(['auth', 'is:Admin'])

Route.get('/admin/posts', 'Admin/PostController.show')
  .middleware(['auth', 'can:showPost'])

API

imperium.can('Action', resource)
imperium.cannot('Action', resource)
imperium.is('Role')
imperium.isnot('Role')

FAQs

Package last updated on 01 Oct 2018

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc