Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support repeatable directive #50

Closed
Eomm opened this issue Nov 22, 2021 · 1 comment · Fixed by #53
Closed

support repeatable directive #50

Eomm opened this issue Nov 22, 2021 · 1 comment · Fixed by #53

Comments

@Eomm
Copy link
Contributor

Eomm commented Nov 22, 2021

At the moment a repeatable directive is executed once: the first directive only.

The 2nd one instead is not executed or processed.

const Fastify = require('fastify')
const mercurius = require('mercurius')
const mercuriusAuth = require('mercurius-auth')

const schema = `
  directive @hasPermission (grant: String!) repeatable on OBJECT | FIELD_DEFINITION

  type Message {
    title: String!
    notes: String @hasPermission(grant: "lv1") @hasPermission(grant: "lv2")
  }

  type Query {
    publicMessages(org: String): [Message!]
  }
`

const resolvers = {
  Query: {
    publicMessages: async () => {
      return [
        { title: 'one', message: 'acme one', notes: 'acme-one' },
        { title: 'two', message: 'acme two', notes: 'acme-two' }
      ]
    }
  }
}

const app = Fastify()
app.register(mercurius, {
  graphiql: true,
  schema,
  resolvers
})

app.register(mercuriusAuth, {
  authContext: hasPermissionContext,
  applyPolicy: hasPermissionPolicy,
  namespace: 'authorization-filtering',
  authDirective: 'hasPermission'
})

app.listen(3000)

function hasPermissionContext (context) {
  return { permission: context.reply.request.headers['x-permission'].split(',') }
}
async function hasPermissionPolicy (authDirectiveAST, parent, args, context, info) {
  const needed = authDirectiveAST.arguments.find(arg => arg.name.value === 'grant').value.value
  const hasGrant = context.auth.permission.includes(needed)
  if (!hasGrant) {
    throw new Error(`Needed ${needed} grant`)
  }
  return true
}

I think the issue is on this line:

const authDirective = astNode.directives.find(directive => directive.name.value === this[kAuthDirective])

find should be filter instead.
Changing the output to an array impacts to the getPolicy() public API. I'm not sure about the impacts

@jonnydgreen
Copy link
Collaborator

Good find and nice explanation - we should definitely support this (as you mentioned, I think we may need to add this support to the external policy API - so we'll need to be careful to avoid breaking existing functionality) - would you like to work on a draft PR so we can discuss the approach there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants