Skip to content

Commit

Permalink
fix: u8aintarrays do not work with REST
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Oct 2, 2024
1 parent 6324f97 commit 8c68022
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/jwt-service/__tests__/shared/jwtServiceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export default (testContext: {
alg: "ECDH-ES",
enc: "A256GCM",
payload: decodeJwt(jwt.jwt),
keyManagementParams: {apu: u8a.fromString('apu'), apv: u8a.fromString('apv')},
apu: u8a.toString(u8a.fromString('apu'), 'base64url'),
apv: u8a.toString(u8a.fromString('apv'), 'base64url'),
// @ts-ignore
recipientKey: await agent.identifierExternalResolveByJwk({identifier: ietfJwk})
})
Expand Down
11 changes: 9 additions & 2 deletions packages/jwt-service/src/agent/JwtService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
} from '..'
import {CompactJwtEncrypter} from "../functions/JWE";

import * as u8a from 'uint8arrays'

/**
* @public
*/
Expand Down Expand Up @@ -62,7 +64,9 @@ export class JwtService implements IAgentPlugin {
}

private async jwtEncryptJweCompactJwt(args: EncryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult> {
const {payload, protectedHeader = {alg: args.alg, enc: args.enc}, recipientKey, keyManagementParams, issuer, expirationTime, audience} = args
const {payload, protectedHeader = {alg: args.alg, enc: args.enc}, recipientKey, issuer, expirationTime, audience} = args

console.log(JSON.stringify(args, null, 2))

const alg = jweAlg(args.alg) ?? jweAlg(protectedHeader.alg) ?? 'ECDH-ES'
const enc = jweEnc(args.enc) ?? jweEnc(protectedHeader.enc) ?? 'A256GCM'
Expand All @@ -77,7 +81,10 @@ export class JwtService implements IAgentPlugin {
if (jwkInfo.jwk.kty?.startsWith('EC') !== true || !alg.startsWith('ECDH')) {
return Promise.reject(Error(`Currently only ECDH-ES is supported for encryption. JWK alg ${jwkInfo.jwk.kty}, header alg ${alg}`)) // TODO: Probably we support way more already
}
const {apu, apv} = {...keyManagementParams}
const apuVal = protectedHeader.apu ?? args.apu
const apu = apuVal ? u8a.fromString(apuVal, 'base64url') : undefined
const apvVal = protectedHeader.apv ?? args.apv
const apv = apvVal ? u8a.fromString(apvVal, 'base64url') : undefined

const pubKey = await crypto.subtle.importKey('jwk', jwkInfo.jwk, {
name: 'ECDH',
Expand Down
4 changes: 2 additions & 2 deletions packages/jwt-service/src/types/IJwtService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
JWK
} from '@sphereon/ssi-types'
import {IAgentContext, IKeyManager, IPluginMethodMap} from '@veramo/core'
import {JWEKeyManagementHeaderParameters} from "jose";

export type IRequiredContext = IAgentContext<IIdentifierResolution & IKeyManager> // could we still interop with Veramo?

Expand Down Expand Up @@ -208,7 +207,8 @@ export type EncryptJweCompactJwtArgs = {
recipientKey: ExternalIdentifierResult & { kid?: string}
alg?: JweAlg
enc?: JweEnc
keyManagementParams?: JWEKeyManagementHeaderParameters
apu?: string // base64url
apv?: string // base64url
expirationTime?: number | string | Date
issuer?: string
audience?: string | string[]
Expand Down

0 comments on commit 8c68022

Please sign in to comment.