Skip to content

Commit

Permalink
Use ColorSettings of ServerConfig for QRCode gen.
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Enrico Ragucci <[email protected]>
  • Loading branch information
ghmer committed Mar 29, 2021
1 parent 56e87eb commit 188dceb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 12 additions & 3 deletions qrcode/qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
const FormatString string = "otpauth://totp/%s:%s@%s?algorithm=SHA1&digits=%d&issuer=%s&period=30&secret=%s"

// GenerateQrCode Generates a QRCode of the totp url
func GenerateQrCode(user structs.User, digits uint8) ([]byte, error) {
func GenerateQrCode(user structs.User, bgcolor, fgcolor structs.ColorSetting, digits uint8) ([]byte, error) {
var png []byte
secret, err := middleware.GetUserKeyBase32(user)
if err != nil {
Expand All @@ -26,14 +26,23 @@ func GenerateQrCode(user structs.User, digits uint8) ([]byte, error) {
return nil, err
}

code.BackgroundColor = color.Transparent
code.ForegroundColor = color.Black
code.BackgroundColor = convertColorSetting(bgcolor)
code.ForegroundColor = convertColorSetting(fgcolor)

png, err = code.PNG(256)

return png, err
}

func convertColorSetting(setting structs.ColorSetting) color.Color {
return color.RGBA{
R: setting.Red,
G: setting.Green,
B: setting.Blue,
A: setting.Alpha,
}
}

// WriteQrCodeImage writes a png to the filesystem
func WriteQrCodeImage(user structs.User, filePath string, digits uint8) error {
secret, err := middleware.GetUserKeyBase32(user)
Expand Down
8 changes: 7 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,13 @@ func GenerateQrCode(w http.ResponseWriter, r *http.Request) {
return
}

png, err := qrcode.GenerateQrCode(userStruct, tokenlength)
bgcolor, fgcolor, err := middleware.GetQrCodeColors()
if err != nil {
returnError(err, 500, w)
return
}

png, err := qrcode.GenerateQrCode(userStruct, bgcolor, fgcolor, tokenlength)
if err != nil {
returnError(err, 500, w)
return
Expand Down

0 comments on commit 188dceb

Please sign in to comment.