package verify import ( "bytes" "encoding/base64" "html/template" "image/png" "net/http" "github.com/boombuler/barcode" "github.com/boombuler/barcode/code128" "github.com/boombuler/barcode/qr" "github.com/gin-gonic/gin" "edge-infra.dev/pkg/edge/iam/log" "edge-infra.dev/pkg/edge/iam/verify/templates" ) func (v *Verifier) viewBarcode(ctx *gin.Context) { log := log.Get(ctx.Request.Context()) // Access the form data barcodeValue := ctx.Query("barcode") if barcodeValue == "" { log.Info("No barcode value found") ctx.AbortWithStatus(http.StatusBadRequest) return } result := &BarcodeScanner{} result.Token = barcodeValue var bc barcode.Barcode // generate image for the code128 barcode if len(barcodeValue) == GetBarcodeLength() { bc, _ = code128.EncodeWithoutChecksum(barcodeValue) bc, _ = barcode.Scale(bc, 700, 150) } else { bc, _ = qr.Encode(barcodeValue, qr.L, qr.Auto) bc, _ = barcode.Scale(bc, 256, 256) } var buf bytes.Buffer _ = png.Encode(&buf, bc) result.Image = base64.StdEncoding.EncodeToString(buf.Bytes()) result.ScanBarcodeURI = v.ClientURL + "/verify/scan-barcode?barcode=" t, err := template.New("").Parse(templates.BarcodeScanner) tpl := template.Must(t, err) var w bytes.Buffer _ = tpl.Execute(&w, result) // Write the HTML to the response ctx.Data(http.StatusOK, gin.MIMEHTML, w.Bytes()) }