Skip to content

Commit

Permalink
Add support for CLI-based MFA recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
nmanoogian committed Jun 5, 2024
1 parent 4e54853 commit 095b55a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/cmd/mfa-recovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright © 2023 Doppler <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/http"
"github.com/DopplerHQ/cli/pkg/utils"
"github.com/spf13/cobra"
)

var mfaRecoveryCmd = &cobra.Command{
Use: "mfa-recovery",
Short: "Initiate an MFA recovery",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
yes := utils.GetBoolFlag(cmd, "yes")
localConfig := configuration.LocalConfig(cmd)

utils.RequireValue("token", localConfig.Token.Value)

if !yes {
utils.Print("MFA recovery allows you to use the Doppler CLI to generate a short-lived, one-time recovery code to log into the Doppler dashboard.")
utils.Print("The code will be sent to your email address.")
}

confirmed := yes || utils.ConfirmationPrompt("Initiate MFA recovery", false)
if !confirmed {
return
}

err := http.InitiateMfaRecovery(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}

utils.Print("A one-time recovery code has been sent to your email address.")
},
}

func init() {
mfaRecoveryCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
rootCmd.AddCommand(mfaRecoveryCmd)
}
14 changes: 14 additions & 0 deletions pkg/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1384,3 +1384,17 @@ func GetActorInfo(host string, verifyTLS bool, apiKey string) (models.ActorInfo,

return info, Error{}
}

func InitiateMfaRecovery(host string, verifyTLS bool, apiKey string) Error {
url, err := generateURL(host, "/v3/me/mfa_recovery", nil)
if err != nil {
return Error{Err: err, Message: "Unable to generate url"}
}

statusCode, _, _, err := PostRequest(url, verifyTLS, apiKeyHeader(apiKey), nil)
if err != nil {
return Error{Err: err, Message: "Unable to initiate MFA recovery", Code: statusCode}
}

return Error{}
}

0 comments on commit 095b55a

Please sign in to comment.