replace zxq.co/ripple/hanayo
This commit is contained in:
193
vendor/github.com/pquerna/otp/totp/totp.go
generated
vendored
Normal file
193
vendor/github.com/pquerna/otp/totp/totp.go
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
/**
|
||||
* Copyright 2014 Paul Querna
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package totp
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pquerna/otp"
|
||||
"github.com/pquerna/otp/hotp"
|
||||
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
"math"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Validate a TOTP using the current time.
|
||||
// A shortcut for ValidateCustom, Validate uses a configuration
|
||||
// that is compatible with Google-Authenticator and most clients.
|
||||
func Validate(passcode string, secret string) bool {
|
||||
rv, _ := ValidateCustom(
|
||||
passcode,
|
||||
secret,
|
||||
time.Now().UTC(),
|
||||
ValidateOpts{
|
||||
Period: 30,
|
||||
Skew: 1,
|
||||
Digits: otp.DigitsSix,
|
||||
Algorithm: otp.AlgorithmSHA1,
|
||||
},
|
||||
)
|
||||
return rv
|
||||
}
|
||||
|
||||
// GenerateCode creates a TOTP token using the current time.
|
||||
// A shortcut for GenerateCodeCustom, GenerateCode uses a configuration
|
||||
// that is compatible with Google-Authenticator and most clients.
|
||||
func GenerateCode(secret string, t time.Time) (string, error) {
|
||||
return GenerateCodeCustom(secret, t, ValidateOpts{
|
||||
Period: 30,
|
||||
Skew: 1,
|
||||
Digits: otp.DigitsSix,
|
||||
Algorithm: otp.AlgorithmSHA1,
|
||||
})
|
||||
}
|
||||
|
||||
// ValidateOpts provides options for ValidateCustom().
|
||||
type ValidateOpts struct {
|
||||
// Number of seconds a TOTP hash is valid for. Defaults to 30 seconds.
|
||||
Period uint
|
||||
// Periods before or after the current time to allow. Value of 1 allows up to Period
|
||||
// of either side of the specified time. Defaults to 0 allowed skews. Values greater
|
||||
// than 1 are likely sketchy.
|
||||
Skew uint
|
||||
// Digits as part of the input. Defaults to 6.
|
||||
Digits otp.Digits
|
||||
// Algorithm to use for HMAC. Defaults to SHA1.
|
||||
Algorithm otp.Algorithm
|
||||
}
|
||||
|
||||
// GenerateCodeCustom takes a timepoint and produces a passcode using a
|
||||
// secret and the provided opts. (Under the hood, this is making an adapted
|
||||
// call to hotp.GenerateCodeCustom)
|
||||
func GenerateCodeCustom(secret string, t time.Time, opts ValidateOpts) (passcode string, err error) {
|
||||
if opts.Period == 0 {
|
||||
opts.Period = 30
|
||||
}
|
||||
counter := uint64(math.Floor(float64(t.Unix()) / float64(opts.Period)))
|
||||
passcode, err = hotp.GenerateCodeCustom(secret, counter, hotp.ValidateOpts{
|
||||
Digits: opts.Digits,
|
||||
Algorithm: opts.Algorithm,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return passcode, nil
|
||||
}
|
||||
|
||||
// ValidateCustom validates a TOTP given a user specified time and custom options.
|
||||
// Most users should use Validate() to provide an interpolatable TOTP experience.
|
||||
func ValidateCustom(passcode string, secret string, t time.Time, opts ValidateOpts) (bool, error) {
|
||||
if opts.Period == 0 {
|
||||
opts.Period = 30
|
||||
}
|
||||
|
||||
counters := []uint64{}
|
||||
counter := int64(math.Floor(float64(t.Unix()) / float64(opts.Period)))
|
||||
|
||||
counters = append(counters, uint64(counter))
|
||||
for i := 1; i <= int(opts.Skew); i++ {
|
||||
counters = append(counters, uint64(counter+int64(i)))
|
||||
counters = append(counters, uint64(counter-int64(i)))
|
||||
}
|
||||
|
||||
for _, counter := range counters {
|
||||
rv, err := hotp.ValidateCustom(passcode, counter, secret, hotp.ValidateOpts{
|
||||
Digits: opts.Digits,
|
||||
Algorithm: opts.Algorithm,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if rv == true {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// GenerateOpts provides options for Generate(). The default values
|
||||
// are compatible with Google-Authenticator.
|
||||
type GenerateOpts struct {
|
||||
// Name of the issuing Organization/Company.
|
||||
Issuer string
|
||||
// Name of the User's Account (eg, email address)
|
||||
AccountName string
|
||||
// Number of seconds a TOTP hash is valid for. Defaults to 30 seconds.
|
||||
Period uint
|
||||
// Size in size of the generated Secret. Defaults to 10 bytes.
|
||||
SecretSize uint
|
||||
// Digits to request. Defaults to 6.
|
||||
Digits otp.Digits
|
||||
// Algorithm to use for HMAC. Defaults to SHA1.
|
||||
Algorithm otp.Algorithm
|
||||
}
|
||||
|
||||
// Generate a new TOTP Key.
|
||||
func Generate(opts GenerateOpts) (*otp.Key, error) {
|
||||
// url encode the Issuer/AccountName
|
||||
if opts.Issuer == "" {
|
||||
return nil, otp.ErrGenerateMissingIssuer
|
||||
}
|
||||
|
||||
if opts.AccountName == "" {
|
||||
return nil, otp.ErrGenerateMissingAccountName
|
||||
}
|
||||
|
||||
if opts.Period == 0 {
|
||||
opts.Period = 30
|
||||
}
|
||||
|
||||
if opts.SecretSize == 0 {
|
||||
opts.SecretSize = 10
|
||||
}
|
||||
|
||||
if opts.Digits == 0 {
|
||||
opts.Digits = otp.DigitsSix
|
||||
}
|
||||
|
||||
// otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example
|
||||
|
||||
v := url.Values{}
|
||||
secret := make([]byte, opts.SecretSize)
|
||||
_, err := rand.Read(secret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v.Set("secret", strings.TrimRight(base32.StdEncoding.EncodeToString(secret), "="))
|
||||
v.Set("issuer", opts.Issuer)
|
||||
v.Set("period", strconv.FormatUint(uint64(opts.Period), 10))
|
||||
v.Set("algorithm", opts.Algorithm.String())
|
||||
v.Set("digits", opts.Digits.String())
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "otpauth",
|
||||
Host: "totp",
|
||||
Path: "/" + opts.Issuer + ":" + opts.AccountName,
|
||||
RawQuery: v.Encode(),
|
||||
}
|
||||
|
||||
return otp.NewKeyFromURL(u.String())
|
||||
}
|
146
vendor/github.com/pquerna/otp/totp/totp_test.go
generated
vendored
Normal file
146
vendor/github.com/pquerna/otp/totp/totp_test.go
generated
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* Copyright 2014 Paul Querna
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package totp
|
||||
|
||||
import (
|
||||
"github.com/pquerna/otp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"encoding/base32"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type tc struct {
|
||||
TS int64
|
||||
TOTP string
|
||||
Mode otp.Algorithm
|
||||
Secret string
|
||||
}
|
||||
|
||||
var (
|
||||
secSha1 = base32.StdEncoding.EncodeToString([]byte("12345678901234567890"))
|
||||
secSha256 = base32.StdEncoding.EncodeToString([]byte("12345678901234567890123456789012"))
|
||||
secSha512 = base32.StdEncoding.EncodeToString([]byte("1234567890123456789012345678901234567890123456789012345678901234"))
|
||||
|
||||
rfcMatrixTCs = []tc{
|
||||
tc{59, "94287082", otp.AlgorithmSHA1, secSha1},
|
||||
tc{59, "46119246", otp.AlgorithmSHA256, secSha256},
|
||||
tc{59, "90693936", otp.AlgorithmSHA512, secSha512},
|
||||
tc{1111111109, "07081804", otp.AlgorithmSHA1, secSha1},
|
||||
tc{1111111109, "68084774", otp.AlgorithmSHA256, secSha256},
|
||||
tc{1111111109, "25091201", otp.AlgorithmSHA512, secSha512},
|
||||
tc{1111111111, "14050471", otp.AlgorithmSHA1, secSha1},
|
||||
tc{1111111111, "67062674", otp.AlgorithmSHA256, secSha256},
|
||||
tc{1111111111, "99943326", otp.AlgorithmSHA512, secSha512},
|
||||
tc{1234567890, "89005924", otp.AlgorithmSHA1, secSha1},
|
||||
tc{1234567890, "91819424", otp.AlgorithmSHA256, secSha256},
|
||||
tc{1234567890, "93441116", otp.AlgorithmSHA512, secSha512},
|
||||
tc{2000000000, "69279037", otp.AlgorithmSHA1, secSha1},
|
||||
tc{2000000000, "90698825", otp.AlgorithmSHA256, secSha256},
|
||||
tc{2000000000, "38618901", otp.AlgorithmSHA512, secSha512},
|
||||
tc{20000000000, "65353130", otp.AlgorithmSHA1, secSha1},
|
||||
tc{20000000000, "77737706", otp.AlgorithmSHA256, secSha256},
|
||||
tc{20000000000, "47863826", otp.AlgorithmSHA512, secSha512},
|
||||
}
|
||||
)
|
||||
|
||||
//
|
||||
// Test vectors from http://tools.ietf.org/html/rfc6238#appendix-B
|
||||
// NOTE -- the test vectors are documented as having the SAME
|
||||
// secret -- this is WRONG -- they have a variable secret
|
||||
// depending upon the hmac algorithm:
|
||||
// http://www.rfc-editor.org/errata_search.php?rfc=6238
|
||||
// this only took a few hours of head/desk interaction to figure out.
|
||||
//
|
||||
func TestValidateRFCMatrix(t *testing.T) {
|
||||
for _, tx := range rfcMatrixTCs {
|
||||
valid, err := ValidateCustom(tx.TOTP, tx.Secret, time.Unix(tx.TS, 0).UTC(),
|
||||
ValidateOpts{
|
||||
Digits: otp.DigitsEight,
|
||||
Algorithm: tx.Mode,
|
||||
})
|
||||
require.NoError(t, err,
|
||||
"unexpected error totp=%s mode=%v ts=%v", tx.TOTP, tx.Mode, tx.TS)
|
||||
require.True(t, valid,
|
||||
"unexpected totp failure totp=%s mode=%v ts=%v", tx.TOTP, tx.Mode, tx.TS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateRFCTCs(t *testing.T) {
|
||||
for _, tx := range rfcMatrixTCs {
|
||||
passcode, err := GenerateCodeCustom(tx.Secret, time.Unix(tx.TS, 0).UTC(),
|
||||
ValidateOpts{
|
||||
Digits: otp.DigitsEight,
|
||||
Algorithm: tx.Mode,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, tx.TOTP, passcode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateSkew(t *testing.T) {
|
||||
secSha1 := base32.StdEncoding.EncodeToString([]byte("12345678901234567890"))
|
||||
|
||||
tests := []tc{
|
||||
tc{29, "94287082", otp.AlgorithmSHA1, secSha1},
|
||||
tc{59, "94287082", otp.AlgorithmSHA1, secSha1},
|
||||
tc{61, "94287082", otp.AlgorithmSHA1, secSha1},
|
||||
}
|
||||
|
||||
for _, tx := range tests {
|
||||
valid, err := ValidateCustom(tx.TOTP, tx.Secret, time.Unix(tx.TS, 0).UTC(),
|
||||
ValidateOpts{
|
||||
Digits: otp.DigitsEight,
|
||||
Algorithm: tx.Mode,
|
||||
Skew: 1,
|
||||
})
|
||||
require.NoError(t, err,
|
||||
"unexpected error totp=%s mode=%v ts=%v", tx.TOTP, tx.Mode, tx.TS)
|
||||
require.True(t, valid,
|
||||
"unexpected totp failure totp=%s mode=%v ts=%v", tx.TOTP, tx.Mode, tx.TS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
k, err := Generate(GenerateOpts{
|
||||
Issuer: "SnakeOil",
|
||||
AccountName: "alice@example.com",
|
||||
})
|
||||
require.NoError(t, err, "generate basic TOTP")
|
||||
require.Equal(t, "SnakeOil", k.Issuer(), "Extracting Issuer")
|
||||
require.Equal(t, "alice@example.com", k.AccountName(), "Extracting Account Name")
|
||||
require.Equal(t, 16, len(k.Secret()), "Secret is 16 bytes long as base32.")
|
||||
|
||||
k, err = Generate(GenerateOpts{
|
||||
Issuer: "SnakeOil",
|
||||
AccountName: "alice@example.com",
|
||||
SecretSize: 20,
|
||||
})
|
||||
require.NoError(t, err, "generate larger TOTP")
|
||||
require.Equal(t, 32, len(k.Secret()), "Secret is 32 bytes long as base32.")
|
||||
|
||||
k, err = Generate(GenerateOpts{
|
||||
Issuer: "SnakeOil",
|
||||
AccountName: "alice@example.com",
|
||||
SecretSize: 13, // anything that is not divisable by 5, really
|
||||
})
|
||||
require.NoError(t, err, "Secret size is valid when length not divisable by 5.")
|
||||
require.NotContains(t, k.Secret(), "=", "Secret has no escaped characters.")
|
||||
}
|
Reference in New Issue
Block a user