@sphereon/ssi-sdk.w3c-vc-api 0.13.0 → 0.13.1-next.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"version":3,"file":"VCAPIServer.js","sourceRoot":"","sources":["../src/VCAPIServer.ts"],"names":[],"mappings":";AAAA,qCAAqC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKrC,8DAAoC;AACpC,kEAAwC;AACxC,oDAAqC;AACrC,sDAA6D;AAC7D,+BAAyB;AAiEzB,MAAa,WAAW;IAKtB,YAAY,IAA+E;;QACzF,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;QACjB,MAAM,eAAe,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,CAAA;QACtC,IAAI,CAAC,QAAQ,GAAG,MAAA,IAAI,CAAC,OAAO,mCAAI,IAAA,iBAAO,GAAE,CAAA;QACzC,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,CAAA;QAElC,uBAAuB;QACvB,IAAI,CAAC,uBAAuB,EAAE,CAAA;QAC9B,IAAI,CAAC,qBAAqB,EAAE,CAAA;QAC5B,IAAI,CAAC,sBAAsB,EAAE,CAAA;QAC7B,IAAI,CAAC,wBAAwB,EAAE,CAAA,CAAC,2BAA2B;QAC3D,IAAI,CAAC,wBAAwB,EAAE,CAAA;IACjC,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAEO,YAAY,CAAC,eAAwB;;QAC3C,MAAM,CAAC,MAAM,EAAE,CA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,CAAA;QACH,CAAC;KAAA;CACF;AAhND,kCAgNC"}
@@ -1,283 +0,0 @@
1
- // noinspection JSUnusedGlobalSymbols
2
-
3
- import { CredentialPayload, TAgent, VerifiableCredential } from '@veramo/core'
4
- import { ProofFormat } from '@veramo/core/src/types/ICredentialIssuer'
5
- import { W3CVerifiableCredential } from '@veramo/core/src/types/vc-data-model'
6
- import bodyParser from 'body-parser'
7
- import cookieParser from 'cookie-parser'
8
- import * as dotenv from 'dotenv-flow'
9
- import express, { Express, Request, Response } from 'express'
10
- import { v4 } from 'uuid'
11
- import { IRequiredPlugins } from './types'
12
-
13
- export interface IVCAPIOpts {
14
- issueCredentialOpts?: IIssueOpts
15
- serverOpts?: IServerOpts
16
- }
17
-
18
- export interface IServerOpts {
19
- port?: number // The port to listen on
20
- cookieSigningKey?: string
21
- hostname?: string // defaults to "0.0.0.0", meaning it will listen on all IP addresses. Can be an IP address or hostname
22
- }
23
-
24
- export interface IIssueOpts {
25
- issueCredentialPath?: string
26
- getCredentialsPath?: string
27
- getCredentialPath?: string
28
- deleteCredentialPath?: string
29
- verifyCredentialPath?: string
30
- verifyPresentationPath?: string
31
-
32
- persistIssuedCredentials?: boolean // Whether the issuer persists issued credentials or not. Defaults to true
33
-
34
- /**
35
- * The desired format for the VerifiablePresentation to be created.
36
- */
37
- proofFormat: ProofFormat
38
-
39
- /**
40
- * Remove payload members during JWT-JSON transformation. Defaults to `true`.
41
- * See https://www.w3.org/TR/vc-data-model/#jwt-encoding
42
- */
43
- removeOriginalFields?: boolean
44
-
45
- /**
46
- * [Optional] The ID of the key that should sign this credential.
47
- * If this is not specified, the first matching key will be used.
48
- */
49
- keyRef?: string
50
-
51
- /**
52
- * When dealing with JSON-LD you also MUST provide the proper contexts.
53
- * Set this to `true` ONLY if you want the `@context` URLs to be fetched in case they are not preloaded.
54
- * The context definitions SHOULD rather be provided at startup instead of being fetched.
55
- *
56
- * Defaults to `false`
57
- */
58
- fetchRemoteContexts?: boolean
59
- }
60
-
61
- export interface IIssueOptionsPayload {
62
- created?: string
63
- challenge?: string
64
- domain?: string
65
- credentialStatus?: {
66
- type: string
67
- }
68
- }
69
-
70
- export interface ChallengeOptsPayload {
71
- challenge?: string
72
- domain?: string
73
- }
74
-
75
- export class VCAPIServer {
76
- private readonly _express: Express
77
- private readonly _agent: TAgent<IRequiredPlugins>
78
- private readonly _opts?: IVCAPIOpts
79
-
80
- constructor(args: { agent: TAgent<IRequiredPlugins>; express?: Express; opts?: IVCAPIOpts }) {
81
- const { agent, opts } = args
82
- this._agent = agent
83
- this._opts = opts
84
- const existingExpress = !!args.express
85
- this._express = args.express ?? express()
86
- this.setupExpress(existingExpress)
87
-
88
- // Credential endpoints
89
- this.issueCredentialEndpoint()
90
- this.getCredentialEndpoint()
91
- this.getCredentialsEndpoint()
92
- this.deleteCredentialEndpoint() // not in spec. TODO: Authz
93
- this.verifyCredentialEndpoint()
94
- }
95
-
96
- get agent(): TAgent<IRequiredPlugins> {
97
- return this._agent
98
- }
99
-
100
- get opts(): IVCAPIOpts | undefined {
101
- return this._opts
102
- }
103
-
104
- get express(): Express {
105
- return this._express
106
- }
107
-
108
- private setupExpress(existingExpress: boolean) {
109
- dotenv.config()
110
- if (!existingExpress) {
111
- const port = this.opts?.serverOpts?.port || process.env.PORT || 5000
112
- const secret = this.opts?.serverOpts?.cookieSigningKey || process.env.COOKIE_SIGNING_KEY
113
- const hostname = this.opts?.serverOpts?.hostname || '0.0.0.0'
114
- this._express.use((req, res, next) => {
115
- res.header('Access-Control-Allow-Origin', '*')
116
- // Request methods you wish to allow
117
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
118
-
119
- // Request headers you wish to allow
120
- res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
121
-
122
- // Set to true if you need the website to include cookies in the requests sent
123
- // to the API (e.g. in case you use sessions)
124
- res.setHeader('Access-Control-Allow-Credentials', 'true')
125
- next()
126
- })
127
- // this.express.use(cors({ credentials: true }));
128
- // this.express.use('/proxy', proxy('www.gssoogle.com'));
129
- this._express.use(bodyParser.urlencoded({ extended: true }))
130
- this._express.use(bodyParser.json())
131
- this._express.use(cookieParser(secret))
132
- this._express.listen(port as number, hostname, () => console.log(`Listening on ${hostname}, port ${port}`))
133
- }
134
- }
135
-
136
- private static sendErrorResponse(response: Response, statusCode: number, message: string) {
137
- console.log(message)
138
- response.statusCode = statusCode
139
- response.status(statusCode).send(message)
140
- }
141
-
142
- private getCredentialsEndpoint() {
143
- this._express.get(this.opts?.issueCredentialOpts?.getCredentialsPath ?? '/credentials', async (request: Request, response: Response) => {
144
- try {
145
- const uniqueVCs = await this.agent.dataStoreORMGetVerifiableCredentials()
146
- response.statusCode = 202
147
- return response.send(uniqueVCs.map((uVC) => uVC.verifiableCredential))
148
- } catch (e) {
149
- console.log(e)
150
- return VCAPIServer.sendErrorResponse(response, 500, e.message as string)
151
- }
152
- })
153
- }
154
-
155
- private getCredentialEndpoint() {
156
- this._express.get(this.opts?.issueCredentialOpts?.getCredentialPath ?? '/credentials/:id', async (request: Request, response: Response) => {
157
- try {
158
- const id = request.params.id
159
- if (!id) {
160
- return VCAPIServer.sendErrorResponse(response, 400, 'no id provided')
161
- }
162
- let vcInfo = await this.getCredentialByIdOrHash(id)
163
- if (!vcInfo.vc) {
164
- return VCAPIServer.sendErrorResponse(response, 404, `id ${id} not found`)
165
- }
166
- response.statusCode = 200
167
- return response.send(vcInfo.vc)
168
- } catch (e) {
169
- console.log(e)
170
- return VCAPIServer.sendErrorResponse(response, 500, e.message as string)
171
- }
172
- })
173
- }
174
-
175
- private verifyCredentialEndpoint() {
176
- this._express.post(
177
- this.opts?.issueCredentialOpts?.verifyCredentialPath ?? '/credentials/verify',
178
- async (request: Request, response: Response) => {
179
- try {
180
- console.log(request.body)
181
- const credential: W3CVerifiableCredential = request.body.verifiableCredential
182
- // const options: IIssueOptionsPayload = request.body.options
183
- if (!credential) {
184
- return VCAPIServer.sendErrorResponse(response, 400, 'No verifiable credential supplied')
185
- }
186
- const verifyResult = await this.agent.verifyCredential({
187
- credential,
188
- fetchRemoteContexts: this.opts?.issueCredentialOpts?.fetchRemoteContexts,
189
- })
190
-
191
- response.statusCode = 200
192
- return response.send(verifyResult)
193
- } catch (e) {
194
- console.log(e)
195
- return VCAPIServer.sendErrorResponse(response, 500, e.message as string)
196
- }
197
- }
198
- )
199
- }
200
-
201
- private deleteCredentialEndpoint() {
202
- this._express.delete(this.opts?.issueCredentialOpts?.getCredentialsPath ?? '/credentials/:id', async (request: Request, response: Response) => {
203
- try {
204
- const id = request.params.id
205
- if (!id) {
206
- return VCAPIServer.sendErrorResponse(response, 400, 'no id provided')
207
- }
208
- let vcInfo = await this.getCredentialByIdOrHash(id)
209
- if (!vcInfo.vc || !vcInfo.hash) {
210
- return VCAPIServer.sendErrorResponse(response, 404, `id ${id} not found`)
211
- }
212
- const success = this.agent.dataStoreDeleteVerifiableCredential({ hash: vcInfo.hash })
213
- if (!success) {
214
- return VCAPIServer.sendErrorResponse(response, 400, `Could not delete Verifiable Credential with id ${id}`)
215
- }
216
- response.statusCode = 200
217
- return response.send()
218
- } catch (e) {
219
- console.log(e)
220
- return VCAPIServer.sendErrorResponse(response, 500, e.message as string)
221
- }
222
- })
223
- }
224
-
225
- private issueCredentialEndpoint() {
226
- this._express.post(this.opts?.issueCredentialOpts?.issueCredentialPath ?? '/credentials/issue', async (request: Request, response: Response) => {
227
- try {
228
- const credential: CredentialPayload = request.body.credential
229
- // const options: IIssueOptionsPayload = request.body.options
230
- if (!credential) {
231
- return VCAPIServer.sendErrorResponse(response, 400, 'No credential supplied')
232
- }
233
- if (!credential.id) {
234
- credential.id = `urn:uuid:${v4()}`
235
- }
236
- const issueOpts = this.opts?.issueCredentialOpts
237
- const vc = await this._agent.createVerifiableCredential({
238
- credential,
239
- save: issueOpts?.persistIssuedCredentials ?? true,
240
- proofFormat: issueOpts?.proofFormat ?? 'lds',
241
- fetchRemoteContexts: issueOpts?.fetchRemoteContexts || true,
242
- })
243
- response.statusCode = 201
244
- return response.send({ verifiableCredential: vc })
245
- } catch (e) {
246
- console.log(e)
247
- return VCAPIServer.sendErrorResponse(response, 500, e.message as string)
248
- }
249
- })
250
- }
251
-
252
- private async getCredentialByIdOrHash(idOrHash: string): Promise<{
253
- id: string
254
- hash?: string
255
- vc?: VerifiableCredential
256
- }> {
257
- let vc: VerifiableCredential
258
- let hash: string
259
- const uniqueVCs = await this.agent.dataStoreORMGetVerifiableCredentials({
260
- where: [
261
- {
262
- column: 'id',
263
- value: [idOrHash],
264
- op: 'Equal',
265
- },
266
- ],
267
- })
268
- if (uniqueVCs.length === 0) {
269
- hash = idOrHash
270
- vc = await this.agent.dataStoreGetVerifiableCredential({ hash })
271
- } else {
272
- const uniqueVC = uniqueVCs[0]
273
- hash = uniqueVC.hash
274
- vc = uniqueVC.verifiableCredential
275
- }
276
-
277
- return {
278
- vc,
279
- id: idOrHash,
280
- hash,
281
- }
282
- }
283
- }