Add support for validating 2FA

This commit is contained in:
Neeraj Gupta
2023-09-15 16:17:38 +05:30
parent 018833c543
commit d442dff14a
4 changed files with 47 additions and 7 deletions

View File

@@ -13,8 +13,6 @@ var versionCmd = &cobra.Command{
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("ente-cli version %s\n", AppVersion)
// increcement counter flag
},
}

View File

@@ -134,3 +134,30 @@ func (c *Client) VerifyEmail(
}
return &res, nil
}
func (c *Client) VerifyTotp(
ctx context.Context,
sessionID string,
otp string,
) (*AuthorizationResponse, error) {
var res AuthorizationResponse
payload := map[string]interface{}{
"sessionID": sessionID,
"code": otp,
}
r, err := c.restClient.R().
SetContext(ctx).
SetResult(&res).
SetBody(payload).
Post("/users/two-factor/verify")
if err != nil {
return nil, err
}
if r.IsError() {
return nil, &ApiError{
StatusCode: r.StatusCode(),
Message: r.String(),
}
}
return &res, nil
}

View File

@@ -59,11 +59,8 @@ func (c *ClICtrl) AddAccount(cxt context.Context) {
if flowErr != nil {
return
}
if authResponse == nil {
return
}
if authResponse.IsMFARequired() {
authResponse, flowErr = c.validateTOTP(cxt, authResponse)
}
if keyEncKey == nil {
pass, flowErr := GetSensitiveField("Enter password")

View File

@@ -46,5 +46,23 @@ func (c *ClICtrl) signInViaPassword(ctx context.Context, email string, srpAttr *
}
return authResp, keyEncKey, nil
}
}
func (c *ClICtrl) validateTOTP(ctx context.Context, authResp *api.AuthorizationResponse) (*api.AuthorizationResponse, error) {
if !authResp.IsMFARequired() {
return authResp, nil
}
for {
// CLI prompt for TOTP
totp, flowErr := GetCode("Enter TOTP", 6)
if flowErr != nil {
return nil, flowErr
}
totpResp, err := c.Client.VerifyTotp(ctx, authResp.TwoFactorSessionID, totp)
if err != nil {
log.Printf("failed to verify %v", err)
continue
}
return totpResp, nil
}
}