forked from hesusruiz/VCBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issuer.go
427 lines (343 loc) · 12.3 KB
/
issuer.go
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package issuer
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"os"
"strings"
"time"
"github.com/evidenceledger/vcdemo/back/handlers"
"github.com/evidenceledger/vcdemo/internal/util"
"github.com/evidenceledger/vcdemo/vault"
"github.com/evidenceledger/vcdemo/vault/x509util"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/csrf"
"github.com/gofiber/fiber/v2/utils"
"github.com/hesusruiz/vcutils/yaml"
zlog "github.com/rs/zerolog/log"
qrcode "github.com/skip2/go-qrcode"
"github.com/valyala/fasttemplate"
)
const defaultPassword = "ThePassword"
const defaultStaticDir = "back/www"
const issuerAPIPrefix = "/issuer/api/v1"
type Issuer struct {
rootServer *handlers.Server
vault *vault.Vault
cfg *yaml.YAML
id string
name string
did string
}
// Setup creates and setups the Issuer routes
func Setup(s *handlers.Server, cfg *yaml.YAML) {
var err error
issuer := &Issuer{}
issuer.rootServer = s
issuer.cfg = cfg
issuer.id = issuer.cfg.String("id")
issuer.name = issuer.cfg.String("name")
// Connect to the Issuer vault
if issuer.vault, err = vault.New(issuer.cfg); err != nil {
panic(err)
}
// Read the configured x509 certificate generation
e := cfg.Map("x509.ELSIName")
elsiName := x509util.ELSIName{
OrganizationIdentifier: (e["OrganizationIdentifier"]).(string),
CommonName: (e["CommonName"]).(string),
GivenName: (e["GivenName"]).(string),
Surname: (e["Surname"]).(string),
EmailAddress: (e["EmailAddress"]).(string),
SerialNumber: (e["SerialNumber"]).(string),
Organization: (e["Organization"]).(string),
Country: (e["Country"]).(string),
}
// Create the issuer user
// TODO: the password is only for testing
// user, err := issuer.vault.CreateOrGetUserWithDIDKey(issuer.id, issuer.name, "legalperson", cfg.String("issuer.password", defaultPassword))
user, err := issuer.vault.CreateOrGetUserWithDIDelsi(issuer.id, issuer.name, elsiName, "legalperson", cfg.String("issuer.password", defaultPassword))
if err != nil {
panic(err)
}
issuer.did = user.DID()
zlog.Info().Str("id", issuer.id).Str("name", issuer.name).Str("DID", issuer.did).Msg("starting Issuer")
// CSRF for protecting the forms
csrfHandler := csrf.New(csrf.Config{
KeyLookup: "form:_csrf",
ContextKey: "csrftoken",
CookieName: "csrf_",
CookieSameSite: "Strict",
Expiration: 1 * time.Hour,
KeyGenerator: utils.UUID,
})
s.Get("/issuer", issuer.HandleIssuerHome)
// Define the prefix for Issuer routes
issuerRoutes := s.Group(issuerAPIPrefix)
issuerRoutes.Use(cors.New())
// Forms for new credential
issuerRoutes.Get("/newcredential", csrfHandler, issuer.IssuerPageNewCredentialFormDisplay)
issuerRoutes.Post("/newcredential", csrfHandler, issuer.IssuerPageNewCredentialFormPost)
issuerRoutes.Post("/credential", issuer.CreateNewCredential)
// Display details of a credential
issuerRoutes.Get("/creddetails/:id", issuer.IssuerPageCredentialDetails)
// Display a QR with a URL for retrieving the credential from the server
issuerRoutes.Get("/displayqrurl/:id", issuer.IssuerPageDisplayQRURL)
// Get a list of all credentials
issuerRoutes.Get("/allcredentials", issuer.IssuerAPIAllCredentials)
// Get a credential given its ID
issuerRoutes.Get("/credential/:id", issuer.IssuerAPICredential)
}
func (i *Issuer) HandleIssuerHome(c *fiber.Ctx) error {
// Get the list of credentials
credsSummary := i.vault.GetAllCredentials()
// Render template
m := fiber.Map{
"apiPrefix": issuerAPIPrefix,
"credlist": credsSummary,
}
return c.Render("issuer_home", m)
}
func (i *Issuer) IssuerAPIAllCredentials(c *fiber.Ctx) error {
// Get the list of credentials
credsSummary := i.vault.GetAllCredentials()
return c.JSON(credsSummary)
}
func (i *Issuer) IssuerPageDisplayQRURL(c *fiber.Ctx) error {
// Get the credential ID from the path parameter
id := c.Params("id")
// QR code for cross-device SIOP
template := "openid-credential-offer://{{hostname}}{{prefix}}/credential/{{id}}"
t := fasttemplate.New(template, "{{", "}}")
issuerCredentialURI := t.ExecuteString(map[string]interface{}{
"protocol": c.Protocol(),
"hostname": c.Hostname(),
"prefix": issuerAPIPrefix,
"id": id,
})
// Create the QR
png, err := qrcode.Encode(issuerCredentialURI, qrcode.Medium, 256)
if err != nil {
return err
}
// Convert to a dataURL
base64Img := base64.StdEncoding.EncodeToString(png)
base64Img = "data:image/png;base64," + base64Img
// URL to direct the wallet to retrieve the credential
sameDeviceWallet := i.cfg.String("samedeviceWallet", "https://wallet.mycredential.eu")
template = "{{sameDeviceWallet}}?command=getvc&vcid={{protocol}}://{{hostname}}{{prefix}}/credential/{{id}}"
t = fasttemplate.New(template, "{{", "}}")
sameDeviceURI := t.ExecuteString(map[string]interface{}{
"sameDeviceWallet": sameDeviceWallet,
"protocol": c.Protocol(),
"hostname": c.Hostname(),
"prefix": issuerAPIPrefix,
"id": id,
})
// Render index
m := fiber.Map{
"apiPrefix": issuerAPIPrefix,
"qrcode": base64Img,
"url": sameDeviceURI,
}
return c.Render("issuer_present_qr", m)
}
func (i *Issuer) IssuerAPICredential(c *fiber.Ctx) error {
// Get the ID of the credential
credID := c.Params("id")
// Get the raw credential from the Vault
rawCred, err := i.vault.DB().Credential.Get(context.Background(), credID)
if err != nil {
return err
}
return c.SendString(string(rawCred.Raw))
}
// ##########################################
// ##########################################
// New Credential begin
func (i *Issuer) IssuerPageNewCredentialFormDisplay(c *fiber.Ctx) error {
// Display the form to enter credential data
m := fiber.Map{
"apiPrefix": issuerAPIPrefix,
"csrftoken": c.Locals("csrftoken"),
}
return c.Render("issuer_newcredential", m)
}
type NewCredentialForm struct {
FirstName string `form:"firstName,omitempty"`
FamilyName string `form:"familyName,omitempty"`
Email string `form:"email,omitempty"`
Target string `form:"target,omitempty"`
Roles string `form:"roles,omitempty"`
}
type waltResponse struct {
*json.RawMessage
}
func (i *Issuer) CreateNewCredential(c *fiber.Ctx) error {
zlog.Info().Msg("Create a new credential")
// The user submitted the form. Get all the data
newCred := &NewCredentialForm{}
if err := c.BodyParser(newCred); err != nil {
zlog.Err(err).Msgf("error parsing")
return err
}
credentialString, err := i.createNewCredential(newCred)
if err != nil {
zlog.Err(err).Msg("error creating credential")
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
var rawMessage json.RawMessage = credentialString
jsonResponse := &waltResponse{&rawMessage}
return c.JSON(jsonResponse)
}
func (i *Issuer) createNewCredential(newCred *NewCredentialForm) (credString []byte, err error) {
panic("Not implemented")
}
func (i *Issuer) IssuerPageNewCredentialFormPost(c *fiber.Ctx) error {
// The user submitted the form. Get all the data
newCred := &NewCredentialForm{}
if err := c.BodyParser(newCred); err != nil {
return err
}
str, err := i.createNewCredential(newCred)
if err != nil && err.Error() == "invalid_credential" {
m := fiber.Map{}
m["Errormessage"] = "Enter all fields"
return c.Render("issuer_newcredential", m)
}
// Render
m := fiber.Map{
"apiPrefix": issuerAPIPrefix,
"claims": util.PrettyFormatJSON(str),
}
return c.Render("creddetails", m)
}
// New Credential end
// ##########################################
// ##########################################
func (i *Issuer) IssuerPageCredentialDetails(c *fiber.Ctx) error {
// Get the ID of the credential
credID := c.Params("id")
entCredential, err := i.vault.DB().Credential.Get(context.Background(), credID)
if err != nil {
return err
}
var iface any
err = json.Unmarshal(entCredential.Raw, &iface)
if err != nil {
return err
}
var b bytes.Buffer
err = json.Indent(&b, entCredential.Raw, "", " ")
if err != nil {
return err
}
// Render
m := fiber.Map{
"APIPrefix": issuerAPIPrefix,
"claims": b.String(),
}
return c.Render("creddetails", m)
}
const defaultCredentialDataFile = "employee_data.yaml"
func BatchGenerateCredentials(issuerConfig *yaml.YAML) {
// Get the name of the SQLite database file from the config URI, e.g: "issuer.sqlite?mode=rwc&cache=shared&_fk=1"
storeDataSourceName := issuerConfig.String("store.dataSourceName")
parts := strings.Split(storeDataSourceName, "?")
if len(parts) == 0 {
panic("invalid Issuer storeDataSourceName")
}
storeDataSourceName = parts[0]
storeDataSourceLocation := issuerConfig.String("store.dataSourceLocation")
storeDataSourceFullName := storeDataSourceLocation + "/" + storeDataSourceName
// We do nothing if the file already exists, or panic if an error happened
_, err := os.Stat(storeDataSourceFullName)
if err == nil {
zlog.Info().Str("name", storeDataSourceFullName).Msg("database already exists, doing nothing")
return
} else if !os.IsNotExist(err) {
panic("error checking existence of file")
}
// At this point, we know the file does not exist and we can create it and the credentials
// Connect to the Issuer vault
issuerVault := vault.Must(vault.New(issuerConfig))
// Parse credential data
credentialDataFile := issuerConfig.String("credentialInputDataFile", defaultCredentialDataFile)
data, err := yaml.ParseYamlFile(credentialDataFile)
if err != nil {
panic(err)
}
// Get the top-level list (the list of credentials)
creds := data.List("")
if len(creds) == 0 {
panic("no credentials found in config")
}
// Iterate through the list creating each credential which will use its own template
for _, item := range creds {
// Cast to a map so it can be passed to CreateCredentialFromMap
cred, _ := item.(map[string]any)
_, _, err := issuerVault.CreateCredentialJWTFromMap(cred)
if err != nil {
zlog.Err(err).Send()
continue
}
}
}
func BatchGenerateLEARCredentials(issuerConfig *yaml.YAML) {
zlog.Info().Msg("creating LEAR Credentials")
e := issuerConfig.Map("x509.ELSIName")
elsiName := x509util.ELSIName{
OrganizationIdentifier: (e["OrganizationIdentifier"]).(string),
CommonName: (e["CommonName"]).(string),
GivenName: (e["GivenName"]).(string),
Surname: (e["Surname"]).(string),
EmailAddress: (e["EmailAddress"]).(string),
SerialNumber: (e["SerialNumber"]).(string),
Organization: (e["Organization"]).(string),
Country: (e["Country"]).(string),
}
// Get the name of the SQLite database file from the config URI, e.g: "issuer.sqlite?mode=rwc&cache=shared&_fk=1"
storeDataSourceName := issuerConfig.String("store.dataSourceName")
parts := strings.Split(storeDataSourceName, "?")
if len(parts) == 0 {
panic("invalid Issuer storeDataSourceName")
}
storeDataSourceName = parts[0]
storeDataSourceLocation := issuerConfig.String("store.dataSourceLocation")
storeDataSourceFullName := storeDataSourceLocation + "/" + storeDataSourceName
// We do nothing if the file already exists, or panic if an error happened
_, err := os.Stat(storeDataSourceFullName)
if err == nil {
zlog.Info().Str("name", storeDataSourceFullName).Msg("database already exists, doing nothing")
return
} else if !os.IsNotExist(err) {
panic("error checking existence of file")
}
// At this point, we know the file does not exist and we can create it and the credentials
// Connect to the Issuer vault
issuerVault := vault.Must(vault.New(issuerConfig))
// Parse credential data
credentialDataFile := "data/example_data/employee_data_lear.yaml"
// credentialDataFile := issuerConfig.String("credentialInputDataFile", defaultCredentialDataFile)
data, err := yaml.ParseYamlFile(credentialDataFile)
if err != nil {
panic(err)
}
// Get the top-level list (the list of credentials)
creds := data.List("")
if len(creds) == 0 {
panic("no credentials found in config")
}
// Iterate through the list creating each credential which will use its own template
for _, item := range creds {
// Cast to a map so it can be passed to CreateCredentialFromMap
cred, _ := item.(map[string]any)
_, _, err := issuerVault.CreateLEARCredentialJWTFromMap(cred, elsiName)
if err != nil {
zlog.Err(err).Send()
continue
}
}
}