Skip to content

Commit

Permalink
feat: added totp support for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
sheensantoscapadngan committed Nov 13, 2024
1 parent 8fef691 commit b050db8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 4 additions & 2 deletions cli/packages/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type GetOrganizationsResponse struct {
type SelectOrganizationResponse struct {
Token string `json:"token"`
MfaEnabled bool `json:"isMfaEnabled"`
MfaMethod string `json:"mfaMethod"`
}

type SelectOrganizationRequest struct {
Expand Down Expand Up @@ -260,8 +261,9 @@ type GetLoginTwoV2Response struct {
}

type VerifyMfaTokenRequest struct {
Email string `json:"email"`
MFAToken string `json:"mfaToken"`
Email string `json:"email"`
MFAToken string `json:"mfaToken"`
MFAMethod string `json:"mfaMethod"`
}

type VerifyMfaTokenResponse struct {
Expand Down
11 changes: 6 additions & 5 deletions cli/packages/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ var initCmd = &cobra.Command{
if tokenResponse.MfaEnabled {
i := 1
for i < 6 {
mfaVerifyCode := askForMFACode()
mfaVerifyCode := askForMFACode(tokenResponse.MfaMethod)

httpClient := resty.New()
httpClient.SetAuthToken(tokenResponse.Token)
verifyMFAresponse, mfaErrorResponse, requestError := api.CallVerifyMfaToken(httpClient, api.VerifyMfaTokenRequest{
Email: userCreds.UserCredentials.Email,
MFAToken: mfaVerifyCode,
Email: userCreds.UserCredentials.Email,
MFAToken: mfaVerifyCode,
MFAMethod: tokenResponse.MfaMethod,
})
if requestError != nil {
util.HandleError(err)
Expand All @@ -99,7 +100,7 @@ var initCmd = &cobra.Command{
break
}
}

if mfaErrorResponse.Context.Code == "mfa_expired" {
util.PrintErrorMessageAndExit("Your 2FA verification code has expired, please try logging in again")
break
Expand Down
19 changes: 13 additions & 6 deletions cli/packages/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func cliDefaultLogin(userCredentialsToBeStored *models.UserCredentials) {
if loginTwoResponse.MfaEnabled {
i := 1
for i < 6 {
mfaVerifyCode := askForMFACode()
mfaVerifyCode := askForMFACode("email")

httpClient := resty.New()
httpClient.SetAuthToken(loginTwoResponse.Token)
Expand Down Expand Up @@ -756,13 +756,14 @@ func GetJwtTokenWithOrganizationId(oldJwtToken string, email string) string {
if selectedOrgRes.MfaEnabled {
i := 1
for i < 6 {
mfaVerifyCode := askForMFACode()
mfaVerifyCode := askForMFACode(selectedOrgRes.MfaMethod)

httpClient := resty.New()
httpClient.SetAuthToken(selectedOrgRes.Token)
verifyMFAresponse, mfaErrorResponse, requestError := api.CallVerifyMfaToken(httpClient, api.VerifyMfaTokenRequest{
Email: email,
MFAToken: mfaVerifyCode,
Email: email,
MFAToken: mfaVerifyCode,
MFAMethod: selectedOrgRes.MfaMethod,
})
if requestError != nil {
util.HandleError(err)
Expand Down Expand Up @@ -817,9 +818,15 @@ func generateFromPassword(password string, salt []byte, p *params) (hash []byte,
return hash, nil
}

func askForMFACode() string {
func askForMFACode(mfaMethod string) string {
var label string
if mfaMethod == "totp" {
label = "Enter the verification code from your mobile authenticator app or use a recovery code"
} else {
label = "Enter the 2FA verification code sent to your email"
}
mfaCodePromptUI := promptui.Prompt{
Label: "Enter the 2FA verification code sent to your email",
Label: label,
}

mfaVerifyCode, err := mfaCodePromptUI.Run()
Expand Down

0 comments on commit b050db8

Please sign in to comment.