func requestLoginWith2faRetry(email, password string) error { var otp []byte var err error var msgs = [3]string{ i18n.G("Two-factor code: "), i18n.G("Bad code. Try again: "), i18n.G("Wrong again. Once more: "), } cli := Client() reader := bufio.NewReader(nil) for i := 0; ; i++ { // first try is without otp _, err = cli.Login(email, password, string(otp)) if i >= len(msgs) || !client.IsTwoFactorError(err) { return err } reader.Reset(Stdin) fmt.Fprint(Stdout, msgs[i]) // the browser shows it as well (and Sergio wants to see it ;) otp, _, err = reader.ReadLine() if err != nil { return err } } }
func (cs *clientSuite) TestIsTwoFactor(c *check.C) { c.Check(client.IsTwoFactorError(&client.Error{Kind: client.ErrorKindTwoFactorRequired}), check.Equals, true) c.Check(client.IsTwoFactorError(&client.Error{Kind: client.ErrorKindTwoFactorFailed}), check.Equals, true) c.Check(client.IsTwoFactorError(&client.Error{Kind: "some other kind"}), check.Equals, false) c.Check(client.IsTwoFactorError(errors.New("test")), check.Equals, false) c.Check(client.IsTwoFactorError(nil), check.Equals, false) c.Check(client.IsTwoFactorError((*client.Error)(nil)), check.Equals, false) }