Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

change method proto #26

Merged
merged 2 commits into from
Dec 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 13 additions & 38 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestInvalidMethod(t *testing.T) {
key := Key{
Method: "crypto!",
}
v, _ := key.IsValid()
if v == true {
err := key.Validate()
if err == nil {
t.Fail()
}
}
Expand All @@ -33,19 +33,8 @@ func TestMissingLabel(t *testing.T) {
key := Key{
Method: "totp",
}
v, _ := key.IsValid()
if v == true {
t.Fail()
}
}

func TestInvalidLabel(t *testing.T) {
key := Key{
Method: "totp",
Label: "t/w",
}
v, _ := key.IsValid()
if v == true {
err := key.Validate()
if err == nil {
t.Fail()
}
}
Expand All @@ -55,8 +44,8 @@ func TestMissingSecret(t *testing.T) {
Method: "totp",
Label: "t@w",
}
v, _ := key.IsValid()
if v == true {
err := key.Validate()
if err == nil {
t.Fail()
}
}
Expand All @@ -67,21 +56,8 @@ func TestBadSecret(t *testing.T) {
Label: "t@w",
Secret: "abc123",
}
v, _ := key.IsValid()
if v == true {
t.Fail()
}
}

func TestBadIssuer(t *testing.T) {
key := Key{
Method: "totp",
Label: "t@w",
Secret: "MFRGGZDFMZTWQ2LK",
Issuer: "issu/er",
}
v, _ := key.IsValid()
if v == true {
err := key.Validate()
if err == nil {
t.Fail()
}
}
Expand All @@ -94,8 +70,8 @@ func TestBadAlgo(t *testing.T) {
Issuer: "issuer",
Algo: md4.New,
}
v, _ := key.IsValid()
if v == true {
err := key.Validate()
if err == nil {
t.Fail()
}
}
Expand All @@ -109,8 +85,8 @@ func TestBadDigits(t *testing.T) {
Algo: sha1.New,
Digits: 99,
}
v, _ := key.IsValid()
if v == true {
err := key.Validate()
if err == nil {
t.Fail()
}
}
Expand All @@ -125,8 +101,7 @@ func TestBadPeriod(t *testing.T) {
Digits: 6,
Period: -42,
}
v, _ := key.IsValid()
if v == true {
if err := key.Validate(); err == nil {
t.Fail()
}
}
Expand Down
4 changes: 2 additions & 2 deletions keymakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func newKey(method, label, secret, issuer string, algo Hash, digits, period, cou
Counter: counter,
}

if v, err := k.IsValid(); v != true {
if err := k.Validate(); err != nil {
return &k, err
}

Expand All @@ -41,7 +41,7 @@ func NewKey(uri string) (*Key, error) {
return &k, err
}

if _, err := k.IsValid(); err != nil {
if err := k.Validate(); err != nil {
return &k, err
}

Expand Down
34 changes: 8 additions & 26 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package otp

import (
"encoding/base32"
"strings"
)

func (k Key) hasValidMethod() error {
Expand All @@ -16,11 +15,6 @@ func (k Key) hasValidLabel() error {
if len(k.Label) == 0 {
return KeyError{"Label", "Missing value"}
}

if strings.ContainsRune(k.Label, '/') {
return KeyError{"Label", "Contains '/'"}
}

return nil
}

Expand All @@ -36,13 +30,6 @@ func (k Key) hasValidSecret() error {
return nil
}

func (k Key) hasValidIssuer() error {
if strings.ContainsRune(k.Issuer, '/') {
return KeyError{"Issuer", "Contains '/'"}
}
return nil
}

func (k Key) hasValidAlgo() error {
if !hashInSlice(k.Algo, HASHES) {
return KeyError{"Algo", "Invalid hashing algorithm"}
Expand All @@ -64,42 +51,37 @@ func (k Key) hasValidPeriod() error {
return nil
}

func (k Key) IsValid() (bool, error) {
func (k Key) Validate() error {

// check method
if err := k.hasValidMethod(); err != nil {
return false, err
return err
}

//check label
if err := k.hasValidLabel(); err != nil {
return false, err
return err
}

// check secret
if err := k.hasValidSecret(); err != nil {
return false, err
}

// check issuer
if err := k.hasValidIssuer(); err != nil {
return false, err
return err
}

// check algo
if err := k.hasValidAlgo(); err != nil {
return false, err
return err
}

// check digits
if err := k.hasValidDigits(); err != nil {
return false, err
return err
}

// check period
if err := k.hasValidPeriod(); err != nil {
return false, err
return err
}

return true, nil
return nil
}