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

Commit

Permalink
Merge pull request #23 from tristanwietsma/newkey
Browse files Browse the repository at this point in the history
newkey
  • Loading branch information
tristanwietsma committed Dec 1, 2014
2 parents eef0d13 + 5315369 commit 2d8552e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions keymakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ func NewHOTPKey(label, secret, issuer string, algo Hash, digits, counter int) (*
k, err := newKey("hotp", label, secret, issuer, algo, digits, 0, counter)
return k, err
}

// Returns a new Key given a OTPAUTH URI.
func NewKey(uri string) (*Key, error) {
k := Key{}
if err := k.FromURI(uri); err != nil {
return &k, err
}

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

return &k, nil
}
18 changes: 18 additions & 0 deletions keymakers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@ func TestNewHOTPKey(t *testing.T) {
t.Error("failed to build new hotp key")
}
}

func TestNewKeyFromURI(t *testing.T) {
uri := "otpauth://totp/label?secret=MFRGGZDFMZTWQ2LK&issuer=theIssuer&algo=SHA512"
if _, err := NewKey(uri); err != nil {
t.Errorf("Constructor failed:\n%v", err)
}

uri = "blahblah"
if _, err := NewKey(uri); err == nil {
t.Error("Should have failed...")
}

uri = "otpauth://totp/label?secret=MFRGGZDFMZTWQ2LK&issuer=theIssuer&algo=SHA512&period=0"
if _, err := NewKey(uri); err == nil {
t.Error("Should have failed...")
}

}

0 comments on commit 2d8552e

Please sign in to comment.