diff options
author | James Sandlin <jsandlin@gitlab.com> | 2022-07-15 15:21:02 -0700 |
---|---|---|
committer | James Sandlin <jsandlin@gitlab.com> | 2022-07-15 15:21:02 -0700 |
commit | eb6033a66e054425fd43c8436fbc448907c0f160 (patch) | |
tree | 10b08332fcda1a3c75ed97ab7a16b90fbef4084e | |
parent | 1fa30cb09115b72707d14b3b42d4c14a7ea6965b (diff) | |
download | gitlab-shell-506-jsandlin.tar.gz |
close; things are colliding506-jsandlin
-rw-r--r-- | internal/command/twofactorverify/twofactorverify.go | 156 |
1 files changed, 27 insertions, 129 deletions
diff --git a/internal/command/twofactorverify/twofactorverify.go b/internal/command/twofactorverify/twofactorverify.go index 88c4978..20e3df2 100644 --- a/internal/command/twofactorverify/twofactorverify.go +++ b/internal/command/twofactorverify/twofactorverify.go @@ -34,107 +34,6 @@ var ( ctxMaxTime = time.Second + 30 ) -func (c *Command) Execute2(ctx context.Context) error { - ctxlog := log.ContextLogger(ctx) - - // config.GetHTTPClient isn't thread-safe so save Client in struct for concurrency - // workaround until #518 is fixed - var err error - c.Client, err = twofactorverify.NewClient(c.Config) - fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") - fmt.Println("client = ", c.Client) - fmt.Println("err = ", err) - fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") - if err != nil { - ctxlog.WithError(err).Error("twofactorverify: execute: OTP verification failed") - return err - } - - // Create timeout context - // TODO: make timeout configurable - const ctxTimeout = 30 - timeoutCtx, cancelTimeout := context.WithTimeout(ctx, ctxTimeout*time.Second) - verifyCtx, cancelVerify := context.WithCancel(timeoutCtx) - pushCtx, cancelPush := context.WithCancel(timeoutCtx) - defer cancelTimeout() - //fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") - //fmt.Println(verifyCtx, ", ", cancelVerify) - //fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") - // Background push notification with timeout - pushauth := make(chan Result) - go func() { - defer close(pushauth) - status, success, err := c.pushAuth(pushCtx) - - select { - case <-pushCtx.Done(): // push cancelled by manual OTP - pushauth <- Result{Error: nil, Status: "cancelled", Success: false} - default: - pushauth <- Result{Error: err, Status: status, Success: success} - cancelVerify() - } - }() - - // Also allow manual OTP entry while waiting for push, with same timeout as push - verify := make(chan Result) - go func() { - defer close(verify) - ctxlog.Info("twofactorverify: execute: waiting for user input") - answer := "" - answer = c.getOTP(verifyCtx) - - select { - case <-verifyCtx.Done(): // manual OTP cancelled by push - verify <- Result{Error: nil, Status: "cancelled", Success: false} - default: - cancelPush() - ctxlog.Info("twofactorverify: execute: verifying entered OTP") - status, success, err := c.verifyOTP(verifyCtx, answer) - //fmt.Println("-------------") - //fmt.Println("pushAuth.status = ", status) - //fmt.Println("pushAuth.success = ", success) - //fmt.Println("pushAuth.err = ", err) - //fmt.Println("-------------") - ctxlog.WithError(err).Info("twofactorverify: execute: OTP verified") - verify <- Result{Error: err, Status: status, Success: success} - } - }() - - - for { - select { - case res := <-verify: // manual OTP - //fmt.Println("-------------") - //fmt.Println("verify.res = ", res) - //fmt.Println("-------------") - if res.Status == "cancelled" { - // verify cancelled; don't print anything - } else if res.Status == "" { - // channel closed; don't print anything - } else { - fmt.Fprint(c.ReadWriter.Out, res.Status) - return nil - } - case res := <-pushauth: // push - //fmt.Println("-------------") - //fmt.Println("pushauth.res = ", res) - //fmt.Println("-------------") - if res.Status == "cancelled" { - // push cancelled; don't print anything - } else if res.Status == "" { - // channel closed; don't print anything - } else { - fmt.Fprint(c.ReadWriter.Out, res.Status) - return nil - } - case <-timeoutCtx.Done(): // push timed out - fmt.Fprint(c.ReadWriter.Out, "\nOTP verification timed out\n") - return nil - } - } - - return nil -} func (c *Command) Execute(ctx context.Context) error { ctxlog := log.ContextLogger(ctx) @@ -154,6 +53,8 @@ func (c *Command) Execute(ctx context.Context) error { defer cancelCtx() //myctx, mycancel := context.WithCancel(timeoutCtx) + myctx2, cancelCtx2 := context.WithTimeout(ctx, ctxMaxTime) + defer cancelCtx2() // Also allow manual OTP entry while waiting for push, with same timeout as push @@ -182,44 +83,41 @@ func (c *Command) Execute(ctx context.Context) error { defer waitGroup.Done() //defer close(pushChannel) ctxlog.Info("twofactorverify: execute: waiting for push auth") - //status, success, err := c.pushAuth(myctx) - //ctxlog.WithError(err).Info("twofactorverify: execute: push auth verified") + ctxlog.WithError(err).Info("twofactorverify: execute: push auth verified") select { - case <-myctx.Done(): // push cancelled by manual OTP + case <-myctx2.Done(): // push cancelled by manual OTP // skip writing to channel pushChannel <- Result{Error: nil, Status: "cancelled", Success: false} ctxlog.Info("twofactorverify: execute: push auth cancelled") - //default: - // pushChannel <- Result{Error: err, Status: status, Success: success} + default: + status, success, err := c.pushAuth(myctx2) + pushChannel <- Result{Error: err, Status: status, Success: success} } }() - //for { - select { - case res := <-otpChannel: - //fmt.Println("Received from otpChannel => ", res) - if len(res.Status) > 0 && res.Status != "cancelled" { - fmt.Fprint(c.ReadWriter.Out, res.Status) - return nil - } - case res := <-pushChannel: - if len(res.Status) > 0 && res.Status != "cancelled" { - //fmt.Println("Received from pushChannel => ", res) - fmt.Println("res.Status == ", res.Status, " -> ", len(res.Status)) - // //fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") - // //fmt.Println(res) - // //fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") - fmt.Fprint(c.ReadWriter.Out, res.Status) - return nil - } - //case <- myctx.Done(): - // fmt.Fprint(c.ReadWriter.Out, "\nOTP verification timed out\n") - // return nil - } + select { + case res := <-otpChannel: + //fmt.Println("Received from otpChannel => ", res) + if len(res.Status) > 0 && res.Status != "cancelled" { + fmt.Fprint(c.ReadWriter.Out, res.Status) + return nil + } + case res := <-pushChannel: + if len(res.Status) > 0 && res.Status != "cancelled" { + //fmt.Println("Received from pushChannel => ", res) + fmt.Println("res.Status == ", res.Status, " -> ", len(res.Status)) + fmt.Fprint(c.ReadWriter.Out, res.Status) + return nil + } + + case <- myctx.Done(): + fmt.Fprint(c.ReadWriter.Out, "\nOTP verification timed out\n") + return nil + + } waitGroup.Wait() - //} return nil } |