toymachiner62 / hapi-authorization

ACL plugin for hapijs
MIT License
80 stars 25 forks source link

List ACL on endpoints #29

Closed rickysullivan closed 5 years ago

rickysullivan commented 5 years ago

I'm writing a /help section for my API.

I wanted to show what roles are assigned to each endpoint.

Is this possible?

const _ = require("lodash");

exports.plugin = {
  name: "help-plugin",
  version: "v1",
  register: server => {
    server.route({
      method: "GET",
      path: "/help",
      options: {
        description: "List all API endpoints",
        handler: request => {
          const routes = server.table().map(route => {
            const {
              public: { method, path },
              settings: { description = "", tags = [] }
            } = { ...route };

            let routeInfo = {};

            if (
              _.includes(tags, request.auth.isAuthenticated ? "" : "private") ||
              _.includes(tags, "protected")
            ) {
              routeInfo = null;
            } else {
              routeInfo.method = method.toUpperCase();
              routeInfo.path = path;
              routeInfo.description = description;
              routeInfo.access = //ROLES TO GO HERE//
            }

            return routeInfo;
          });
          return _(routes)
            .compact()
            .sortBy("path")
            .values();
        }
      }
    });
  }
};

Before using this plugin, I was defining my auth on each route and grabbing auth.access.scope.required. Now that my ACL is defined at a plugin level, I can't seem to find a way to pull this information.