mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
interact: improve strict mode authentication
This commit is contained in:
parent
72a925f659
commit
62e5706657
|
@ -135,6 +135,7 @@ func main() {
|
|||
})
|
||||
|
||||
globalInteraction.AddCustomInteraction(&interact.AuthInteract{
|
||||
Strict: true,
|
||||
Mode: interact.AuthModeToken,
|
||||
Token: "123",
|
||||
})
|
||||
|
|
|
@ -2,9 +2,12 @@ package interact
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp"
|
||||
"github.com/pquerna/otp/totp"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type AuthMode string
|
||||
|
@ -32,14 +35,39 @@ type AuthInteract struct {
|
|||
|
||||
func (it *AuthInteract) Commands(interact *Interact) {
|
||||
if it.Strict {
|
||||
// generate a one-time-use otp
|
||||
if it.OneTimePasswordKey == nil {
|
||||
opts := totp.GenerateOpts{
|
||||
Issuer: "interact",
|
||||
AccountName: os.Getenv("USER"),
|
||||
Period: 30,
|
||||
}
|
||||
log.Infof("[interact] one-time password key is not configured, generating one with %+v", opts)
|
||||
key, err := totp.Generate(opts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
it.OneTimePasswordKey = key
|
||||
}
|
||||
interact.Command("/auth", func(reply Reply) error {
|
||||
reply.Message("Enter your authentication token")
|
||||
return nil
|
||||
}).Next(func(token string, reply Reply) error {
|
||||
if token == it.Token {
|
||||
reply.Message("Token passed, please enter your one-time password")
|
||||
|
||||
code, err := totp.GenerateCode(it.OneTimePasswordKey.Secret(), time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("[interact] ======================================")
|
||||
log.Infof("[interact] your one-time password code: %s", code)
|
||||
log.Infof("[interact] ======================================")
|
||||
return nil
|
||||
}
|
||||
|
||||
return ErrAuthenticationFailed
|
||||
}).NamedNext(StateAuthenticated, func(code string, reply Reply, authorizer Authorizer) error {
|
||||
if totp.Validate(code, it.OneTimePasswordKey.Secret()) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user