Add support for validating 2FA
This commit is contained in:
@@ -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
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user