From 63f525970fc5b91dcae26895388fb3700199c14e Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 17 Apr 2022 00:18:48 +0800 Subject: [PATCH] auth: store otp key url instead of just secret --- pkg/bbgo/environment.go | 33 +++++++++++++++++++++++---------- pkg/interact/auth.go | 4 ++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 187bc5020..95397e566 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -737,9 +737,9 @@ func getAuthStoreID() string { func (environ *Environment) setupInteraction(persistence service.PersistenceService) error { var otpQRCodeImagePath = fmt.Sprintf("otp.png") var key *otp.Key - var keySecret string + var keyURL string var authStore = environ.getAuthStore(persistence) - if err := authStore.Load(&keySecret); err != nil { + if err := authStore.Load(&keyURL); err != nil { log.Warnf("telegram session not found, generating new one-time password key for new telegram session...") newKey, err := setupNewOTPKey(otpQRCodeImagePath) @@ -748,21 +748,34 @@ func (environ *Environment) setupInteraction(persistence service.PersistenceServ } key = newKey - keySecret = key.Secret() - if err := authStore.Save(keySecret); err != nil { + keyURL = key.URL() + if err := authStore.Save(keyURL); err != nil { return err } printOtpAuthGuide(otpQRCodeImagePath) - } else if keySecret != "" { - key, err = otp.NewKeyFromURL(keySecret) + } else if keyURL != "" { + key, err = otp.NewKeyFromURL(keyURL) if err != nil { - return err - } + log.WithError(err).Errorf("can not load otp key from url: %s, generating new otp key", keyURL) - log.Infof("otp key loaded: %s", util.MaskKey(key.Secret())) - printOtpAuthGuide(otpQRCodeImagePath) + newKey, err := setupNewOTPKey(otpQRCodeImagePath) + if err != nil { + return errors.Wrapf(err, "failed to setup totp (time-based one time password) key") + } + + key = newKey + keyURL = key.URL() + if err := authStore.Save(keyURL); err != nil { + return err + } + + printOtpAuthGuide(otpQRCodeImagePath) + } else { + log.Infof("otp key loaded: %s", util.MaskKey(key.Secret())) + printOtpAuthGuide(otpQRCodeImagePath) + } } authStrict := false diff --git a/pkg/interact/auth.go b/pkg/interact/auth.go index 204937cb0..32162ea14 100644 --- a/pkg/interact/auth.go +++ b/pkg/interact/auth.go @@ -92,7 +92,7 @@ func (it *AuthInteract) Commands(interact *Interact) { reply.Message("Enter your one-time password") default: - log.Warn("unexpected auth mode: %s", it.Mode) + log.Warnf("unexpected auth mode: %s", it.Mode) } return nil }).NamedNext(StateAuthenticated, func(code string, reply Reply, session Session) error { @@ -116,7 +116,7 @@ func (it *AuthInteract) Commands(interact *Interact) { reply.Message("Incorrect one-time pass code") default: - log.Warn("unexpected auth mode: %s", it.Mode) + log.Warnf("unexpected auth mode: %s", it.Mode) } return ErrAuthenticationFailed