-
Notifications
You must be signed in to change notification settings - Fork 90
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
refactor(backend): add getOrCreate
method for grantReferenceService
#787
Changes from 8 commits
e6bfa1a
ba6afb5
577595d
39054c9
6129c0d
04bd508
a5a56c6
28860c8
9f63dd9
84a2e18
6378e3f
c6eaaa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,6 +263,42 @@ describe('Auth Middleware', (): void => { | |
scope.done() | ||
}) | ||
|
||
test('if getOrCreateGrantReference throws, return 500', async (): Promise<void> => { | ||
const getOrCreateGrantReferenceSpy = jest | ||
.spyOn(grantReferenceService, 'getOrCreate') | ||
.mockImplementationOnce(async () => { | ||
throw new Error('unexpected') | ||
}) | ||
|
||
const grant = new TokenInfo( | ||
{ | ||
active: true, | ||
clientId: uuid(), | ||
grant: uuid(), | ||
access: [ | ||
{ | ||
type: AccessType.IncomingPayment, | ||
actions: [AccessAction.Read], | ||
identifier: ctx.paymentPointer.url | ||
} | ||
] | ||
}, | ||
mockKeyInfo | ||
) | ||
|
||
await grantReferenceService.create({ | ||
id: grant.grant, | ||
clientId: uuid() | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need this create |
||
|
||
const scope = mockAuthServer(grant.toJSON()) | ||
await expect(middleware(ctx, next)).rejects.toMatchObject({ | ||
status: 500 | ||
}) | ||
expect(getOrCreateGrantReferenceSpy).toHaveBeenCalled() | ||
scope.done() | ||
}) | ||
|
||
test.each` | ||
limitAccount | ||
${false} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,4 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { AccessType, AccessAction } from './grant' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Transaction } from 'objection' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { GrantReference } from '../grantReference/model' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { HttpSigContext, verifySigAndChallenge } from 'auth' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export function createAuthMiddleware({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -55,26 +53,18 @@ export function createAuthMiddleware({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ctx.throw(401, `Invalid signature`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await GrantReference.transaction(async (trx: Transaction) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const grantRef = await grantReferenceService.get(grant.grant, trx) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (grantRef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (grantRef.clientId !== grant.clientId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.debug( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
`clientID ${grant.clientId} for grant ${grant.grant} does not match internal reference clientId ${grantRef.clientId}.` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ctx.throw(500) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (action === AccessAction.Create) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Grant and client ID's are only stored for create routes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await grantReferenceService.create( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id: grant.grant, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
clientId: grant.clientId | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
trx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await grantReferenceService.getOrCreate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ id: grant.grant, clientId: grant.clientId }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
action | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By moving the action check to the caller, the rafiki/packages/backend/src/open_payments/grantReference/service.ts Lines 58 to 63 in c6eaaa9
will probably need to be moved to the getGrantRerefence methodrafiki/packages/backend/src/open_payments/grantReference/service.ts Lines 27 to 29 in c6eaaa9
and will have to take both arguments ( id and clientId )
async function getGrantReference(
options: CreateGrantReferenceOptions,
trx: Transaction
) {
// ...
} otherwise there will be no validation for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm leaning towards not passing
Suggested change
🤔 that looks a lot like how it was before... Is there an argument for not actually using |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const errInfo = e && typeof e === 'object' && e.stack ? e.stack : e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.debug(errInfo) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
would prefer something like this, without needing to check the type etc |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ctx.throw(500) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ctx.grant = grant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Unless the relevant grant action is ReadAll/ListAll add the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,6 +9,7 @@ import { Config } from '../../config/app' | |||||
import { GrantReferenceService } from './service' | ||||||
import { truncateTables } from '../../tests/tableManager' | ||||||
import { GrantReference } from './model' | ||||||
import { AccessAction } from '../auth/grant' | ||||||
|
||||||
describe('Grant Reference Service', (): void => { | ||||||
let deps: IocContract<AppServices> | ||||||
|
@@ -79,4 +80,60 @@ describe('Grant Reference Service', (): void => { | |||||
) | ||||||
}) | ||||||
}) | ||||||
|
||||||
describe('Get or Create Grant Reference', (): void => { | ||||||
test('cannot fetch a non-existing grant reference', async (): Promise<void> => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
expect( | ||||||
await grantReferenceService.getOrCreate( | ||||||
{ id: uuid(), clientId: uuid() }, | ||||||
AccessAction.List | ||||||
) | ||||||
).toBeUndefined() | ||||||
}) | ||||||
|
||||||
test('throws an error when clientId does not match', async (): Promise<void> => { | ||||||
const id = uuid() | ||||||
|
||||||
await grantReferenceService.create({ | ||||||
id, | ||||||
clientId: uuid() | ||||||
}) | ||||||
|
||||||
await expect( | ||||||
async () => | ||||||
await grantReferenceService.getOrCreate( | ||||||
{ id, clientId: uuid() }, | ||||||
AccessAction.List | ||||||
) | ||||||
).rejects.toThrowError('does not match internal reference clientId') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we would have needed a RegEx here, but it seems to work?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking the same, but based on Jest docs, RegEx and strings are equivalent in this case. |
||||||
}) | ||||||
|
||||||
test('fetch an existing grant reference', async (): Promise<void> => { | ||||||
const existingRef = await grantReferenceService.create({ | ||||||
id: uuid(), | ||||||
clientId: uuid() | ||||||
}) | ||||||
|
||||||
const retrievedRef = await grantReferenceService.getOrCreate( | ||||||
existingRef, | ||||||
AccessAction.List | ||||||
) | ||||||
|
||||||
expect(retrievedRef).toEqual(existingRef) | ||||||
}) | ||||||
|
||||||
test('create a grant reference', async (): Promise<void> => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nitpick, generally naming the test as though it's doing the action: the test creates, returns, throws etc |
||||||
const receivedRef = { | ||||||
id: uuid(), | ||||||
clientId: uuid() | ||||||
} | ||||||
|
||||||
const grantRef = await grantReferenceService.getOrCreate( | ||||||
receivedRef, | ||||||
AccessAction.Create | ||||||
) | ||||||
|
||||||
expect(grantRef).toEqual(receivedRef) | ||||||
}) | ||||||
}) | ||||||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { Transaction, TransactionOrKnex } from 'objection' | ||
import { AccessAction } from '../auth/grant' | ||
import { GrantReference } from './model' | ||
|
||
export interface GrantReferenceService { | ||
|
@@ -8,13 +9,18 @@ export interface GrantReferenceService { | |
trx?: Transaction | ||
): Promise<GrantReference> | ||
lock(grantId: string, trx: TransactionOrKnex): Promise<void> | ||
getOrCreate( | ||
options: CreateGrantReferenceOptions, | ||
action: AccessAction | ||
): Promise<GrantReference> | ||
} | ||
|
||
export async function createGrantReferenceService(): Promise<GrantReferenceService> { | ||
return { | ||
get: (grantId, trx) => getGrantReference(grantId, trx), | ||
create: (options, trx) => createGrantReference(options, trx), | ||
lock: (grantId, trx) => lockGrantReference(grantId, trx) | ||
lock: (grantId, trx) => lockGrantReference(grantId, trx), | ||
getOrCreate: (options, action) => getOrCreateGrantReference(options, action) | ||
} | ||
} | ||
|
||
|
@@ -42,3 +48,26 @@ async function lockGrantReference(grantId: string, trx: TransactionOrKnex) { | |
.forNoKeyUpdate() | ||
.timeout(5000) | ||
} | ||
|
||
async function getOrCreateGrantReference( | ||
options: CreateGrantReferenceOptions, | ||
action: AccessAction | ||
) { | ||
const grant = await GrantReference.transaction(async (trx: Transaction) => { | ||
const grantRef = await getGrantReference(options.id, trx) | ||
if (grantRef) { | ||
if (grantRef.clientId !== options.clientId) { | ||
throw new Error( | ||
`clientID ${options.clientId} for grant ${options.id} does not match internal reference clientId ${grantRef.clientId}.` | ||
) | ||
} | ||
|
||
return grantRef | ||
} else if (action === AccessAction.Create) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check should be moved out to the caller. |
||
// Grant and client ID's are only stored for create routes | ||
return await createGrantReference(options, trx) | ||
} | ||
}) | ||
|
||
return grant | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the same as comments below