Adding language hint for markdown examples.

This commit is contained in:
Justin Judd 2016-01-11 10:03:40 +09:00
parent 395d932f93
commit 6ad513fa31
1 changed files with 79 additions and 78 deletions

157
README.md
View File

@ -7,102 +7,103 @@ Go library for generating and using One Time Passwords. Supports both HOTP ([RFC
## Examples ## Examples
### OTP Server ### OTP Server
```go
import (
"dev.justinjudd.org/justin/otp"
)
import ( var Issuer = "example.com"
"dev.justinjudd.org/justin/otp"
)
var Issuer = "example.com" func CreateKeyForUser(user string) {
func CreateKeyForUser(user string) { opts := otp.NewHOTPKeyOptions()
opts.Issuer = Issuer
opts.Label = user
key := otp.NewHOTPKey(opts)
opts := otp.NewHOTPKeyOptions() keyURL := key.URL()
opts.Issuer = Issuer Store(user, keyURL)
opts.Label = user
key := otp.NewHOTPKey(opts)
keyURL := key.URL() // Provide the URL to the customer so they can include it in their 2FA client.
Store(user, keyURL) // Can email URL, or present QR code encoding of the URL
}
// Provide the URL to the customer so they can include it in their 2FA client. // Store this string variable in your database
// Can email URL, or present QR code encoding of the URL func Store(user, url string) {
}
// Retrieve the url string variable from your database
func GetURL(user string) string {
return ""
}
func CheckUsersCode(user string, code string) (bool, error) {
keyURL := GetURL(user)
key, err := otp.FromURL(keyURL)
if err != nil {
return false, err
}
// Ensure you are using the correct key
if key.Label() != user {
return false, nil
}
success := key.Verify(code)
// Counter has been updated, update this info in the database
// Don't need this step for TOTP keys as the counter is time-based
keyURL = key.URL()
return success, nil
} }
```
// Store this string variable in your database
func Store(user, url string) {
}
// Retrieve the url string variable from your database
func GetURL(user string) string {
return ""
}
func CheckUsersCode(user string, code string) (bool, error) {
keyURL := GetURL(user)
key, err := otp.FromURL(keyURL)
if err != nil {
return false, err
}
// Ensure you are using the correct key
if key.Label() != user {
return false, nil
}
success := key.Verify(code)
// Counter has been updated, update this info in the database
// Don't need this step for TOTP keys as the counter is time-based
keyURL = key.URL()
return success, nil
}
### OTP Client ### OTP Client
```go
import (
"dev.justinjudd.org/justin/otp"
)
import ( // Just an example for storing OTP keys on the client
"dev.justinjudd.org/justin/otp" var keys map[Key]string
)
// 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 // Key is used as keys for the otp key storing map
type Key struct { type Key struct {
Issuer, Label string Issuer, Label string
} }
func GetCode(issuer, username string) (string, error) { func GetCode(issuer, username string) (string, error) {
mapKey := Key{issuer, username} mapKey := Key{issuer, username}
// Get the stored Key URL // Get the stored Key URL
keyURL, ok := keys[mapKey] keyURL, ok := keys[mapKey]
if !ok { if !ok {
return "", nil return "", nil
} }
// Build the key from the URL // Build the key from the URL
key, err := otp.FromURL(keyURL) key, err := otp.FromURL(keyURL)
if err != nil { if err != nil {
return "", err return "", err
} }
// Verify Issuer and Label are correct // Verify Issuer and Label are correct
if key.Issuer() != issuer || key.Label() != username { if key.Issuer() != issuer || key.Label() != username {
return "", nil return "", nil
} }
code := key.OTP() code := key.OTP()
// If using HOTP, than need to save the state // If using HOTP, than need to save the state
keyURL = key.URL() keyURL = key.URL()
keys[mapKey]= keyURL keys[mapKey]= keyURL
return code, nil return code, nil
} }
```