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

feat: Optionally add permissions to gql resolvers #119

Merged

Conversation

walfly
Copy link
Contributor

@walfly walfly commented Apr 20, 2020

Still need to add more tests. Allows configuring agent with authentication boolean at instantiation. An auth-identity can be set in the request context and resolvers will include that identity in their generated queries if authentication is set to true.

@walfly walfly marked this pull request as draft April 20, 2020 06:47
@walfly walfly force-pushed the walfly/permissioning branch from d4a97a5 to 8ed73c0 Compare April 21, 2020 08:16
Copy link
Contributor

@simonas-notcat simonas-notcat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Needs some changes

@walfly walfly force-pushed the walfly/permissioning branch 2 times, most recently from 20a2591 to 009c637 Compare April 21, 2020 17:19
@walfly walfly marked this pull request as ready for review April 21, 2020 17:20
@walfly
Copy link
Contributor Author

walfly commented Apr 21, 2020

@simonas-notcat updated


if (input?.order) {
for (const item of input.order) {
qb = qb.orderBy(`${escape(tablename)}.${escape(item.column)}`, item.direction)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use another package for that. You can do the same thing with:

qb = qb.orderBy(`${qb.connection.driver.escape(tablename)}.${qb.connection.driver.escape(item.column)}`, item.direction)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cool, i looked for this, guess I missed it.

@@ -21,6 +24,7 @@ import { Identity } from '../entities/identity'

export interface Context {
agent: Agent
authenticatedDid?: Promise<string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this Promise<string> and not just a simple string?

I was thinking, that it can be used like that:

const server = new ApolloServer({
  typeDefs: [Daf.Gql.baseTypeDefs, Daf.Gql.Core.typeDefs],
  resolvers: merge(Daf.Gql.Core.resolvers),
  context: async ({ req }) => {
    const authenticatedDid = await applicationSpecificLogic(req.headers.authorization)
    return { agent, authenticatedDid }
  }
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the context function can't be async. Because verifying a jwt is an async function, this has to be a promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your mentioned issue is almost 4 years old. I just tried it and it works fine:
https://github.com/uport-project/daf/blob/master/examples/id-hub/src/index.ts#L50

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was causing linting warning in my ide when I tried before, now its not, must have been something wrong with my config. My mistake, will change.

@@ -9,6 +9,7 @@ import { ActionHandler } from './action/action-handler'
import { Action } from './types'
import { Message, MetaData } from './entities/message'
import { Connection } from 'typeorm'
import { verifyJWT } from 'did-jwt'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

@@ -34,6 +35,7 @@ interface Config {

export class Agent extends EventEmitter {
readonly dbConnection: Promise<Connection>
readonly authentication?: boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

Comment on lines 153 to 159
if (ctx.authenticatedDid) {
const authIdent = await ctx.authenticatedDid
qb = qb.andWhere(
new Brackets(qb => {
qb.where('message.to = :ident', { ident: authIdent }).orWhere('message.from = :ident', {
ident: authIdent,
})
}),
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change authenticatedDid to string then this can be simplified like that:

if (ctx.authenticatedDid) {
      qb = qb.andWhere(
       new Brackets(qb => {
          qb.where('message.to = :ident', { ident: ctx.authenticatedDid })
          .orWhere('message.from = :ident', { ident: ctx.authenticatedDid})
       }),
     )
   }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonas-notcat
Copy link
Contributor

@walfly I just released 4.0. Please create a new PR to master with your changes to

packages/daf-core/src/__tests__/resolvers.test.ts
packages/daf-core/src/graphql/graphql-core.ts

Sorry for inconvenience

@walfly walfly force-pushed the walfly/permissioning branch from 6afdc81 to 0d5b212 Compare April 24, 2020 01:33
@walfly
Copy link
Contributor Author

walfly commented Apr 24, 2020

@simonas-notcat alright ready now

Copy link
Contributor

@simonas-notcat simonas-notcat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thank you

@simonas-notcat simonas-notcat merged commit 9c7f98e into decentralized-identity:master Apr 24, 2020
@walfly walfly deleted the walfly/permissioning branch April 24, 2020 15:32
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 this pull request may close these issues.

2 participants