From e521ab10a116026fe60009f10e7a9aa447d7a111 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 14 Sep 2019 13:31:45 +0200 Subject: [PATCH] Allow other digit counts than 6 or 8 Signed-off-by: Knut Ahlers --- go.mod | 1 + go.sum | 2 ++ token.go | 10 ++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 761ff44..4f17dc7 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/onsi/ginkgo v1.10.1 // indirect github.com/onsi/gomega v1.7.0 // indirect github.com/pierrec/lz4 v2.3.0+incompatible // indirect + github.com/pkg/errors v0.8.1 github.com/pquerna/otp v1.2.0 github.com/sirupsen/logrus v1.4.2 github.com/tdewolff/minify v2.3.6+incompatible diff --git a/go.sum b/go.sum index 71dc026..e9aecc7 100644 --- a/go.sum +++ b/go.sum @@ -105,6 +105,8 @@ github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pierrec/lz4 v2.3.0+incompatible h1:CZzRn4Ut9GbUkHlQ7jqBXeZQV41ZSKWFc302ZU6lUTk= github.com/pierrec/lz4 v2.3.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= diff --git a/token.go b/token.go index 6d397d3..7f6bd02 100644 --- a/token.go +++ b/token.go @@ -4,11 +4,13 @@ import ( "fmt" "path" "sort" + "strconv" "strings" "sync" "time" "github.com/hashicorp/vault/api" + "github.com/pkg/errors" "github.com/pquerna/otp" "github.com/pquerna/otp/totp" log "github.com/sirupsen/logrus" @@ -36,8 +38,12 @@ func (t *token) GenerateCode(in time.Time) error { Algorithm: otp.AlgorithmSHA1, } - if t.Digits == "8" { - opts.Digits = otp.DigitsEight + if t.Digits != "" { + d, err := strconv.Atoi(t.Digits) + if err != nil { + return errors.Wrap(err, "Unable to parse digits to int") + } + opts.Digits = otp.Digits(d) } var err error