forked from hesusruiz/VCBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issuer.go
515 lines (407 loc) · 14.5 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package issuernew
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"database/sql"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"encoding/pem"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path"
"github.com/Masterminds/sprig/v3"
"github.com/evidenceledger/vcdemo/vault/x509util"
"github.com/google/uuid"
my "github.com/hesusruiz/vcutils/yaml"
"github.com/labstack/echo/v5"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multicodec"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/security"
pbtemplate "github.com/pocketbase/pocketbase/tools/template"
"github.com/skip2/go-qrcode"
)
const defaultCredentialTemplatesDir = "vault/templates"
const signerApiGroupPrefix = "/apisigner"
const userApiGroupPrefix = "/apiuser"
var credTemplate *template.Template
func initCredentialTemplates(credentialTemplatesPath string) {
credTemplate = template.Must(template.New("base").Funcs(sprig.TxtFuncMap()).ParseGlob(credentialTemplatesPath))
}
// LookupEnvOrString gets a value from the environment or returns the specified default value
func LookupEnvOrString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
type IssuerServer struct {
App *pocketbase.PocketBase
cfg *my.YAML
treg *pbtemplate.Registry
}
func New(cfg *my.YAML) *IssuerServer {
is := &IssuerServer{}
is.App = pocketbase.New()
is.cfg = cfg
return is
}
// Start initializes the Issuer hooks and adds routes and spawns the server to handle them
func (is *IssuerServer) Start() error {
app := is.App
cfg := is.cfg
// // Check that we can access to the certificate
// if _, err := getConfigPrivateKey(); err != nil {
// return err
// }
// Create the HTML templates registry
is.treg = pbtemplate.NewRegistry()
is.treg.AddFuncs(sprig.FuncMap())
// Initialize the Go HTML templates for credential creation
credentialTemplatesDir := cfg.String("credentialTemplatesDir", defaultCredentialTemplatesDir)
credentialTemplatesPath := path.Join(credentialTemplatesDir, "*.tpl")
initCredentialTemplates(credentialTemplatesPath)
// Perform initialization of Pocketbase before serving requests
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
dao := e.App.Dao()
// The configured TCP address for the server to listen
e.Server.Addr = cfg.String("listenAddress", ":8090")
// The default Settings
settings, _ := dao.FindSettings()
settings.Meta.AppName = cfg.String("Meta.appName", "DOME Issuer")
settings.Meta.AppUrl = cfg.String("Meta.appUrl", "issuer.mycredential.eu")
settings.Logs.MaxDays = 2
settings.Meta.SenderName = cfg.String("Meta.senderName", "Support")
settings.Meta.SenderAddress = cfg.String("Meta.senderAddress", "admin@mycredential.eu")
settings.Smtp.Enabled = cfg.Bool("SMTP.Enabled", true)
settings.Smtp.Host = cfg.String("SMTP.Host", "example.com")
settings.Smtp.Port = cfg.Int("SMTP.Port", 465)
settings.Smtp.Tls = cfg.Bool("SMTP.Tls", true)
settings.Smtp.Username = cfg.String("SMTP.Username", "admin@mycredential.eu")
// settings.Smtp.Password = cfg.String("SMTP.Password")
// Write the settings to the database
err := dao.SaveSettings(settings)
if err != nil {
return err
}
log.Println("Running as", settings.Meta.AppName, "in", settings.Meta.AppUrl)
// Create the default admin if needed
adminEmail := cfg.String("admin.email")
if len(adminEmail) == 0 {
log.Fatal("Email for server administrator is not specified in the configuration file")
}
admin, err := dao.FindAdminByEmail(adminEmail)
if err != nil {
return err
}
if admin == nil {
admin = &models.Admin{}
admin.Email = adminEmail
admin.SetPassword("1234567890")
err = dao.SaveAdmin(admin)
if err != nil {
return err
}
log.Println("Default Admin added:", admin.Email)
} else {
log.Println("Default Admin already existed:", admin.Email)
}
// Middleware to select the home page depending on the type of user (signers or holders)
e.Router.Use(selectHomePage)
// Serves static files from the provided public dir (if exists)
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./www"), false))
// Add routes for Signers
is.addSignerRoutes(e)
// Add routes for Holders
is.addUserRoutes(e)
return nil
})
// Hook to add certificate information when creating a user which can sign credentials
app.OnRecordBeforeCreateRequest("signers").Add(func(e *core.RecordCreateEvent) error {
// The request must have information about the x509 certificate used for client auth
receivedCert, _, subject, err := getX509UserFromHeader(e.HttpContext.Request())
if err != nil {
return err
}
// Perform some verifications of the certificate
if len(receivedCert.SubjectKeyId) == 0 {
return fmt.Errorf("invalid certificate, Subject Key Identifier does not exist")
}
if len(subject.SerialNumber) == 0 {
return fmt.Errorf("invalid certificate, Subject SerialNumber does not exist")
}
if len(subject.CommonName) == 0 {
return fmt.Errorf("invalid certificate, Subject CommonName does not exist")
}
// Enrich the incoming Record with the subject info in the certificate
e.Record.Set("ski", hex.EncodeToString(receivedCert.SubjectKeyId))
e.Record.Set("commonName", subject.CommonName)
e.Record.Set("serialNumber", subject.SerialNumber)
e.Record.Set("country", subject.Country)
// If it is a personal certificate, fill the Organization info with the personal one
if len(subject.Organization) > 0 {
e.Record.Set("organization", subject.Organization)
} else {
e.Record.Set("organization", subject.CommonName)
}
if len(subject.OrganizationIdentifier) > 0 {
e.Record.Set("organizationIdentifier", subject.OrganizationIdentifier)
} else {
e.Record.Set("organizationIdentifier", subject.SerialNumber)
}
cert, err := getCertPEMFromHeader(e.HttpContext.Request())
if err != nil {
return err
}
// Enrich the Record with the new CA certificate, which will be used to sign
e.Record.Set("certificatePem", string(cert))
return nil
})
// Hook to ensure that authorization for "signers" requires a client x509 certificate
app.OnRecordBeforeAuthWithPasswordRequest("signers").Add(func(e *core.RecordAuthWithPasswordEvent) error {
receivedCert, _, subject, err := getX509UserFromHeader(e.HttpContext.Request())
if err != nil {
return err
}
log.Println("OnRecordBeforeAuthWithPasswordRequest")
log.Println(subject)
// Check if an auth record was found (based only on the email send by the user)
if e.Record == nil {
return apis.NewUnauthorizedError("Please register your email before", nil)
}
// Check if the Subject Key Identifier are the same in the received certificate and in the registered one
receivedSKI := hex.EncodeToString(receivedCert.SubjectKeyId)
log.Printf("SubjectKeyId: %s", receivedCert.SubjectKeyId)
registeredSKI := e.Record.GetString("ski")
log.Printf("ReceivedSKI: %s - RegisteredSKI: %s", receivedSKI, registeredSKI)
if receivedSKI != registeredSKI {
return fmt.Errorf("invalid SKI")
}
return nil
})
// Hook to ensure that creation of credentials requires a client x509 certificate
app.OnRecordBeforeCreateRequest("credentials").Add(func(e *core.RecordCreateEvent) error {
log.Println("OnRecordBeforeCreateRequest(credentials)")
_, _, subject, err := getX509UserFromHeader(e.HttpContext.Request())
if err != nil {
return err
}
log.Println(subject)
return nil
})
// Hook to send an email reminder to the subject when a new credential is created
app.OnRecordAfterCreateRequest("credentials").Add(func(e *core.RecordCreateEvent) error {
log.Println("OnRecordAfterCreateRequest(credentials)")
status := e.Record.GetString("status")
if status == "offered" {
// Send an email to the user
return is.sendEmailReminder(e.Record.Id)
}
return nil
})
app.OnRecordAfterUpdateRequest("credentials").Add(func(e *core.RecordUpdateEvent) error {
log.Println("OnRecordAfterUpdateRequest(credentials)")
status := e.Record.GetString("status")
switch status {
case "offered":
log.Println("Status is OFFERED")
case "tobesigned":
log.Println("Status is TOBESIGNED")
case "signed":
log.Println("Status is SIGNED")
}
// Check if the user already exists, to create a new one if needed
user, err := app.Dao().FindAuthRecordByEmail("users", e.Record.Email())
pass := security.RandomString(10)
if err == sql.ErrNoRows {
// The user does not exist, must create it
collection, err := app.Dao().FindCollectionByNameOrId("users")
if err != nil {
return err
}
user = models.NewRecord(collection)
user.SetPassword(pass)
user.SetEmail(e.Record.Email())
user.SetUsername(pass)
// TODO: we will later require verification process
user.SetVerified(true)
tokenKey := user.TokenKey()
log.Println("Token key", tokenKey)
if err := app.Dao().SaveRecord(user); err != nil {
return err
}
} else if err != nil {
// An error occurred (different from sql.ErrNoRows)
return err
}
log.Println(user.Email())
if status == "signed" {
// Send an email to the user
return is.sendEmailReminder(e.Record.Id)
}
return nil
})
return is.App.Start()
}
// selectHomePage determines the HTML file to serve for the root path, depending on wether the server requires client certificate
// authorization or not.
func selectHomePage(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().URL.Path == "/" {
log.Println("My host is", c.Request().Host)
clientCertDer := c.Request().Header.Get("Tls-Client-Certificate")
if clientCertDer == "" {
return c.File("www/issuerLandingPage.html")
} else {
return c.File("www/indexSigner.html")
}
}
return next(c) // proceed with the request chain
}
}
// getX509UserFromHeader retrieves the 'issuer' and 'subject' information from the
// incoming x509 client certificate
func getX509UserFromHeader(r *http.Request) (cert *x509.Certificate, issuer *x509util.ELSIName, subject *x509util.ELSIName, err error) {
// Get the clientCertDer value before processing the request
// This HTTP request header has been set by the reverse proxy in front of the application
clientCertDer := r.Header.Get("Tls-Client-Certificate")
if clientCertDer == "" {
log.Println("Client certificate not provided")
return nil, nil, nil, apis.NewBadRequestError("Invalid request", nil)
}
cert, issuer, subject, err = x509util.ParseEIDASCertB64Der(clientCertDer)
if err != nil {
log.Println("parsing eIDAS certificate", err)
return nil, nil, nil, err
}
return cert, issuer, subject, nil
}
func getCertPEMFromHeader(r *http.Request) (cert x509util.PEMCert, err error) {
// Get the clientCertDer value
// This HTTP request header has been set by the reverse proxy in front of the application
clientCertDer := r.Header.Get("Tls-Client-Certificate")
if clientCertDer == "" {
return nil, fmt.Errorf("no x509 certificate in request header")
}
rawCert, err := base64.StdEncoding.DecodeString(clientCertDer)
if err != nil {
return nil, err
}
// Encode the DER buffer into PEM, so it can be stored on disk or database
var buf bytes.Buffer
if err := pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: rawCert}); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// RequireAdminOrX509Auth middleware requires a request to have
// a valid admin Authorization header or x509 client certification set.
func RequireAdminOrX509Auth() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
admin, _ := c.Get(apis.ContextAdminKey).(*models.Admin)
if admin != nil {
return next(c)
}
_, _, _, err := getX509UserFromHeader(c.Request())
if err != nil {
return apis.NewUnauthorizedError("The request requires admin or x509 authorization token to be set.", nil)
}
return next(c)
}
}
}
func getConfigPrivateKey() (any, error) {
return rsa.GenerateKey(rand.Reader, 2048)
// userHome, err := os.UserHomeDir()
// if err != nil {
// panic(err)
// }
// certFilePath := LookupEnvOrString("CERT_FILE_PATH", filepath.Join(userHome, ".certs", "testcert.pfx"))
// password := LookupEnvOrString("CERT_PASSWORD", "")
// if len(password) == 0 {
// passwordFilePath := LookupEnvOrString("CERT_PASSWORD_FILE", filepath.Join(userHome, ".certs", "pass.txt"))
// passwordBytes, err := os.ReadFile(passwordFilePath)
// if err != nil {
// return nil, err
// }
// password = string(bytes.TrimSpace(passwordBytes))
// }
// certBinary, err := os.ReadFile(certFilePath)
// if err != nil {
// return nil, err
// }
// privateKey, _, _, err := pkcs12.DecodeChain(certBinary, password)
// if err != nil {
// return nil, err
// }
// return privateKey, nil
}
func newRandomString() string {
newID, _ := uuid.NewRandom()
return newID.String()
}
// GenDIDKey generates a new 'did:key' DID by creating an EC key pair
func GenDIDKey() (did string, privateKey jwk.Key, err error) {
// Generate a raw EC key with the P-256 curve
rawPrivKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return "", nil, err
}
// Create the JWK for the private and public pair
privKeyJWK, err := jwk.FromRaw(rawPrivKey)
if err != nil {
return "", nil, err
}
// privKeyJWK.Set(jwk.AlgorithmKey, jwa.ES256)
// privKeyJWK.Set(jwk.KeyUsageKey, jwk.ForSignature)
pubKeyJWK, err := privKeyJWK.PublicKey()
if err != nil {
return "", nil, err
}
// Create the 'did:key' associated to the public key
did, err = PubKeyToDIDKey(pubKeyJWK)
if err != nil {
return "", nil, err
}
return did, privKeyJWK, nil
}
func PubKeyToDIDKey(pubKeyJWK jwk.Key) (did string, err error) {
var buf [10]byte
n := binary.PutUvarint(buf[0:], uint64(multicodec.Jwk_jcsPub))
Jwk_jcsPub_Buf := buf[0:n]
serialized, err := json.Marshal(pubKeyJWK)
if err != nil {
return "", err
}
keyEncoded := append(Jwk_jcsPub_Buf, serialized...)
mb, err := multibase.Encode(multibase.Base58BTC, keyEncoded)
if err != nil {
return "", err
}
return "did:key:" + mb, nil
}
func qrcodeFromUrl(url string) (string, error) {
// Create the QR code
png, err := qrcode.Encode(url, qrcode.Medium, 256)
if err != nil {
return "", err
}
// Convert the image data to a dataURL
imgDataUrl := base64.StdEncoding.EncodeToString(png)
imgDataUrl = "data:image/png;base64," + imgDataUrl
return imgDataUrl, nil
}