Skip to content

Commit

Permalink
feat(backend): start of handling grant token rotation more gracefully (
Browse files Browse the repository at this point in the history
…#2887)

* feat(backend): add grantService.getOrCreate method

* feat(backend): update grant lookup call

* feat(backend): add deletedAt to grants table

* feat(backend): add migrations for grants table

* chore(backend): separating into functions

* chore(backend): adding authServerService to list of services

* test(backend): adding tests for getOrCreate and getExistingGrant functions

* chore(backend): merge migrations

* feat(backend): add subset actions if present
  • Loading branch information
mkurapov authored Aug 23, 2024
1 parent b08a157 commit 175a7cf
Show file tree
Hide file tree
Showing 7 changed files with 680 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('grants', (table) => {
table.dropUnique(['authServerId', 'accessType', 'accessActions'])
table.timestamp('deletedAt').nullable()
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('grants', (table) => {
table.unique(['authServerId', 'accessType', 'accessActions'])
table.dropColumn('deletedAt')
})
}
4 changes: 4 additions & 0 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ import {
} from './open_payments/wallet_address/middleware'

import { LoggingPlugin } from './graphql/plugin'
import { GrantService } from './open_payments/grant/service'
import { AuthServerService } from './open_payments/authServer/service'
export interface AppContextData {
logger: Logger
container: AppContainer
Expand Down Expand Up @@ -232,6 +234,8 @@ export interface AppServices {
incomingPaymentService: Promise<IncomingPaymentService>
remoteIncomingPaymentService: Promise<RemoteIncomingPaymentService>
receiverService: Promise<ReceiverService>
grantService: Promise<GrantService>
authServerService: Promise<AuthServerService>
streamServer: Promise<StreamServer>
webhookService: Promise<WebhookService>
quoteService: Promise<QuoteService>
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export function initIocContainer(
container.singleton('grantService', async (deps) => {
return await createGrantService({
authServerService: await deps.use('authServerService'),
openPaymentsClient: await deps.use('openPaymentsClient'),
logger: await deps.use('logger'),
knex: await deps.use('knex')
})
Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/open_payments/grant/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum GrantError {
GrantRequiresInteraction = 'GrantRequiresInteraction',
InvalidGrantRequest = 'InvalidGrantRequest'
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export const isGrantError = (o: any): o is GrantError =>
Object.values(GrantError).includes(o)
1 change: 1 addition & 0 deletions packages/backend/src/open_payments/grant/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class Grant extends BaseModel {
public accessType!: AccessType
public accessActions!: AccessAction[]
public expiresAt?: Date | null
public deletedAt?: Date

public get expired(): boolean {
return !!this.expiresAt && this.expiresAt <= new Date()
Expand Down
Loading

0 comments on commit 175a7cf

Please sign in to comment.