...

Source file src/edge-infra.dev/pkg/edge/iam/verify/verifier_view_barcode.go

Documentation: edge-infra.dev/pkg/edge/iam/verify

     1  package verify
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"html/template"
     7  	"image/png"
     8  	"net/http"
     9  
    10  	"github.com/boombuler/barcode"
    11  	"github.com/boombuler/barcode/code128"
    12  	"github.com/boombuler/barcode/qr"
    13  	"github.com/gin-gonic/gin"
    14  
    15  	"edge-infra.dev/pkg/edge/iam/log"
    16  	"edge-infra.dev/pkg/edge/iam/verify/templates"
    17  )
    18  
    19  func (v *Verifier) viewBarcode(ctx *gin.Context) {
    20  	log := log.Get(ctx.Request.Context())
    21  	// Access the form data
    22  	barcodeValue := ctx.Query("barcode")
    23  	if barcodeValue == "" {
    24  		log.Info("No barcode value found")
    25  		ctx.AbortWithStatus(http.StatusBadRequest)
    26  		return
    27  	}
    28  
    29  	result := &BarcodeScanner{}
    30  
    31  	result.Token = barcodeValue
    32  	var bc barcode.Barcode
    33  	// generate image for the code128 barcode
    34  	if len(barcodeValue) == GetBarcodeLength() {
    35  		bc, _ = code128.EncodeWithoutChecksum(barcodeValue)
    36  		bc, _ = barcode.Scale(bc, 700, 150)
    37  	} else {
    38  		bc, _ = qr.Encode(barcodeValue, qr.L, qr.Auto)
    39  		bc, _ = barcode.Scale(bc, 256, 256)
    40  	}
    41  	var buf bytes.Buffer
    42  	_ = png.Encode(&buf, bc)
    43  	result.Image = base64.StdEncoding.EncodeToString(buf.Bytes())
    44  
    45  	result.ScanBarcodeURI = v.ClientURL + "/verify/scan-barcode?barcode="
    46  
    47  	t, err := template.New("").Parse(templates.BarcodeScanner)
    48  	tpl := template.Must(t, err)
    49  	var w bytes.Buffer
    50  	_ = tpl.Execute(&w, result)
    51  
    52  	// Write the HTML to the response
    53  	ctx.Data(http.StatusOK, gin.MIMEHTML, w.Bytes())
    54  }
    55  

View as plain text