summaryrefslogtreecommitdiff
path: root/internal/command/twofactorverify/twofactorverify.go
blob: a0a952cccfec1e7bfebdb7ac44a78f4d3de377d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package twofactorverify

import (
	"context"
	"fmt"
	"io"

	"gitlab.com/gitlab-org/labkit/log"

	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/readwriter"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/config"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/twofactorverify"
)

type Command struct {
	Config     *config.Config
	Args       *commandargs.Shell
	ReadWriter *readwriter.ReadWriter
}

func (c *Command) Execute(ctx context.Context) error {
	ctxlog := log.ContextLogger(ctx)
	ctxlog.Info("twofactorverify: execute: waiting for user input")
	otp := c.getOTP(ctx)

	ctxlog.Info("twofactorverify: execute: verifying entered OTP")
	err := c.verifyOTP(ctx, otp)
	if err != nil {
		ctxlog.WithError(err).Error("twofactorverify: execute: OTP verification failed")
		return err
	}

	ctxlog.WithError(err).Info("twofactorverify: execute: OTP verified")
	return nil
}

func (c *Command) getOTP(ctx context.Context) string {
	prompt := "OTP: "
	fmt.Fprint(c.ReadWriter.Out, prompt)

	var answer string
	otpLength := int64(64)
	reader := io.LimitReader(c.ReadWriter.In, otpLength)
	if _, err := fmt.Fscanln(reader, &answer); err != nil {
		log.ContextLogger(ctx).WithError(err).Debug("twofactorverify: getOTP: Failed to get user input")
	}

	return answer
}

func (c *Command) verifyOTP(ctx context.Context, otp string) error {
	client, err := twofactorverify.NewClient(c.Config)
	if err != nil {
		return err
	}

	err = client.VerifyOTP(ctx, c.Args, otp)
	if err == nil {
		fmt.Fprint(c.ReadWriter.Out, "\nOTP validation successful. Git operations are now allowed.\n")
	} else {
		fmt.Fprintf(c.ReadWriter.Out, "\nOTP validation failed.\n%v\n", err)
	}

	return nil
}