/
functions.ts
214 lines (201 loc) · 7.95 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { CredentialIssuerMetadata, Jwt, JwtVerifyResult, OID4VCICredentialFormat, UniformCredentialRequest } from '@sphereon/oid4vci-common'
import { CredentialDataSupplier, CredentialIssuanceInput, CredentialSignerCallback, VcIssuer, VcIssuerBuilder } from '@sphereon/oid4vci-issuer'
import { getAgentResolver, getDID, getFirstKeyWithRelation, getIdentifier, getKey, IDIDOptions, toDID } from '@sphereon/ssi-sdk-ext.did-utils'
import { ICredential, W3CVerifiableCredential } from '@sphereon/ssi-types'
import { DIDDocument, IIdentifier, IKey, ProofFormat } from '@veramo/core'
import { CredentialPayload } from '@veramo/core/src/types/vc-data-model'
import { bytesToBase64 } from '@veramo/utils'
import { createJWT, decodeJWT, JWTVerifyOptions, verifyJWT } from 'did-jwt'
import { Resolvable } from 'did-resolver'
import { IIssuerOptions, IRequiredContext } from './types/IOID4VCIIssuer'
export function getJwtVerifyCallback({ verifyOpts }: { verifyOpts?: JWTVerifyOptions }, _context: IRequiredContext) {
return async (args: { jwt: string; kid?: string }): Promise<JwtVerifyResult<DIDDocument>> => {
const resolver = getAgentResolver(_context, { resolverResolution: true, uniresolverResolution: true, localResolution: true })
verifyOpts = { ...verifyOpts, resolver: verifyOpts?.resolver } // Resolver seperately as that is a function
if (!verifyOpts?.resolver || typeof verifyOpts?.resolver?.resolve !== 'function') {
verifyOpts.resolver = resolver
}
const result = await verifyJWT(args.jwt, verifyOpts)
if (!result.verified) {
console.log(`JWT invalid: ${args.jwt}`)
throw Error('JWT did not verify successfully')
}
const jwt = (await decodeJWT(args.jwt)) as Jwt
const kid = args.kid ?? jwt.header.kid
if (!kid) {
throw Error('No kid value found')
}
const did = kid.split('#')[0]
const didResolution = await resolver.resolve(did)
if (!didResolution || !didResolution.didDocument) {
throw Error(`Could not resolve did: ${did}, metadata: ${didResolution?.didResolutionMetadata}`)
}
const didDocument = didResolution.didDocument
const alg = jwt.header.alg
return {
alg,
kid,
did,
didDocument,
jwt,
}
}
}
export async function getAccessTokenKeyRef(
opts: {
iss?: string
keyRef?: string
didOpts?: IDIDOptions
},
context: IRequiredContext,
) {
let keyRef =
opts.keyRef ??
opts.didOpts?.identifierOpts?.kid ??
(typeof opts.didOpts?.identifierOpts.identifier === 'object'
? (opts.didOpts?.identifierOpts.identifier as IIdentifier).keys[0].kid
: !!opts.didOpts?.identifierOpts.kid
? opts.didOpts?.identifierOpts.kid
: undefined)
if (!keyRef) {
throw Error('Key ref is needed for access token signer')
}
if (keyRef.startsWith('did:')) {
const did = keyRef.split('#')[0]
let vm: string | undefined
if (keyRef.includes('#')) {
vm = keyRef.split('#')[1]
}
const identifier = await getIdentifier({ identifier: did }, context)
let key: IKey | undefined
if (vm) {
key = await getKey(identifier, 'assertionMethod', context, vm)
keyRef = key?.kid
}
if (!key) {
key = await getFirstKeyWithRelation(identifier, context, 'assertionMethod', false)
if (!key) {
key = await getFirstKeyWithRelation(identifier, context, 'verificationMethod', true)
}
keyRef = key?.kid
}
}
return keyRef
}
export function getAccessTokenSignerCallback(
opts: {
iss?: string
keyRef?: string
didOpts?: IDIDOptions
},
context: IRequiredContext,
) {
const signer = (data: string | Uint8Array) => {
let dataString, encoding: 'base64' | undefined
const keyRef = opts.keyRef ?? opts?.didOpts?.identifierOpts?.kid
if (!keyRef) {
throw Error('Cannot sign access tokens without a key ref')
}
if (typeof data === 'string') {
dataString = data
encoding = undefined
} else {
dataString = bytesToBase64(data)
encoding = 'base64'
}
return context.agent.keyManagerSign({ keyRef, data: dataString, encoding })
}
async function accessTokenSignerCallback(jwt: Jwt, kid?: string): Promise<string> {
const issuer = opts?.iss ?? opts.didOpts?.identifierOpts?.identifier.toString()
if (!issuer) {
throw Error('No issuer configured for access tokens')
}
const result = await createJWT(jwt.payload, { signer, issuer }, { ...jwt.header, typ: 'JWT' })
return result
}
return accessTokenSignerCallback
}
export function getCredentialSignerCallback(didOpts: IDIDOptions, context: IRequiredContext): CredentialSignerCallback<DIDDocument> {
async function issueVCCallback(args: {
credentialRequest: UniformCredentialRequest
credential: CredentialIssuanceInput
jwtVerifyResult: JwtVerifyResult<DIDDocument>
format?: OID4VCICredentialFormat
}): Promise<W3CVerifiableCredential> {
const { jwtVerifyResult, format } = args
const credential = args.credential as ICredential // TODO: SDJWT
let proofFormat: ProofFormat
proofFormat = format?.includes('ld') ? 'lds' : 'jwt'
if (!credential.issuer && didOpts.identifierOpts.identifier) {
credential.issuer = toDID(didOpts.identifierOpts.identifier)
} else if (typeof credential.issuer === 'object' && !credential.issuer.id && didOpts.identifierOpts.identifier) {
credential.issuer.id = toDID(didOpts.identifierOpts.identifier)
}
const subjectIsArray = Array.isArray(credential.credentialSubject)
let credentialSubjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject]
credentialSubjects = credentialSubjects.map((subject) => {
if (!subject.id) {
subject.id = jwtVerifyResult.did
}
return subject
})
credential.credentialSubject = subjectIsArray ? credentialSubjects : credentialSubjects[0]
const result = await context.agent.createVerifiableCredential({
credential: credential as CredentialPayload,
proofFormat,
removeOriginalFields: false,
fetchRemoteContexts: true,
domain: getDID(didOpts.identifierOpts),
})
return (proofFormat === 'jwt' && 'jwt' in result.proof ? result.proof.jwt : result) as W3CVerifiableCredential
}
return issueVCCallback
}
export async function createVciIssuerBuilder(
args: {
issuerOpts: IIssuerOptions
metadata: CredentialIssuerMetadata
resolver?: Resolvable
credentialDataSupplier?: CredentialDataSupplier
},
context: IRequiredContext,
): Promise<VcIssuerBuilder<DIDDocument>> {
const { issuerOpts, metadata } = args
const { didOpts } = issuerOpts
const builder = new VcIssuerBuilder<DIDDocument>()
const resolver = args.resolver ?? args?.issuerOpts?.didOpts?.resolveOpts?.resolver ?? args.issuerOpts?.didOpts?.resolveOpts?.jwtVerifyOpts?.resolver
if (!resolver) {
throw Error('A Resolver is necessary to verify DID JWTs')
}
const jwtVerifyOpts: JWTVerifyOptions = {
...args?.issuerOpts?.didOpts?.resolveOpts?.jwtVerifyOpts,
resolver,
audience: metadata.credential_issuer,
}
builder.withIssuerMetadata(metadata)
builder.withUserPinRequired(issuerOpts.userPinRequired ?? false)
builder.withCredentialSignerCallback(getCredentialSignerCallback(didOpts, context))
builder.withJWTVerifyCallback(getJwtVerifyCallback({ verifyOpts: jwtVerifyOpts }, context))
if (args.credentialDataSupplier) {
builder.withCredentialDataSupplier(args.credentialDataSupplier)
}
builder.withInMemoryCNonceState()
builder.withInMemoryCredentialOfferState()
builder.withInMemoryCredentialOfferURIState()
builder.build()
return builder
}
export async function createVciIssuer(
{
issuerOpts,
metadata,
credentialDataSupplier,
}: {
issuerOpts: IIssuerOptions
metadata: CredentialIssuerMetadata
credentialDataSupplier?: CredentialDataSupplier
},
context: IRequiredContext,
): Promise<VcIssuer<DIDDocument>> {
return (await createVciIssuerBuilder({ issuerOpts, metadata, credentialDataSupplier }, context)).build()
}