QR codes complete, account settings finished!

+ refactored templates a little; this might need more work!
This commit is contained in:
ari melody 2025-01-26 20:09:18 +00:00
parent 1edc051ae2
commit 3450d879ac
Signed by: ari
GPG key ID: CF99829C92678188
13 changed files with 175 additions and 135 deletions

View file

@ -63,7 +63,7 @@ func accountIndexHandler(app *model.AppState) http.Handler {
session.Message = sessionMessage
session.Error = sessionError
err = pages["account"].Execute(w, accountResponse{
err = accountTemplate.Execute(w, accountResponse{
Session: session,
TOTPs: totps,
})
@ -199,7 +199,7 @@ func totpSetupHandler(app *model.AppState) http.Handler {
session := r.Context().Value("session").(*model.Session)
err := pages["totp-setup"].Execute(w, totpSetupData{ Session: session })
err := totpSetupTemplate.Execute(w, totpSetupData{ Session: session })
if err != nil {
fmt.Printf("WARN: Failed to render TOTP setup page: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -216,6 +216,7 @@ func totpSetupHandler(app *model.AppState) http.Handler {
Session *model.Session
TOTP *model.TOTP
NameEscaped string
QRBase64Image string
}
err := r.ParseForm()
@ -242,7 +243,7 @@ func totpSetupHandler(app *model.AppState) http.Handler {
if err != nil {
fmt.Printf("WARN: Failed to create TOTP method: %s\n", err)
controller.SetSessionError(app.DB, session, "Something went wrong. Please try again.")
err := pages["totp-setup"].Execute(w, totpSetupData{ Session: session })
err := totpSetupTemplate.Execute(w, totpSetupData{ Session: session })
if err != nil {
fmt.Printf("WARN: Failed to render TOTP setup page: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -250,10 +251,24 @@ func totpSetupHandler(app *model.AppState) http.Handler {
return
}
err = pages["totp-confirm"].Execute(w, totpSetupData{
qrBase64Image, err := controller.GenerateQRCode(
controller.GenerateTOTPURI(session.Account.Username, totp.Secret))
if err != nil {
fmt.Printf("WARN: Failed to generate TOTP setup QR code: %s\n", err)
controller.SetSessionError(app.DB, session, "Something went wrong. Please try again.")
err := totpSetupTemplate.Execute(w, totpSetupData{ Session: session })
if err != nil {
fmt.Printf("WARN: Failed to render TOTP setup page: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
err = totpConfirmTemplate.Execute(w, totpSetupData{
Session: session,
TOTP: &totp,
NameEscaped: url.PathEscape(totp.Name),
QRBase64Image: qrBase64Image,
})
if err != nil {
fmt.Printf("WARN: Failed to render TOTP confirm page: %s\n", err)
@ -304,18 +319,12 @@ func totpConfirmHandler(app *model.AppState) http.Handler {
return
}
fmt.Printf(
"TOTP:\n\tName: %s\n\tSecret: %s\n",
totp.Name,
totp.Secret,
)
confirmCode := controller.GenerateTOTP(totp.Secret, 0)
if code != confirmCode {
confirmCodeOffset := controller.GenerateTOTP(totp.Secret, 1)
if code != confirmCodeOffset {
controller.SetSessionError(app.DB, session, "Incorrect TOTP code. Please try again.")
err = pages["totp-confirm"].Execute(w, totpConfirmData{
err = totpConfirmTemplate.Execute(w, totpConfirmData{
Session: session,
TOTP: totp,
})