Skip to content

Commit

Permalink
feat: Authorized agent methods
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored and mirceanis committed Sep 7, 2020
1 parent 9c9a597 commit 53f9454
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 9 deletions.
101 changes: 101 additions & 0 deletions packages/daf-core/src/__tests__/agent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Agent } from '../agent'
import { IAgentPlugin } from '../types'

describe('daf-core agent', () => {
it('should use plugin methods', async () => {
const plugin: IAgentPlugin = {
methods: {
doSomething: jest.fn(),
},
}
const agent = new Agent({
plugins: [plugin],
})

//@ts-ignore
await agent.doSomething({ foo: 'baz' })
expect(plugin.methods.doSomething).toBeCalledWith({ foo: 'baz' }, { agent })
await agent.execute('doSomething', { foo: 'bar' })
expect(plugin.methods.doSomething).toBeCalledWith({ foo: 'bar' }, { agent })
})

it('should allow method overrides', async () => {
const doSomething = jest.fn()
const plugin: IAgentPlugin = {
methods: {
doSomething: jest.fn(),
},
}
const agent = new Agent({
plugins: [plugin],
overrides: {
doSomething,
},
})

//@ts-ignore
await agent.doSomething({ foo: 'baz' })
expect(doSomething).toBeCalledWith({ foo: 'baz' }, { agent })
await agent.execute('doSomething', { foo: 'bar' })
expect(doSomething).toBeCalledWith({ foo: 'bar' }, { agent })
})

it('should expose only authorized methods', async () => {
const doSomething = jest.fn()
const baz = jest.fn()
const plugin: IAgentPlugin = {
methods: {
foo: jest.fn(),
bar: jest.fn(),
},
}
const agent = new Agent({
authorizedMethods: ['bar', 'baz'],
plugins: [plugin],
overrides: {
doSomething,
baz,
},
})

//@ts-ignore
expect(agent.doSomething).toEqual(undefined)
expect(agent.execute('doSomething', { foo: 'bar' })).rejects.toEqual(
Error('Method not available: doSomething'),
)

//@ts-ignore
await agent.bar({ foo: 'baz' })
expect(plugin.methods.bar).toBeCalledWith({ foo: 'baz' }, { agent })
await agent.execute('baz', { foo: 'bar' })
expect(baz).toBeCalledWith({ foo: 'bar' }, { agent })

expect(agent.availableMethods()).toEqual(['bar', 'baz'])
})

it('should pass through context', async () => {
const plugin: IAgentPlugin = {
methods: {
doSomething: jest.fn(),
},
}
const agent = new Agent({
context: {
authorizedDid: 'did:example:123',
},
plugins: [plugin],
})

//@ts-ignore
await agent.doSomething({ foo: 'baz' })
expect(plugin.methods.doSomething).toBeCalledWith(
{ foo: 'baz' },
{ agent, authorizedDid: 'did:example:123' },
)
await agent.execute('doSomething', { foo: 'bar' })
expect(plugin.methods.doSomething).toBeCalledWith(
{ foo: 'bar' },
{ agent, authorizedDid: 'did:example:123' },
)
})
})
6 changes: 0 additions & 6 deletions packages/daf-core/src/__tests__/default.test.ts

This file was deleted.

33 changes: 30 additions & 3 deletions packages/daf-core/src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
import { IAgentBase, TMethodMap, IAgentPlugin } from './types'
import Debug from 'debug'

const filterUnauthorizedMethods = (methods: TMethodMap, authorizedMethods?: string[]): TMethodMap => {
// All methods are authorized by default
if (!authorizedMethods) {
return methods
}

const result: TMethodMap = {}
for (const methodName of Object.keys(methods)) {
if (authorizedMethods.includes(methodName)) {
result[methodName] = methods[methodName]
}
}

return result
}

export class Agent implements IAgentBase {
readonly methods: TMethodMap = {}
private context: Record<string, any>
private protectedMethods = ['execute', 'availableMethods']

constructor(options?: { plugins?: IAgentPlugin[]; overrides?: TMethodMap; context?: Record<string, any> }) {
constructor(options?: {
plugins?: IAgentPlugin[]
overrides?: TMethodMap
authorizedMethods?: string[]
context?: Record<string, any>
}) {
this.context = options?.context

if (options?.plugins) {
for (const plugin of options.plugins) {
this.methods = { ...this.methods, ...plugin.methods }
this.methods = {
...this.methods,
...filterUnauthorizedMethods(plugin.methods, options.authorizedMethods),
}
}
}

if (options?.overrides) {
this.methods = { ...this.methods, ...options.overrides }
this.methods = {
...this.methods,
...filterUnauthorizedMethods(options.overrides, options.authorizedMethods),
}
}

for (const method of Object.keys(this.methods)) {
Expand Down

0 comments on commit 53f9454

Please sign in to comment.