Skip to content

Commit

Permalink
fix: Fixed broken test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoë Maas committed Oct 29, 2024
1 parent 19881bc commit 705edad
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 359 deletions.
6 changes: 5 additions & 1 deletion packages/oidf-client/__tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import oidfClientAgentLogic from './shared/oidfClientAgentLogic'
import { createObjects, getConfig } from '../../agent-config/dist'
import {createObjects, getConfig} from '../../agent-config/dist'
import {com} from "@sphereon/openid-federation-client";
import {defaultCryptoJSImpl} from "./shared/CryptoDefaultCallback";
import DefaultCallbacks = com.sphereon.oid.fed.client.service.DefaultCallbacks;

jest.setTimeout(60000)

let agent: any

const setup = async (): Promise<boolean> => {
DefaultCallbacks.setCryptoServiceDefault(defaultCryptoJSImpl)
const config = await getConfig('packages/oidf-client/agent.yml')
const { localAgent } = await createObjects(config, { localAgent: '/agent' })
agent = localAgent
Expand Down
4 changes: 4 additions & 0 deletions packages/oidf-client/__tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import oidfClientAgentLogic from './shared/oidfClientAgentLogic'
import {createObjects, getConfig} from '../../agent-config/dist'
import {IOIDFClient} from "../src";
import {IJwtService} from "@sphereon/ssi-sdk-ext.jwt-service";
import {com} from "@sphereon/openid-federation-client";
import {defaultCryptoJSImpl} from "./shared/CryptoDefaultCallback";
import DefaultCallbacks = com.sphereon.oid.fed.client.service.DefaultCallbacks;

jest.setTimeout(60000)

Expand All @@ -32,6 +35,7 @@ const getAgent = (options?: IAgentOptions) =>


const setup = async (): Promise<boolean> => {
DefaultCallbacks.setCryptoServiceDefault(defaultCryptoJSImpl)
const config = await getConfig('packages/oidf-client/agent.yml')
const { agent } = await createObjects(config, { agent: '/agent' })
serverAgent = agent
Expand Down
20 changes: 20 additions & 0 deletions packages/oidf-client/__tests__/shared/CryptoDefaultCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {importJWK, JWK, jwtVerify, JWTVerifyOptions} from "jose";
import {com} from "@sphereon/openid-federation-client";
import ICryptoCallbackServiceJS = com.sphereon.oid.fed.client.crypto.ICryptoCallbackServiceJS;

export const defaultCryptoJSImpl: ICryptoCallbackServiceJS = {
verify: async (jwt: string, key: any): Promise<boolean> => {
const jwk:JWK = { ...key }
const publicKey = await importJWK(jwk)

const now = new Date()
const past = now.setDate(now.getDate() - 60)

const options: JWTVerifyOptions = {
currentDate: new Date(past)
}

const result = await jwtVerify(jwt, publicKey, options)
return result !== undefined
}
}
50 changes: 4 additions & 46 deletions packages/oidf-client/__tests__/shared/oidfClientAgentLogic.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'cross-fetch/polyfill'
import {createAgent, TAgent} from '@veramo/core'
import {IOIDFClient, OIDFClient} from "../../src";
import {TAgent} from '@veramo/core'
import {IOIDFClient} from "../../src";
import {mockResponses} from "./TrustChainMockResponses";
import {IJwtService, JwtService} from "@sphereon/ssi-sdk-ext.jwt-service";
import {IJwtService} from "@sphereon/ssi-sdk-ext.jwt-service";
import nock = require('nock');
import {importJWK, JWK, jwtVerify, JWTVerifyOptions} from "jose";

type ConfiguredAgent = TAgent<IOIDFClient & IJwtService>

Expand Down Expand Up @@ -40,48 +39,7 @@ export default (testContext: { getAgent: () => ConfiguredAgent; setup: () => Pro

beforeAll(async (): Promise<void> => {
await testContext.setup()
//agent = testContext.getAgent()
agent = createAgent({
plugins: [
new JwtService(),
new OIDFClient({
// FIXME it should be fixed in the library, the function's name and the JWK object keys are mangled
cryptoServiceCallback: {
// @ts-ignore
q3t: async (jwt: string, key: any): Promise<boolean> => {
const jwk: JWK
= {
kty: key.e3s_1,
kid: key.f3s_1,
crv: key.g3s_1,
x: key.h3s_1,
y: key.i3s_1,
n: key.j3s_1,
e: key.k3s_1,
alg: key.l3s_1,
use: key.m3s_1,
x5u: key.n3s_1,
x5c: key.o3s_1,
x5t: key.p3s_1,
'x5t#S256': key.q3s_1,
}

const publicKey = await importJWK(jwk)

const now = new Date()
const past = now.setDate(now.getDate() - 60)

const options: JWTVerifyOptions = {
currentDate: new Date(past)
}

const result = await jwtVerify(jwt, publicKey, options)
return result !== undefined
}
}
}),
]
})
agent = testContext.getAgent()
})

afterAll(testContext.tearDown)
Expand Down
6 changes: 3 additions & 3 deletions packages/oidf-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"generate-plugin-schema": "ts-node ../../packages/dev/bin/sphereon.js dev generate-plugin-schema"
},
"dependencies": {
"@sphereon/openid-federation-open-api": "0.1.0-unstable.2a3ea1d",
"@sphereon/openid-federation-client": "0.1.0-unstable.2a3ea1d",
"@sphereon/openid-federation-common": "0.1.0-unstable.2a3ea1d",
"@sphereon/openid-federation-open-api": "0.1.0-unstable.151480d",
"@sphereon/openid-federation-client": "0.1.0-unstable.151480d",
"@sphereon/openid-federation-common": "0.1.0-unstable.151480d",
"@sphereon/ssi-types": "workspace:*",
"@sphereon/ssi-sdk-ext.jwt-service": "^0.24.1-unstable.130"
},
Expand Down
13 changes: 8 additions & 5 deletions packages/oidf-client/src/agent/OIDFClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
ResolveTrustChainArgs,
ResolveTrustChainCallbackResult
} from '../types/IOIDFClient';
// @ts-ignore // TODO fix import
import { com } from '@sphereon/openid-federation-client';
import {schema} from '../index';
import FederationClient = com.sphereon.oid.fed.client.FederationClient;
import DefaultFetchJSImpl = com.sphereon.oid.fed.client.fetch.DefaultFetchJSImpl
import DefaultTrustChainJSImpl = com.sphereon.oid.fed.client.trustchain.DefaultTrustChainJSImpl
import DefaultCallbacks = com.sphereon.oid.fed.client.service.DefaultCallbacks

export const oidfClientMethods: Array<string> = [
'resolveTrustChain',
Expand All @@ -29,12 +31,13 @@ export class OIDFClient implements IAgentPlugin {

constructor(args?: OIDFClientArgs) {
const { cryptoServiceCallback } = { ...args }
DefaultCallbacks.setFetchServiceDefault(new DefaultFetchJSImpl())
DefaultCallbacks.setTrustChainServiceDefault(new DefaultTrustChainJSImpl())
if (cryptoServiceCallback) {
this.oidfClient = new FederationClient(null, cryptoServiceCallback)
} else {
//FIXME Default Federation client crypto callback
this.oidfClient = new FederationClient(null, null)
DefaultCallbacks.setCryptoServiceDefault(cryptoServiceCallback)
}
//FIXME set default Federation client crypto callback
this.oidfClient = new FederationClient()
}

readonly methods: IOIDFClient = {
Expand Down
9 changes: 5 additions & 4 deletions packages/oidf-client/src/types/IOIDFClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IAgentContext, IPluginMethodMap} from '@veramo/core';
// @ts-ignore // TODO fix import
import { com, Nullable } from '@sphereon/openid-federation-client';
import ICryptoServiceCallbackJS = com.sphereon.oid.fed.client.crypto.ICryptoServiceCallback;
import { com } from '@sphereon/openid-federation-client';
import ICryptoCallbackServiceJS = com.sphereon.oid.fed.client.crypto.ICryptoCallbackServiceJS;
import {
CreateJwsCompactArgs,
IJwsValidationResult,
Expand All @@ -25,7 +24,9 @@ export type OIDFClientArgs = {
cryptoServiceCallback?: CryptoServiceCallbackArgs
}

export type CryptoServiceCallbackArgs = ICryptoServiceCallbackJS
export type CryptoServiceCallbackArgs = ICryptoCallbackServiceJS

type Nullable<T> = T | null | undefined

export type ResolveTrustChainCallbackResult = Nullable<Array<string>>

Expand Down
Loading

0 comments on commit 705edad

Please sign in to comment.