interact: improve strict mode authentication

This commit is contained in:
c9s 2022-01-14 02:13:59 +08:00
parent 72a925f659
commit 62e5706657
2 changed files with 29 additions and 0 deletions

View File

@ -135,6 +135,7 @@ func main() {
})
globalInteraction.AddCustomInteraction(&interact.AuthInteract{
Strict: true,
Mode: interact.AuthModeToken,
Token: "123",
})

View File

@ -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()) {