4c0e3c5d2f
* Initialized basic outline of TOTP backend using Postgresql backend as template * Updated TOTP backend.go's structure and help string * Updated TOTP path_roles.go's structure and help strings * Updated TOTP path_role_create.go's structure and help strings * Fixed typo in path_roles.go * Fixed errors in path_role_create.go and path_roles.go * Added TOTP secret backend information to cli commands * Fixed build errors in path_roles.go and path_role_create.go * Changed field values of period and digits from uint to int, added uint conversion of period when generating passwords * Initialized TOTP test file based on structure of postgresql test file * Added enforcement of input values * Added otp library to vendor folder * Added test steps and cleaned up errors * Modified read credential test step, not working yet * Use of vendored package not allowed - Test error * Removed vendor files for TOTP library * Revert "Removed vendor files for TOTP library" This reverts commit fcd030994bc1741dbf490f3995944e091b11da61. * Hopefully fixed vendor folder issue with TOTP Library * Added additional tests for TOTP backend * Cleaned up comments in TOTP backend_test.go * Added default values of period, algorithm and digits to field schema * Changed account_name and issuer fields to optional * Removed MD5 as a hash algorithm option * Implemented requested pull request changes * Added ability to validate TOTP codes * Added ability to have a key generated * Added skew, qr size and key size parameters * Reset vendor.json prior to merge * Readded otp and barcode libraries to vendor.json * Modified help strings for path_role_create.go * Fixed test issue in testAccStepReadRole * Cleaned up error formatting, variable names and path names. Also added some additional documentation * Moveed barcode and url output to key creation function and did some additional cleanup based on requested changes * Added ability to pass in TOTP urls * Added additional tests for TOTP server functions * Removed unused QRSize, URL and Generate members of keyEntry struct * Removed unnecessary urlstring variable from pathKeyCreate * Added website documentation for TOTP secret backend * Added errors if generate is true and url or key is passed, removed logger from backend, and revised parameter documentation. * Updated website documentation and added QR example * Added exported variable and ability to disable QR generation, cleaned up error reporting, changed default skew value, updated documentation and added additional tests * Updated API documentation to inlude to exported variable and qr size option * Cleaned up return statements in path_code, added error handling while validating codes and clarified documentation for generate parameters in path_keys
135 lines
2.8 KiB
Go
135 lines
2.8 KiB
Go
package barcode
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
)
|
|
|
|
type wrapFunc func(x, y int) color.Color
|
|
|
|
type scaledBarcode struct {
|
|
wrapped Barcode
|
|
wrapperFunc wrapFunc
|
|
rect image.Rectangle
|
|
}
|
|
|
|
type intCSscaledBC struct {
|
|
scaledBarcode
|
|
}
|
|
|
|
func (bc *scaledBarcode) Content() string {
|
|
return bc.wrapped.Content()
|
|
}
|
|
|
|
func (bc *scaledBarcode) Metadata() Metadata {
|
|
return bc.wrapped.Metadata()
|
|
}
|
|
|
|
func (bc *scaledBarcode) ColorModel() color.Model {
|
|
return bc.wrapped.ColorModel()
|
|
}
|
|
|
|
func (bc *scaledBarcode) Bounds() image.Rectangle {
|
|
return bc.rect
|
|
}
|
|
|
|
func (bc *scaledBarcode) At(x, y int) color.Color {
|
|
return bc.wrapperFunc(x, y)
|
|
}
|
|
|
|
func (bc *intCSscaledBC) CheckSum() int {
|
|
if cs, ok := bc.wrapped.(BarcodeIntCS); ok {
|
|
return cs.CheckSum()
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// Scale returns a resized barcode with the given width and height.
|
|
func Scale(bc Barcode, width, height int) (Barcode, error) {
|
|
switch bc.Metadata().Dimensions {
|
|
case 1:
|
|
return scale1DCode(bc, width, height)
|
|
case 2:
|
|
return scale2DCode(bc, width, height)
|
|
}
|
|
|
|
return nil, errors.New("unsupported barcode format")
|
|
}
|
|
|
|
func newScaledBC(wrapped Barcode, wrapperFunc wrapFunc, rect image.Rectangle) Barcode {
|
|
result := &scaledBarcode{
|
|
wrapped: wrapped,
|
|
wrapperFunc: wrapperFunc,
|
|
rect: rect,
|
|
}
|
|
|
|
if _, ok := wrapped.(BarcodeIntCS); ok {
|
|
return &intCSscaledBC{*result}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
|
|
orgBounds := bc.Bounds()
|
|
orgWidth := orgBounds.Max.X - orgBounds.Min.X
|
|
orgHeight := orgBounds.Max.Y - orgBounds.Min.Y
|
|
|
|
factor := int(math.Min(float64(width)/float64(orgWidth), float64(height)/float64(orgHeight)))
|
|
if factor <= 0 {
|
|
return nil, fmt.Errorf("can not scale barcode to an image smaller than %dx%d", orgWidth, orgHeight)
|
|
}
|
|
|
|
offsetX := (width - (orgWidth * factor)) / 2
|
|
offsetY := (height - (orgHeight * factor)) / 2
|
|
|
|
wrap := func(x, y int) color.Color {
|
|
if x < offsetX || y < offsetY {
|
|
return color.White
|
|
}
|
|
x = (x - offsetX) / factor
|
|
y = (y - offsetY) / factor
|
|
if x >= orgWidth || y >= orgHeight {
|
|
return color.White
|
|
}
|
|
return bc.At(x, y)
|
|
}
|
|
|
|
return newScaledBC(
|
|
bc,
|
|
wrap,
|
|
image.Rect(0, 0, width, height),
|
|
), nil
|
|
}
|
|
|
|
func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
|
|
orgBounds := bc.Bounds()
|
|
orgWidth := orgBounds.Max.X - orgBounds.Min.X
|
|
factor := int(float64(width) / float64(orgWidth))
|
|
|
|
if factor <= 0 {
|
|
return nil, fmt.Errorf("can not scale barcode to an image smaller than %dx1", orgWidth)
|
|
}
|
|
offsetX := (width - (orgWidth * factor)) / 2
|
|
|
|
wrap := func(x, y int) color.Color {
|
|
if x < offsetX {
|
|
return color.White
|
|
}
|
|
x = (x - offsetX) / factor
|
|
|
|
if x >= orgWidth {
|
|
return color.White
|
|
}
|
|
return bc.At(x, 0)
|
|
}
|
|
|
|
return newScaledBC(
|
|
bc,
|
|
wrap,
|
|
image.Rect(0, 0, width, height),
|
|
), nil
|
|
}
|