-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): request grant to query incoming payment receiver (#779)
* feat(backend): request grant to query incoming payment receiver Add services for managing grants. * fix(localenv): connect Docker network Update environment variables. * fix(auth): add grant access actions Remove unused locations and interval fields and account access. * fix(backend): properly export public key jwk * fix(open-payments): properly construct grant request body * chore(backend): import open-payments generateJwk * chore(backend): store and check grant expiresAt * chore(auth): add interval to OutgoingPaymentLimit * chore(localenv): re-format networks * chore(backend): don't cast GrantRequest
- Loading branch information
1 parent
18d2537
commit a0302b3
Showing
25 changed files
with
727 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/backend/migrations/20221005181411_create_auth_servers_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
exports.up = function (knex) { | ||
return knex.schema.createTable('authServers', function (table) { | ||
table.uuid('id').notNullable().primary() | ||
table.string('url').notNullable().unique() | ||
table.timestamp('createdAt').defaultTo(knex.fn.now()) | ||
table.timestamp('updatedAt').defaultTo(knex.fn.now()) | ||
}) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.dropTableIfExists('authServers') | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/backend/migrations/20221012013413_create_grants_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
exports.up = function (knex) { | ||
return knex.schema.createTable('grants', function (table) { | ||
table.uuid('id').notNullable().primary() | ||
table.uuid('authServerId').notNullable() | ||
table.foreign('authServerId').references('authServers.id') | ||
table.string('continueId').nullable() | ||
table.string('continueToken').nullable() | ||
table.string('accessToken').nullable().unique() | ||
table.string('accessType').notNullable() | ||
table.specificType('accessActions', 'text[]') | ||
|
||
table.timestamp('expiresAt').nullable() | ||
|
||
table.timestamp('createdAt').defaultTo(knex.fn.now()) | ||
table.timestamp('updatedAt').defaultTo(knex.fn.now()) | ||
|
||
table.unique(['authServerId', 'accessType', 'accessActions']) | ||
}) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.dropTableIfExists('grants') | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { BaseModel } from '../../shared/baseModel' | ||
|
||
export class AuthServer extends BaseModel { | ||
public static get tableName(): string { | ||
return 'authServers' | ||
} | ||
|
||
public url!: string | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/backend/src/open_payments/authServer/service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { IocContract } from '@adonisjs/fold' | ||
import { faker } from '@faker-js/faker' | ||
import { Knex } from 'knex' | ||
|
||
import { AuthServer } from './model' | ||
import { AuthServerService } from './service' | ||
import { initIocContainer } from '../../' | ||
import { AppServices } from '../../app' | ||
import { Config } from '../../config/app' | ||
import { createTestApp, TestContainer } from '../../tests/app' | ||
import { truncateTables } from '../../tests/tableManager' | ||
|
||
describe('Auth Server Service', (): void => { | ||
let deps: IocContract<AppServices> | ||
let appContainer: TestContainer | ||
let authServerService: AuthServerService | ||
let knex: Knex | ||
|
||
beforeAll(async (): Promise<void> => { | ||
deps = await initIocContainer(Config) | ||
appContainer = await createTestApp(deps) | ||
knex = await deps.use('knex') | ||
authServerService = await deps.use('authServerService') | ||
}) | ||
|
||
afterEach(async (): Promise<void> => { | ||
await truncateTables(knex) | ||
}) | ||
|
||
afterAll(async (): Promise<void> => { | ||
await appContainer.shutdown() | ||
}) | ||
|
||
describe('getOrCreate', (): void => { | ||
test('Auth server can be created or fetched', async (): Promise<void> => { | ||
const url = faker.internet.url() | ||
await expect( | ||
AuthServer.query(knex).findOne({ url }) | ||
).resolves.toBeUndefined() | ||
const authServer = await authServerService.getOrCreate(url) | ||
await expect(authServer).toMatchObject({ url }) | ||
await expect(AuthServer.query(knex).findOne({ url })).resolves.toEqual( | ||
authServer | ||
) | ||
await expect(authServerService.getOrCreate(url)).resolves.toEqual( | ||
authServer | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { UniqueViolationError } from 'objection' | ||
|
||
import { AuthServer } from './model' | ||
import { BaseService } from '../../shared/baseService' | ||
|
||
export interface AuthServerService { | ||
getOrCreate(url: string): Promise<AuthServer> | ||
} | ||
|
||
type ServiceDependencies = BaseService | ||
|
||
export async function createAuthServerService( | ||
deps_: ServiceDependencies | ||
): Promise<AuthServerService> { | ||
const deps: ServiceDependencies = { | ||
...deps_, | ||
logger: deps_.logger.child({ | ||
service: 'AuthServerService' | ||
}) | ||
} | ||
return { | ||
getOrCreate: (url) => getOrCreateAuthServer(deps, url) | ||
} | ||
} | ||
|
||
async function getOrCreateAuthServer( | ||
deps: ServiceDependencies, | ||
url: string | ||
): Promise<AuthServer> { | ||
try { | ||
return await AuthServer.query(deps.knex).insertAndFetch({ | ||
url | ||
}) | ||
} catch (err) { | ||
if (err instanceof UniqueViolationError) { | ||
return await AuthServer.query(deps.knex).findOne({ url }) | ||
} | ||
throw err | ||
} | ||
} |
Oops, something went wrong.