Commit f8a40fff authored by Michael Meskes's avatar Michael Meskes

New upstream version 1.0.1

parent 9187b71e
sudo: false
language: go language: go
go: go:
- 1.x - 1.8
- 1.6 - 1.9
- 1.7.x - "1.10"
- master - master
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "rsc.io/qr"
packages = [
".",
"coding",
"gf256"
]
revision = "48b2ede4844e13f1a2b7ce4d2529c9af7e359fc5"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "b705d306da5a78e76b7ff289744770eef56328a4f0d1c615c1a233d056283651"
solver-name = "gps-cdcl"
solver-version = 1
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
branch = "master"
name = "rsc.io/qr"
[prune]
go-tests = true
unused-packages = true
## `twofactor`
[![GoDoc](https://godoc.org/github.com/gokyle/twofactor?status.svg)](https://godoc.org/github.com/gokyle/twofactor)
[![Build Status](https://travis-ci.org/gokyle/twofactor.svg?branch=master)](https://travis-ci.org/gokyle/twofactor)
### Author
`twofactor` was written by Kyle Isom <kyle@tyrfingr.is>.
### License
```
Copyright (c) 2017 Kyle Isom <kyle@imap.cc>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
...@@ -88,9 +88,10 @@ func hotpFromURL(u *url.URL) (*HOTP, string, error) { ...@@ -88,9 +88,10 @@ func hotpFromURL(u *url.URL) (*HOTP, string, error) {
} }
} }
key, err := base32.StdEncoding.DecodeString(secret) key, err := base32.StdEncoding.DecodeString(Pad(secret))
if err != nil { if err != nil {
return nil, "", err // assume secret isn't base32 encoded
key = []byte(secret)
} }
otp := NewHOTP(key, counter, digits) otp := NewHOTP(key, counter, digits)
return otp, label, nil return otp, label, nil
......
...@@ -69,6 +69,46 @@ func TestURL(t *testing.T) { ...@@ -69,6 +69,46 @@ func TestURL(t *testing.T) {
} }
} }
// This test makes sure we can generate codes for padded and non-padded
// entries
func TestPaddedURL(t *testing.T) {
var urlList = []string{
"otpauth://hotp/?secret=ME",
"otpauth://hotp/?secret=MEFR",
"otpauth://hotp/?secret=MFRGG",
"otpauth://hotp/?secret=MFRGGZA",
"otpauth://hotp/?secret=a6mryljlbufszudtjdt42nh5by=======",
"otpauth://hotp/?secret=a6mryljlbufszudtjdt42nh5by",
"otpauth://hotp/?secret=a6mryljlbufszudtjdt42nh5by%3D%3D%3D%3D%3D%3D%3D",
}
var codeList = []string{
"413198",
"770938",
"670717",
"402378",
"069864",
"069864",
"069864",
}
for i := range urlList {
if o, id, err := FromURL(urlList[i]); err != nil {
fmt.Println("hotp: URL should have parsed successfully")
fmt.Printf("\turl was: %s\n", urlList[i])
t.FailNow()
fmt.Printf("\t%s, %s\n", o.OTP(), id)
} else {
code2 := o.OTP()
if code2 != codeList[i] {
fmt.Printf("hotp: mismatched OTPs\n")
fmt.Printf("\texpected: %s\n", codeList[i])
fmt.Printf("\t actual: %s\n", code2)
t.FailNow()
}
}
}
}
// This test attempts a variety of invalid urls against the parser // This test attempts a variety of invalid urls against the parser
// to ensure they fail. // to ensure they fail.
func TestBadURL(t *testing.T) { func TestBadURL(t *testing.T) {
...@@ -79,10 +119,7 @@ func TestBadURL(t *testing.T) { ...@@ -79,10 +119,7 @@ func TestBadURL(t *testing.T) {
"foo", "foo",
"otpauth:/foo/bar/baz", "otpauth:/foo/bar/baz",
"://", "://",
"otpauth://hotp/secret=bar",
"otpauth://hotp/?secret=QUJDRA&algorithm=SHA256",
"otpauth://hotp/?digits=", "otpauth://hotp/?digits=",
"otpauth://hotp/?secret=123",
"otpauth://hotp/?secret=MFRGGZDF&digits=ABCD", "otpauth://hotp/?secret=MFRGGZDF&digits=ABCD",
"otpauth://hotp/?secret=MFRGGZDF&counter=ABCD", "otpauth://hotp/?secret=MFRGGZDF&counter=ABCD",
} }
......
...@@ -123,12 +123,11 @@ func totpFromURL(u *url.URL) (*TOTP, string, error) { ...@@ -123,12 +123,11 @@ func totpFromURL(u *url.URL) (*TOTP, string, error) {
var algo = crypto.SHA1 var algo = crypto.SHA1
if algorithm := v.Get("algorithm"); algorithm != "" { if algorithm := v.Get("algorithm"); algorithm != "" {
switch { if strings.EqualFold(algorithm, "SHA256") {
case algorithm == "SHA256":
algo = crypto.SHA256 algo = crypto.SHA256
case algorithm == "SHA512": } else if strings.EqualFold(algorithm, "SHA512") {
algo = crypto.SHA512 algo = crypto.SHA512
case algorithm != "SHA1": } else if !strings.EqualFold(algorithm, "SHA1") {
return nil, "", ErrInvalidAlgo return nil, "", ErrInvalidAlgo
} }
} }
...@@ -151,9 +150,10 @@ func totpFromURL(u *url.URL) (*TOTP, string, error) { ...@@ -151,9 +150,10 @@ func totpFromURL(u *url.URL) (*TOTP, string, error) {
} }
} }
key, err := base32.StdEncoding.DecodeString(secret) key, err := base32.StdEncoding.DecodeString(Pad(secret))
if err != nil { if err != nil {
return nil, "", err // assume secret isn't base32 encoded
key = []byte(secret)
} }
otp := NewTOTP(key, 0, period, digits, algo) otp := NewTOTP(key, 0, period, digits, algo)
return otp, label, nil return otp, label, nil
......
package twofactor
import (
"strings"
)
// Pad calculates the number of '='s to add to our encoded string
// to make base32.StdEncoding.DecodeString happy
func Pad(s string) string {
if !strings.HasSuffix(s, "=") && len(s)%8 != 0 {
for len(s)%8 != 0 {
s += "="
}
}
return s
}
package twofactor
import (
"encoding/base32"
"fmt"
"math/rand"
"strings"
"testing"
)
const letters = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randString() string {
b := make([]byte, rand.Intn(len(letters)))
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return base32.StdEncoding.EncodeToString(b)
}
func TestPadding(t *testing.T) {
for i := 0; i < 300; i++ {
b := randString()
origEncoding := string(b)
modEncoding := strings.Replace(string(b), "=", "", -1)
str, err := base32.StdEncoding.DecodeString(origEncoding)
if err != nil {
fmt.Println("Can't decode: ", string(b))
t.FailNow()
}
paddedEncoding := Pad(modEncoding)
if origEncoding != paddedEncoding {
fmt.Println("Padding failed:")
fmt.Printf("Expected: '%s'", origEncoding)
fmt.Printf("Got: '%s'", paddedEncoding)
t.FailNow()
} else {
mstr, err := base32.StdEncoding.DecodeString(paddedEncoding)
if err != nil {
fmt.Println("Can't decode: ", paddedEncoding)
t.FailNow()
}
if string(mstr) != string(str) {
fmt.Println("Re-padding failed:")
fmt.Printf("Expected: '%s'", str)
fmt.Printf("Got: '%s'", mstr)
t.FailNow()
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment