Skip to content

Commit

Permalink
feat(rest): auth improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Mar 31, 2024
1 parent d09b153 commit 3f13d05
Show file tree
Hide file tree
Showing 22 changed files with 12,040 additions and 12,167 deletions.
2 changes: 1 addition & 1 deletion packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"credo-rest": "bin/credo-rest.js"
},
"scripts": {
"tsoa": "tsoa spec-and-routes",
"tsoa": "ts-node ./scripts/generate-spec-and-routes.ts",
"dev": "yarn tsoa && tsnd --respawn samples/sampleWithApp.ts",
"sample": "yarn tsoa && tsnd --respawn samples/sample.ts",
"sample:with-app": "yarn tsoa && tsnd --respawn samples/sampleWithApp.ts",
Expand Down
34 changes: 34 additions & 0 deletions packages/rest/scripts/generate-spec-and-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { readFile, writeFile } from 'fs/promises'
import { generateSpecAndRoutes } from 'tsoa'

interface SwaggerJson {
paths: {
[path: string]: {
[method: string]: { parameters?: unknown[]; security?: unknown }
}
}
}

async function run() {
await generateSpecAndRoutes({})

// Modify swagger
const swaggerJson: SwaggerJson = JSON.parse(await readFile('./src/generated/swagger.json', 'utf-8'))

for (const [path, pathValue] of Object.entries(swaggerJson.paths)) {
for (const [method, methodValue] of Object.entries(pathValue)) {
swaggerJson.paths[path][method] = {
...methodValue,
parameters: [...(methodValue.parameters ?? []), { $ref: '#/components/parameters/tenant' }],
// Removes the security
security: undefined,
}
}
}

await writeFile('./src/generated/swagger.json', JSON.stringify(swaggerJson, null, 2))
// eslint-disable-next-line no-console
console.log('Successfully generated spec and routes')
}

run()
2 changes: 1 addition & 1 deletion packages/rest/src/controllers/agent/AgentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AgentInfo } from './AgentControllerTypes'
import { Controller, Example, Get, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithRootAgent } from '../../authentication'
import { RequestWithRootAgent } from '../../tenantMiddleware'

import { agentInfoExample } from './AgentControllerExamples'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
import { Body, Controller, Example, Response, Get, Path, Post, Route, Tags, Security, Request } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../authentication'
import { RequestWithAgent } from '../../tenantMiddleware'
import { alternativeResponse } from '../../utils/response'

import {
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/controllers/did/DidsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../authentication'
import { RequestWithAgent } from '../../tenantMiddleware2'

Check failure on line 29 in packages/rest/src/controllers/did/DidsController.ts

View workflow job for this annotation

GitHub Actions / Validate

Unable to resolve path to module '../../tenantMiddleware2'
import { alternativeResponse } from '../../utils/response'

import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RecordNotFoundError, BasicMessageRole } from '@credo-ts/core'
import { Body, Controller, Example, Get, Post, Query, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'
import { RecordId, ThreadId } from '../../types'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DidExchangeState, RecordNotFoundError } from '@credo-ts/core'
import { Controller, Delete, Example, Get, Path, Post, Query, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'
import { Did } from '../../did/DidsControllerTypes'
import { RecordId } from '../../types'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CredentialState, RecordNotFoundError, CredentialRole } from '@credo-ts/
import { Body, Controller, Delete, Get, Path, Post, Route, Tags, Example, Query, Security, Request } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'
import { RecordId, ThreadId } from '../../types'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { parseMessageType, supportsIncomingMessageType } from '@credo-ts/core/bu
import { Body, Controller, Delete, Example, Get, Path, Post, Query, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'
import { RecordId } from '../../types'
import { connectionRecordExample } from '../connections/ConnectionsControllerExamples'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ProofRole, ProofState, RecordNotFoundError } from '@credo-ts/core'
import { Body, Controller, Delete, Example, Get, Path, Post, Query, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'
import { RecordId, ThreadId } from '../../types'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { OpenId4VcIssuanceSessionRepository } from '@credo-ts/openid4vc/build/op
import { Body, Controller, Delete, Example, Get, Path, Post, Query, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'
import { PublicIssuerId } from '../issuers/OpenId4VcIssuersControllerTypes'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OpenId4VcIssuerRepository } from '@credo-ts/openid4vc/build/openid4vc-i
import { Controller, Route, Tags, Security, Post, Request, Body, Delete, Path, Put, Example, Query, Get } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'

import { openId4VcIssuerRecordExample } from './OpenId4VcIssuersControllerExamples'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { OpenId4VcVerificationSessionRepository } from '@credo-ts/openid4vc/buil
import { Body, Controller, Delete, Example, Get, Path, Post, Query, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'
import { PublicIssuerId } from '../issuers/OpenId4VcIssuersControllerTypes'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OpenId4VcVerifierRepository } from '@credo-ts/openid4vc/build/openid4vc
import { Controller, Route, Tags, Security, Post, Request, Body, Delete, Path, Example, Get, Query } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithAgent } from '../../../authentication'
import { RequestWithAgent } from '../../../tenantMiddleware'
import { apiErrorResponse } from '../../../utils/response'

import { openId4VcIssuerRecordExample } from './OpenId4VcVerifiersControllerExamples'
Expand Down
12 changes: 12 additions & 0 deletions packages/rest/src/controllers/tenants/TenantControllerExamples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { TenantRecord } from './TenantsControllerTypes'

export const tenantRecordExample: TenantRecord = {
config: {
label: 'Example',
},
createdAt: new Date('2022-08-18T08:38:40.216Z'),
updatedAt: new Date('2022-08-18T08:38:40.216Z'),
id: '27137142-2e5b-471b-a427-7d5d3fd6d39b',
storageVersion: '0.5',
type: 'TenantRecord',
}
19 changes: 12 additions & 7 deletions packages/rest/src/controllers/tenants/TenantsController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { TenantsRecord } from './TenantsControllerTypes'
import type { TenantRecord } from './TenantsControllerTypes'
import type { VersionString } from '@credo-ts/core'

import { Body, Controller, Delete, Get, Path, Post, Put, Query, Request, Route, Security, Tags } from 'tsoa'
import { Body, Controller, Delete, Example, Get, Path, Post, Put, Query, Request, Route, Security, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

import { RequestWithRootTenantAgent } from '../../authentication'
import { RequestWithRootTenantAgent } from '../../tenantMiddleware'

import { tenantRecordExample } from './TenantControllerExamples'
import { tenantRecordToApiModel, TenantsCreateOptions, TenantsUpdateOptions } from './TenantsControllerTypes'

@Tags('Tenants')
Expand All @@ -17,10 +18,11 @@ export class TenantsController extends Controller {
* create new tenant
*/
@Post('/')
@Example<TenantRecord>(tenantRecordExample)
public async createTenant(
@Request() request: RequestWithRootTenantAgent,
@Body() body: TenantsCreateOptions,
): Promise<TenantsRecord> {
): Promise<TenantRecord> {
const tenant = await request.user.agent.modules.tenants.createTenant({
config: body.config,
})
Expand All @@ -32,10 +34,11 @@ export class TenantsController extends Controller {
* get tenant by id
*/
@Get('/{tenantId}')
@Example<TenantRecord>(tenantRecordExample)
public async getTenant(
@Request() request: RequestWithRootTenantAgent,
@Path('tenantId') tenantId: string,
): Promise<TenantsRecord> {
): Promise<TenantRecord> {
const tenant = await request.user.agent.modules.tenants.getTenantById(tenantId)

return tenantRecordToApiModel(tenant)
Expand All @@ -48,11 +51,12 @@ export class TenantsController extends Controller {
* If you want to unset an non-required value, you can pass `null`.
*/
@Put('/{tenantId}')
@Example<TenantRecord>(tenantRecordExample)
public async updateTenant(
@Request() request: RequestWithRootTenantAgent,
@Path('tenantId') tenantId: string,
@Body() body: TenantsUpdateOptions,
): Promise<TenantsRecord> {
): Promise<TenantRecord> {
const tenantRecord = await request.user.agent.modules.tenants.getTenantById(tenantId)

tenantRecord.config = {
Expand Down Expand Up @@ -88,11 +92,12 @@ export class TenantsController extends Controller {
* get tenants by query
*/
@Get('/')
@Example<TenantRecord[]>([tenantRecordExample])
public async getTenantsByQuery(
@Request() request: RequestWithRootTenantAgent,
@Query('label') label?: string,
@Query('storageVersion') storageVersion?: string,
): Promise<TenantsRecord[]> {
): Promise<TenantRecord[]> {
const tenants = await request.user.agent.modules.tenants.findTenantsByQuery({
label,
storageVersion: storageVersion as VersionString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import type { TenantConfig } from '@credo-ts/tenants/build/models/TenantConfig'

type TenantApiConfig = Omit<TenantConfig, 'walletConfig'>

export interface TenantsRecord extends CredoBaseRecord {
export interface TenantRecord extends CredoBaseRecord {
storageVersion: string
config: TenantApiConfig
}

export function tenantRecordToApiModel(record: CredoTenantRecord): TenantsRecord {
export function tenantRecordToApiModel(record: CredoTenantRecord): TenantRecord {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { walletConfig: _, ...config } = record.config

Expand Down
4 changes: 2 additions & 2 deletions packages/rest/src/generated/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { DidController } from './../controllers/did/DidsController';
import { AnonCredsController } from './../controllers/anoncreds/AnonCredsController';
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
import { AgentController } from './../controllers/agent/AgentController';
import { expressAuthentication } from './../authentication';
import { expressAuthentication } from './../tenantMiddleware';
// @ts-ignore - no great way to install types from subpackage
import { iocContainer } from './../utils/tsyringeTsoaIocContainer';
import type { IocContainer, IocContainerFactory } from '@tsoa/runtime';
Expand Down Expand Up @@ -60,7 +60,7 @@ const models: TsoaRoute.Models = {
"type": {"dataType":"string","validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"TenantsRecord": {
"TenantRecord": {
"dataType": "refObject",
"properties": {
"id": {"ref":"RecordId","required":true},
Expand Down
Loading

0 comments on commit 3f13d05

Please sign in to comment.