Skip to content

Commit

Permalink
#3 :: Add json tags to HOTP and TOTP.
Browse files Browse the repository at this point in the history
  • Loading branch information
jltorresm committed Sep 3, 2020
1 parent d0e35b2 commit 7d70e92
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
10 changes: 5 additions & 5 deletions hotp.go
Expand Up @@ -18,11 +18,11 @@ const (

// The HOTP type used to generate HMAC-Based One-Time Passwords.
type HOTP struct {
Key string // Secret
Counter uint64
Leeway uint64
Algorithm config.HmacAlgorithm
Length config.Length
Key string `json:"key"` // Secret base32 encoded string
Counter uint64 `json:"counter"` // Current value to calculate around
Leeway uint64 `json:"leeway"` // Acceptable steps for sync error
Algorithm config.HmacAlgorithm `json:"algorithm"` // Hash algorithm to use in the calculation
Length config.Length `json:"length"` // Length of the resulting code
}

// Generate a HMAC-Based One-Time Password.
Expand Down
23 changes: 23 additions & 0 deletions hotp_test.go
@@ -1,6 +1,7 @@
package otpgo

import (
"encoding/json"
"testing"

"github.com/jltorresm/otpgo/config"
Expand Down Expand Up @@ -267,3 +268,25 @@ func TestHOTP_KeyUri(t *testing.T) {
t.Errorf("unexpected key URI\nexpected: %s\n actual: %s", expectedUri, uri.String())
}
}

func TestHOTPJson(t *testing.T) {
h := HOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ",
Counter: 9338,
Leeway: 1,
Algorithm: config.HmacSHA256,
Length: config.Length7,
}

expectedJson := `{"key":"73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ","counter":9338,"leeway":1,"algorithm":"SHA256","length":7}`

j, err := json.Marshal(h)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}

if expectedJson != string(j) {
t.Errorf("unexpected json:\nexpected: %s\n actual: %s", expectedJson, j)
}
}
10 changes: 5 additions & 5 deletions totp.go
Expand Up @@ -22,11 +22,11 @@ const (

// The TOTP type used to generate Time-Based One-Time Passwords.
type TOTP struct {
Key string // Secret base32 encoded string
Period int // Number of seconds the TOTP is valid
Delay int // Acceptable steps for network delay
Algorithm config.HmacAlgorithm
Length config.Length
Key string `json:"key"` // Secret base32 encoded string
Period int `json:"period"` // Number of seconds the TOTP is valid
Delay int `json:"delay"` // Acceptable steps for network delay
Algorithm config.HmacAlgorithm `json:"algorithm"` // Hash algorithm to use in the calculation
Length config.Length `json:"length"` // Length of the resulting code
}

// Generate a Time-Based One-Time Password.
Expand Down
23 changes: 23 additions & 0 deletions totp_test.go
@@ -1,6 +1,7 @@
package otpgo

import (
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -258,6 +259,28 @@ func TestTOTP_KeyUri(t *testing.T) {
}
}

func TestTOTPJson(t *testing.T) {
h := TOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ",
Period: 30,
Delay: 1,
Algorithm: config.HmacSHA256,
Length: config.Length7,
}

expectedJson := `{"key":"73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ","period":30,"delay":1,"algorithm":"SHA256","length":7}`

j, err := json.Marshal(h)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}

if expectedJson != string(j) {
t.Errorf("unexpected json:\nexpected: %s\n actual: %s", expectedJson, j)
}
}

func getExpectedTOTP(key string, counter uint64, length config.Length, algorithm config.HmacAlgorithm) (string, error) {
expectedOTP, err := generateOTP(key, counter, length, algorithm)
if err != nil {
Expand Down

0 comments on commit 7d70e92

Please sign in to comment.