mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
use own versioned classes to request versioned publicKey
This commit is contained in:
parent
9d7224f128
commit
49c131a684
@ -1,8 +1,10 @@
|
||||
import { GraphQLClient, gql } from 'graphql-request'
|
||||
import { backendLogger as logger } from '@/server/logger'
|
||||
import { FdCommunity } from '@/federation/graphql/1_0/model/FdCommunity'
|
||||
// eslint-disable-next-line camelcase
|
||||
import { V1_0_FdCommunity } from '@/federation/graphql/1_0/model/V1_0_FdCommunity'
|
||||
|
||||
export async function requestGetPublicKey(fdCom: FdCommunity): Promise<string | undefined> {
|
||||
// eslint-disable-next-line camelcase
|
||||
export async function requestGetPublicKey(fdCom: V1_0_FdCommunity): Promise<string | undefined> {
|
||||
let endpoint = fdCom.url.endsWith('/') ? fdCom.url : fdCom.url + '/'
|
||||
endpoint = `${endpoint}${fdCom.apiVersion}/getPublicKey`
|
||||
logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`)
|
||||
|
||||
52
backend/src/federation/client/1_1/FederationClient.ts
Normal file
52
backend/src/federation/client/1_1/FederationClient.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import { GraphQLClient, gql } from 'graphql-request'
|
||||
import { backendLogger as logger } from '@/server/logger'
|
||||
// eslint-disable-next-line camelcase
|
||||
import { V1_1_FdCommunity } from '@/federation/graphql/1_1/model/V1_1_FdCommunity'
|
||||
|
||||
// eslint-disable-next-line camelcase
|
||||
export async function requestGetPublicKey(fdCom: V1_1_FdCommunity): Promise<string | undefined> {
|
||||
let endpoint = fdCom.url.endsWith('/') ? fdCom.url : fdCom.url + '/'
|
||||
endpoint = `${endpoint}${fdCom.apiVersion}/getPublicKey`
|
||||
logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`)
|
||||
|
||||
const graphQLClient = new GraphQLClient(endpoint, {
|
||||
method: 'GET',
|
||||
jsonSerializer: {
|
||||
parse: JSON.parse,
|
||||
stringify: JSON.stringify,
|
||||
},
|
||||
})
|
||||
logger.info(`graphQLClient=${JSON.stringify(graphQLClient)}`)
|
||||
const query = gql`
|
||||
query {
|
||||
getPublicKey {
|
||||
publicKey
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
const variables = {}
|
||||
|
||||
try {
|
||||
const { data, errors, extensions, headers, status } = await graphQLClient.rawRequest(
|
||||
query,
|
||||
variables,
|
||||
)
|
||||
logger.debug(
|
||||
`Response-Data: ${JSON.stringify(
|
||||
{ data, errors, extensions, headers, status },
|
||||
undefined,
|
||||
2,
|
||||
)}`,
|
||||
)
|
||||
if (data) {
|
||||
logger.debug(`Response-PublicKey: ${data.getPublicKey.publicKey}`)
|
||||
logger.info(`requestGetPublicKey processed successfully`)
|
||||
return data.getPublicKey.publicKey
|
||||
}
|
||||
logger.warn(`requestGetPublicKey processed without response data`)
|
||||
} catch (err) {
|
||||
logger.error(`Request-Error:`, err) // ${JSON.stringify(err)}`)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
export enum ApiVersionType {
|
||||
V1_0 = '1_0',
|
||||
V1_1 = '1_1',
|
||||
}
|
||||
|
||||
@ -4,7 +4,8 @@ import { ObjectType, Field } from 'type-graphql'
|
||||
import { Community as DbCommunity } from '@entity/Community'
|
||||
|
||||
@ObjectType()
|
||||
export class FdCommunity {
|
||||
// eslint-disable-next-line camelcase
|
||||
export class V1_0_FdCommunity {
|
||||
constructor(dbCommunity: DbCommunity) {
|
||||
this.apiVersion = dbCommunity.apiVersion
|
||||
this.createdAt = dbCommunity.createdAt
|
||||
42
backend/src/federation/graphql/1_1/model/V1_1_FdCommunity.ts
Normal file
42
backend/src/federation/graphql/1_1/model/V1_1_FdCommunity.ts
Normal file
@ -0,0 +1,42 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
import { ObjectType, Field } from 'type-graphql'
|
||||
import { Community as DbCommunity } from '@entity/Community'
|
||||
|
||||
@ObjectType()
|
||||
// eslint-disable-next-line camelcase
|
||||
export class V1_1_FdCommunity {
|
||||
constructor(dbCommunity: DbCommunity) {
|
||||
this.apiVersion = dbCommunity.apiVersion
|
||||
this.createdAt = dbCommunity.createdAt
|
||||
this.id = dbCommunity.id
|
||||
this.lastAnnouncedAt = dbCommunity.lastAnnouncedAt
|
||||
this.publicKey = dbCommunity.publicKey.toString('hex')
|
||||
this.updatedAt = dbCommunity.updatedAt
|
||||
this.url = dbCommunity.endPoint
|
||||
}
|
||||
|
||||
@Field(() => Number, { nullable: true })
|
||||
id: number
|
||||
|
||||
@Field(() => String)
|
||||
publicKey: string
|
||||
|
||||
@Field(() => String)
|
||||
apiVersion: string
|
||||
|
||||
@Field(() => String)
|
||||
url: string
|
||||
|
||||
@Field(() => Date)
|
||||
createdAt: Date
|
||||
|
||||
@Field(() => Date, { nullable: true })
|
||||
lastAnnouncedAt: Date | null
|
||||
|
||||
@Field(() => Date, { nullable: true })
|
||||
verifiedAt: Date | null
|
||||
|
||||
@Field(() => Date, { nullable: true })
|
||||
updatedAt: Date | null
|
||||
}
|
||||
@ -1,7 +1,13 @@
|
||||
import { Community as DbCommunity } from '@entity/Community'
|
||||
import { IsNull, LessThan, Raw } from '@dbTools/typeorm'
|
||||
import { requestGetPublicKey } from './client/1_0/FederationClient'
|
||||
import { FdCommunity } from './graphql/1_0/model/FdCommunity'
|
||||
import { IsNull } from '@dbTools/typeorm'
|
||||
// eslint-disable-next-line camelcase
|
||||
import { requestGetPublicKey as v1_0_requestGetPublicKey } from './client/1_0/FederationClient'
|
||||
// eslint-disable-next-line camelcase
|
||||
import { requestGetPublicKey as v1_1_requestGetPublicKey } from './client/1_1/FederationClient'
|
||||
// eslint-disable-next-line camelcase
|
||||
import { V1_0_FdCommunity } from './graphql/1_0/model/V1_0_FdCommunity'
|
||||
// eslint-disable-next-line camelcase
|
||||
import { V1_1_FdCommunity } from './graphql/1_1/model/V1_1_FdCommunity'
|
||||
import { backendLogger as logger } from '@/server/logger'
|
||||
import { ApiVersionType } from './enum/apiVersionType'
|
||||
|
||||
@ -23,14 +29,20 @@ export async function startValidateCommunities(timerInterval: number): Promise<v
|
||||
if (dbCommunities) {
|
||||
dbCommunities.forEach(async function (dbCom) {
|
||||
logger.debug(`Federation: dbCom: ${JSON.stringify(dbCom)}`)
|
||||
const fdCom = new FdCommunity(dbCom)
|
||||
console.log(`ApiVersionType=`, ApiVersionType)
|
||||
const fdCom = getVersionedFdCommunity(dbCom)
|
||||
if (!fdCom) {
|
||||
logger.warn(
|
||||
`Federation: unsupported ApiVersion ${dbCom.apiVersion} of Community with id= ${dbCom.id}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const apiValueStrings: string[] = Object.values(ApiVersionType)
|
||||
logger.debug(`suppported ApiVersions=`, apiValueStrings)
|
||||
if (apiValueStrings.includes(fdCom.apiVersion)) {
|
||||
logger.debug(
|
||||
`Federation: validate publicKey for dbCom: ${dbCom.id} with apiVersion=${dbCom.apiVersion}`,
|
||||
)
|
||||
const pubKey = await requestGetPublicKey(fdCom)
|
||||
const pubKey = await invokeVersionedRequestGetPublicKey(fdCom)
|
||||
logger.debug(`Federation: received publicKey: ${pubKey}`)
|
||||
if (pubKey && pubKey === fdCom.publicKey) {
|
||||
logger.debug(`Federation: matching publicKey: ${pubKey}`)
|
||||
@ -53,3 +65,28 @@ export async function startValidateCommunities(timerInterval: number): Promise<v
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
function getVersionedFdCommunity(dbCom: DbCommunity) {
|
||||
switch (dbCom.apiVersion) {
|
||||
case ApiVersionType.V1_0:
|
||||
// eslint-disable-next-line new-cap
|
||||
return new V1_0_FdCommunity(dbCom)
|
||||
case ApiVersionType.V1_1:
|
||||
// eslint-disable-next-line new-cap
|
||||
return new V1_1_FdCommunity(dbCom)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
async function invokeVersionedRequestGetPublicKey(
|
||||
// eslint-disable-next-line camelcase
|
||||
fdCom: V1_0_FdCommunity | V1_1_FdCommunity,
|
||||
): Promise<string | undefined> {
|
||||
switch (fdCom.apiVersion) {
|
||||
case ApiVersionType.V1_0:
|
||||
return v1_0_requestGetPublicKey(fdCom)
|
||||
case ApiVersionType.V1_1:
|
||||
return v1_1_requestGetPublicKey(fdCom)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user