Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update mfa admin methods #1774

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions internal/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type adminUserDeleteParams struct {

type adminUserUpdateFactorParams struct {
FriendlyName string `json:"friendly_name"`
FactorType string `json:"factor_type"`
Phone string `json:"phone"`
}

type AdminListUsersResponse struct {
Expand Down Expand Up @@ -618,11 +618,13 @@ func (a *API) adminUserUpdateFactor(w http.ResponseWriter, r *http.Request) erro
return terr
}
}
if params.FactorType != "" {
if params.FactorType != models.TOTP {
return badRequestError(ErrorCodeValidationFailed, "Factor Type not valid")
J0 marked this conversation as resolved.
Show resolved Hide resolved

if params.Phone != "" && factor.IsPhoneFactor() {
phone, err := validatePhone(params.Phone)
if err != nil {
return badRequestError(ErrorCodeValidationFailed, "Invalid phone number format (E.164 required)")
}
if terr := factor.UpdateFactorType(tx, params.FactorType); terr != nil {
if terr := factor.UpdatePhone(tx, phone); terr != nil {
return terr
}
}
Expand Down
14 changes: 3 additions & 11 deletions internal/api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func (ts *AdminTestSuite) TestAdminUserUpdateFactor() {
require.NoError(ts.T(), err, "Error making new user")
require.NoError(ts.T(), ts.API.db.Create(u), "Error creating user")

f := models.NewTOTPFactor(u, "testSimpleName")
f := models.NewPhoneFactor(u, "123456789", "testSimpleName")
require.NoError(ts.T(), f.SetSecret("secretkey", ts.Config.Security.DBEncryption.Encrypt, ts.Config.Security.DBEncryption.EncryptionKeyID, ts.Config.Security.DBEncryption.EncryptionKey))
require.NoError(ts.T(), ts.API.db.Create(f), "Error saving new test factor")

Expand All @@ -833,20 +833,12 @@ func (ts *AdminTestSuite) TestAdminUserUpdateFactor() {
ExpectedCode: http.StatusOK,
},
{
Desc: "Update factor: valid factor type",
Desc: "Update Factor phone number",
FactorData: map[string]interface{}{
"friendly_name": "john",
"factor_type": models.TOTP,
"phone": "+1976154321",
},
ExpectedCode: http.StatusOK,
},
{
Desc: "Update factor: invalid factor",
FactorData: map[string]interface{}{
"factor_type": "invalid_factor",
},
ExpectedCode: http.StatusBadRequest,
},
}

// Initialize factor data
Expand Down
11 changes: 5 additions & 6 deletions internal/models/factor.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,17 @@ func (f *Factor) UpdateFriendlyName(tx *storage.Connection, friendlyName string)
return tx.UpdateOnly(f, "friendly_name", "updated_at")
}

func (f *Factor) UpdatePhone(tx *storage.Connection, phone string) error {
f.Phone = storage.NullString(phone)
return tx.UpdateOnly(f, "phone", "updated_at")
}

// UpdateStatus modifies the factor status
func (f *Factor) UpdateStatus(tx *storage.Connection, state FactorState) error {
f.Status = state.String()
return tx.UpdateOnly(f, "status", "updated_at")
}

// UpdateFactorType modifies the factor type
func (f *Factor) UpdateFactorType(tx *storage.Connection, factorType string) error {
f.FactorType = factorType
return tx.UpdateOnly(f, "factor_type", "updated_at")
}

func (f *Factor) DowngradeSessionsToAAL1(tx *storage.Connection) error {
sessions, err := FindSessionsByFactorID(tx, f.ID)
if err != nil {
Expand Down
Loading