Skip to content

Commit 9d0e53c

Browse files
committed
feat: add separate env var for webhook enabled
1 parent 53e7801 commit 9d0e53c

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

packages/auth/jest.env.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ process.env.IDENTITY_SERVER_SECRET =
44
'2pEcn2kkCclbOHQiGNEwhJ0rucATZhrA807HTm2rNXE='
55
process.env.AUTH_SERVER_URL = 'http://localhost:3006'
66
process.env.IDENTITY_SERVER_URL = 'http://localhost:3030/mock-idp/'
7+
process.env.WEBHOOK_ENABLED = 'true'
78
process.env.WEBHOOK_URL = 'http://127.0.0.1:4001/webhook'

packages/auth/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class App {
146146
}
147147

148148
// if webhookUrl is not set, webhook events are not saved or processed
149-
if (this.config.webhookUrl) {
149+
if (this.config.webhookEnabled) {
150150
for (let i = 0; i < this.config.webhookWorkers; i++) {
151151
process.nextTick(() => this.processWebhook())
152152
}

packages/auth/src/config/app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ export const Config = {
8383
signatureSecret: process.env.SIGNATURE_SECRET, // optional
8484
signatureVersion: envInt('SIGNATURE_VERSION', 1),
8585

86-
webhookUrl: process.env.WEBHOOK_URL, // optional
86+
webhookEnabled: envBool('WEBHOOK_ENABLED', false),
87+
webhookUrl:
88+
process.env.WEBHOOK_ENABLED === 'true'
89+
? envString('WEBHOOK_URL')
90+
: undefined, // required only when WEBHOOK_ENABLED is true
8791
webhookWorkers: envInt('WEBHOOK_WORKERS', 1),
8892
webhookWorkerIdle: envInt('WEBHOOK_WORKER_IDLE', 200), // milliseconds
8993
webhookTimeout: envInt('WEBHOOK_TIMEOUT', 2000), // milliseconds

packages/auth/src/grant/service.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { AccessToken } from '../accessToken/model'
2424
import { Interaction, InteractionState } from '../interaction/model'
2525
import { Pagination, SortOrder } from '../shared/baseModel'
2626
import { getPageTests } from '../shared/baseModel.test'
27+
import { WebhookEvent } from '../webhook/model'
2728

2829
describe('Grant Service', (): void => {
2930
let deps: IocContract<AppServices>
@@ -366,11 +367,51 @@ describe('Grant Service', (): void => {
366367
updatedAt: expect.any(Date)
367368
}
368369
])
370+
expect(
371+
WebhookEvent.query().where({ grantId: grant.id })
372+
).resolves.toHaveLength(1)
369373
})
370374

371375
test('Can "revoke" unknown grant', async (): Promise<void> => {
372376
await expect(grantService.revokeGrant(v4())).resolves.toEqual(false)
373377
})
378+
379+
describe('revoke with webhook disabled', (): void => {
380+
beforeAll(async (): Promise<void> => {
381+
Config.webhookEnabled = false
382+
})
383+
384+
afterAll(async (): Promise<void> => {
385+
Config.webhookEnabled = true
386+
})
387+
388+
test('Can revoke a grant and not save webhook event', async (): Promise<void> => {
389+
await expect(grantService.revokeGrant(grant.id)).resolves.toEqual(
390+
true
391+
)
392+
393+
const revokedGrant = await Grant.query(knex).findById(grant.id)
394+
expect(revokedGrant?.state).toEqual(GrantState.Finalized)
395+
expect(revokedGrant?.finalizationReason).toEqual(
396+
GrantFinalization.Revoked
397+
)
398+
expect(Access.query().where({ grantId: grant.id })).resolves.toEqual([
399+
{ ...access, limits: null }
400+
])
401+
expect(
402+
AccessToken.query().where({ grantId: grant.id })
403+
).resolves.toEqual([
404+
{
405+
...accessToken,
406+
revokedAt: expect.any(Date),
407+
updatedAt: expect.any(Date)
408+
}
409+
])
410+
expect(
411+
WebhookEvent.query().where({ grantId: grant.id })
412+
).resolves.toHaveLength(0)
413+
})
414+
})
374415
})
375416

376417
describe('lock', (): void => {

packages/auth/src/grant/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ async function revokeGrant(
201201

202202
const revokedAt = await accessTokenService.revokeByGrantId(grant.id, trx)
203203

204-
if (deps.config.webhookUrl) {
204+
if (deps.config.webhookEnabled) {
205205
await GrantEvent.query(trx).insert({
206206
type: GrantEventType.GrantRevoked,
207207
grantId: grant.id,

0 commit comments

Comments
 (0)