59 lines
999 B
Go
59 lines
999 B
Go
|
package otp_test
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"dev.justinjudd.org/justin/otp"
|
||
|
)
|
||
|
|
||
|
// Just an example for storing OTP keys on the client
|
||
|
var keys map[Key]string
|
||
|
|
||
|
// Key is used as keys for the otp key storing map
|
||
|
type Key struct {
|
||
|
Issuer, Label string
|
||
|
}
|
||
|
|
||
|
func GetCode(issuer, username string) (string, error) {
|
||
|
|
||
|
mapKey := Key{issuer, username}
|
||
|
|
||
|
// Get the stored Key URL
|
||
|
keyURL, ok := keys[mapKey]
|
||
|
if !ok {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
// Build the key from the URL
|
||
|
key, err := otp.FromURL(keyURL)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// Verify Issuer and Label are correct
|
||
|
if key.Issuer() != issuer || key.Label() != username {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
code := key.OTP()
|
||
|
|
||
|
// If using HOTP, than need to save the state
|
||
|
keyURL = key.URL()
|
||
|
keys[mapKey] = keyURL
|
||
|
|
||
|
return code, nil
|
||
|
}
|
||
|
|
||
|
func Example_hOTPClient() {
|
||
|
issuer := "example.com"
|
||
|
username := "username"
|
||
|
|
||
|
code, err := GetCode(issuer, username)
|
||
|
if err != nil {
|
||
|
// Handle error
|
||
|
}
|
||
|
|
||
|
// Present code to user, or send code to server
|
||
|
fmt.Println(code)
|
||
|
}
|