mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
interact: pull out authentication interaction
This commit is contained in:
parent
91c831140c
commit
14eea34394
|
@ -128,7 +128,12 @@ func main() {
|
||||||
|
|
||||||
globalInteraction := interact.New()
|
globalInteraction := interact.New()
|
||||||
globalInteraction.SetMessenger(&interact.Telegram{
|
globalInteraction.SetMessenger(&interact.Telegram{
|
||||||
Bot: b,
|
Bot: b,
|
||||||
|
})
|
||||||
|
|
||||||
|
globalInteraction.AddCustomInteraction(&interact.AuthInteract{
|
||||||
|
Mode: interact.AuthModeToken,
|
||||||
|
Token: "123",
|
||||||
})
|
})
|
||||||
|
|
||||||
globalInteraction.AddCustomInteraction(&PositionInteraction{})
|
globalInteraction.AddCustomInteraction(&PositionInteraction{})
|
||||||
|
|
47
pkg/interact/auth.go
Normal file
47
pkg/interact/auth.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package interact
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/pquerna/otp"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AuthMode string
|
||||||
|
|
||||||
|
const (
|
||||||
|
AuthModeOTP AuthMode = "OTP"
|
||||||
|
AuthModeToken AuthMode = "TOKEN"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrAuthenticationFailed = errors.New("authentication failed")
|
||||||
|
|
||||||
|
type AuthInteract struct {
|
||||||
|
Mode AuthMode `json:"authMode"`
|
||||||
|
|
||||||
|
Token string `json:"authToken,omitempty"`
|
||||||
|
|
||||||
|
OneTimePasswordKey *otp.Key `json:"otpKey,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *AuthInteract) Commands(interact *Interact) {
|
||||||
|
interact.Command("/auth", func(reply Reply) error {
|
||||||
|
reply.Message("Enter your authentication code")
|
||||||
|
return nil
|
||||||
|
}).NamedNext(StateAuthenticated, func(reply Reply, code string) error {
|
||||||
|
switch i.Mode {
|
||||||
|
case AuthModeToken:
|
||||||
|
if code == i.Token {
|
||||||
|
reply.Message("Great! You're authenticated!")
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
reply.Message("Incorrect authentication code")
|
||||||
|
return ErrAuthenticationFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
case AuthModeOTP:
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
|
@ -187,15 +187,6 @@ func (i *Interact) SetMessenger(messenger Messenger) {
|
||||||
|
|
||||||
// builtin initializes the built-in commands
|
// builtin initializes the built-in commands
|
||||||
func (i *Interact) builtin() error {
|
func (i *Interact) builtin() error {
|
||||||
i.Command("/auth", func(reply Reply) error {
|
|
||||||
reply.Message("Enter your authentication code")
|
|
||||||
return nil
|
|
||||||
}).NamedNext(StateAuthenticated, func(reply Reply, code string) error {
|
|
||||||
// check code
|
|
||||||
reply.Message("Great! You're authenticated!")
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
i.Command("/uptime", func(reply Reply) error {
|
i.Command("/uptime", func(reply Reply) error {
|
||||||
reply.Message("uptime")
|
reply.Message("uptime")
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue
Block a user