2022-01-13 16:26:53 +00:00
|
|
|
package interact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-01-13 18:13:59 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2022-01-13 16:26:53 +00:00
|
|
|
|
|
|
|
"github.com/pquerna/otp"
|
2022-01-13 17:00:31 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2022-01-13 18:13:59 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-13 16:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AuthMode string
|
|
|
|
|
|
|
|
const (
|
|
|
|
AuthModeOTP AuthMode = "OTP"
|
|
|
|
AuthModeToken AuthMode = "TOKEN"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrAuthenticationFailed = errors.New("authentication failed")
|
|
|
|
|
2022-01-13 17:58:04 +00:00
|
|
|
type Authorizer interface {
|
2022-01-13 18:36:06 +00:00
|
|
|
StartAuthorizing()
|
2022-01-13 17:58:04 +00:00
|
|
|
Authorize() error
|
|
|
|
}
|
|
|
|
|
2022-01-13 16:26:53 +00:00
|
|
|
type AuthInteract struct {
|
2022-01-13 17:58:04 +00:00
|
|
|
Strict bool `json:"strict,omitempty"`
|
|
|
|
|
2022-01-13 16:26:53 +00:00
|
|
|
Mode AuthMode `json:"authMode"`
|
|
|
|
|
|
|
|
Token string `json:"authToken,omitempty"`
|
|
|
|
|
|
|
|
OneTimePasswordKey *otp.Key `json:"otpKey,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:00:31 +00:00
|
|
|
func (it *AuthInteract) Commands(interact *Interact) {
|
2022-01-13 17:58:04 +00:00
|
|
|
if it.Strict {
|
2022-01-13 18:13:59 +00:00
|
|
|
// 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
|
|
|
|
}
|
2022-01-13 18:57:39 +00:00
|
|
|
interact.Command("/auth", "authorize", func(reply Reply, authorizer Authorizer) error {
|
2022-01-13 17:58:04 +00:00
|
|
|
reply.Message("Enter your authentication token")
|
2022-01-13 18:36:06 +00:00
|
|
|
authorizer.StartAuthorizing()
|
2022-01-13 17:58:04 +00:00
|
|
|
return nil
|
|
|
|
}).Next(func(token string, reply Reply) error {
|
|
|
|
if token == it.Token {
|
|
|
|
reply.Message("Token passed, please enter your one-time password")
|
2022-01-13 18:13:59 +00:00
|
|
|
|
|
|
|
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] ======================================")
|
2022-01-13 16:26:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-13 18:13:59 +00:00
|
|
|
|
2022-01-13 17:58:04 +00:00
|
|
|
return ErrAuthenticationFailed
|
|
|
|
}).NamedNext(StateAuthenticated, func(code string, reply Reply, authorizer Authorizer) error {
|
2022-01-13 17:00:31 +00:00
|
|
|
if totp.Validate(code, it.OneTimePasswordKey.Secret()) {
|
|
|
|
reply.Message("Great! You're authenticated!")
|
2022-01-13 18:36:06 +00:00
|
|
|
interact.SetOriginState(StateAuthenticated)
|
2022-01-13 17:58:04 +00:00
|
|
|
return authorizer.Authorize()
|
2022-01-13 17:00:31 +00:00
|
|
|
}
|
2022-01-13 16:26:53 +00:00
|
|
|
|
2022-01-13 17:58:04 +00:00
|
|
|
reply.Message("Incorrect authentication code")
|
|
|
|
return ErrAuthenticationFailed
|
|
|
|
})
|
|
|
|
} else {
|
2022-01-13 18:57:39 +00:00
|
|
|
interact.Command("/auth", "authorize", func(reply Reply) error {
|
2022-01-13 17:58:04 +00:00
|
|
|
reply.Message("Enter your authentication code")
|
|
|
|
return nil
|
|
|
|
}).NamedNext(StateAuthenticated, func(code string, reply Reply, authorizer Authorizer) error {
|
|
|
|
switch it.Mode {
|
|
|
|
case AuthModeToken:
|
|
|
|
if code == it.Token {
|
|
|
|
reply.Message("Great! You're authenticated!")
|
2022-01-13 18:36:06 +00:00
|
|
|
interact.SetOriginState(StateAuthenticated)
|
2022-01-13 17:58:04 +00:00
|
|
|
return authorizer.Authorize()
|
|
|
|
}
|
|
|
|
|
|
|
|
case AuthModeOTP:
|
|
|
|
if totp.Validate(code, it.OneTimePasswordKey.Secret()) {
|
|
|
|
reply.Message("Great! You're authenticated!")
|
2022-01-13 18:36:06 +00:00
|
|
|
interact.SetOriginState(StateAuthenticated)
|
2022-01-13 17:58:04 +00:00
|
|
|
return authorizer.Authorize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Message("Incorrect authentication code")
|
|
|
|
return ErrAuthenticationFailed
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 16:26:53 +00:00
|
|
|
}
|