Smaller QR image on edit OTP page

This commit is contained in:
Melon 2023-10-16 15:18:34 +01:00
parent 9fccb563e1
commit 0b63e87691
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 11 additions and 2 deletions

View File

@ -10,9 +10,9 @@
<main> <main>
<form method="POST" action="/edit/otp"> <form method="POST" action="/edit/otp">
<p> <p>
<img src="{{.OtpQr}}" alt="OTP QR code not loading"/> <img src="{{.OtpQr}}" style="width:{{.QrWidth}}px" alt="OTP QR code not loading"/>
</p> </p>
<p>Raw OTP string: {{.OtpUrl}}</p> <p style="display:none">Raw OTP string: {{.OtpUrl}}</p>
<div> <div>
<label for="field_code">OTP Code:</label> <label for="field_code">OTP Code:</label>
<input type="text" name="code" id="field_code" required pattern="[0-9]{6,8}" title="6/7/8 digit one time passcode"/> <input type="text" name="code" id="field_code" required pattern="[0-9]{6,8}" title="6/7/8 digit one time passcode"/>

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"bytes"
"crypto" "crypto"
"encoding/base64" "encoding/base64"
"github.com/1f349/tulip/database" "github.com/1f349/tulip/database"
@ -9,6 +10,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"html/template" "html/template"
"image/png"
"log" "log"
"net/http" "net/http"
) )
@ -143,6 +145,12 @@ func (h *HttpServer) EditOtpGet(rw http.ResponseWriter, req *http.Request, _ htt
http.Error(rw, "500 Internal Server Error: Failed to generate OTP QR code", http.StatusInternalServerError) http.Error(rw, "500 Internal Server Error: Failed to generate OTP QR code", http.StatusInternalServerError)
return return
} }
decode, err := png.Decode(bytes.NewReader(otpQr))
if err != nil {
return
}
b := decode.Bounds()
qrWidth := b.Dx() / 4
otpUrl, err := otp.URL() otpUrl, err := otp.URL()
if err != nil { if err != nil {
http.Error(rw, "500 Internal Server Error: Failed to generate OTP URL", http.StatusInternalServerError) http.Error(rw, "500 Internal Server Error: Failed to generate OTP URL", http.StatusInternalServerError)
@ -153,6 +161,7 @@ func (h *HttpServer) EditOtpGet(rw http.ResponseWriter, req *http.Request, _ htt
pages.RenderPageTemplate(rw, "edit-otp", map[string]any{ pages.RenderPageTemplate(rw, "edit-otp", map[string]any{
"ServiceName": h.conf.ServiceName, "ServiceName": h.conf.ServiceName,
"OtpQr": template.URL("data:image/png;base64," + base64.StdEncoding.EncodeToString(otpQr)), "OtpQr": template.URL("data:image/png;base64," + base64.StdEncoding.EncodeToString(otpQr)),
"QrWidth": qrWidth,
"OtpUrl": otpUrl, "OtpUrl": otpUrl,
}) })
} }