forked from hesusruiz/VCBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
82 lines (65 loc) · 1.98 KB
/
server.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
package handlers
import (
"encoding/base64"
"errors"
"os"
"github.com/hesusruiz/vcutils/yaml"
"github.com/skip2/go-qrcode"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/storage/memory"
)
const issuerPrefix = "/issuer/api/v1"
const verifierPrefix = "/verifier/api/v1"
const walletPrefix = "/wallet/api/v1"
const defaultWalletProvisioning = "wallet.mycredential.eu"
var (
ErrNoStateReceived = errors.New("no state received")
ErrInvalidStateReceived = errors.New("invalid state received")
ErrNoCredentialFoundInState = errors.New("no credential found in state")
ErrBadCredentialFormat = errors.New("credential received not in JSON format")
)
// Server is the struct holding the state of the server
type Server struct {
*fiber.App
Cfg *yaml.YAML
SessionStorage *memory.Storage
}
func NewServer(cfg *yaml.YAML) *Server {
srv := &Server{
App: &fiber.App{},
Cfg: cfg,
SessionStorage: &memory.Storage{},
}
return srv
}
func (s *Server) HandleHome(c *fiber.Ctx) error {
// Render index
return c.Render("index", "")
}
func (s *Server) HandleStop(c *fiber.Ctx) error {
os.Exit(0)
return nil
}
// HandleWalletProviderHome displays a QR code to be scanned and obtain the wallet
func (v *Server) HandleWalletProviderHome(c *fiber.Ctx) error {
// This is the url for the demo wallet
walletDomain := v.Cfg.String("server.walletProvisioning", defaultWalletProvisioning)
wallet_url := c.Protocol() + "://" + walletDomain
// Create the QR code
png, err := qrcode.Encode(wallet_url, qrcode.Medium, 256)
if err != nil {
return err
}
// Convert the image data to a dataURL
base64Img := base64.StdEncoding.EncodeToString(png)
base64Img = "data:image/png;base64," + base64Img
// Render the page
m := fiber.Map{
"issuerPrefix": issuerPrefix,
"verifierPrefix": verifierPrefix,
"walletPrefix": walletPrefix,
"qrcode": base64Img,
"walletDomain": walletDomain,
}
return c.Render("walletprovider_present_qr", m)
}