Skip to content

Commit 2ec220a

Browse files
fix(auth): Allow to update MFA (#530)
* fix: MFA uid can be empty string * fix: update request have different structure * test: cover changes * fix: display name required for MFA * chore: link to the mfa
1 parent c294161 commit 2ec220a

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

auth/user_mgt.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ type multiFactorInfoResponse struct {
6868
EnrolledAt string `json:"enrolledAt,omitempty"`
6969
}
7070

71+
type multiFactorEnrollments struct {
72+
Enrollments []*multiFactorInfoResponse `json:"enrollments"`
73+
}
74+
7175
// MultiFactorInfo describes a user enrolled second phone factor.
7276
// TODO : convert PhoneNumber to PhoneMultiFactorInfo struct
7377
type MultiFactorInfo struct {
@@ -166,18 +170,19 @@ func (u *UserToCreate) set(key string, value interface{}) *UserToCreate {
166170

167171
// Converts a client format second factor object to server format.
168172
func convertMultiFactorInfoToServerFormat(mfaInfo MultiFactorInfo) (multiFactorInfoResponse, error) {
169-
var authFactorInfo multiFactorInfoResponse
173+
authFactorInfo := multiFactorInfoResponse{DisplayName: mfaInfo.DisplayName}
170174
if mfaInfo.EnrollmentTimestamp != 0 {
171175
authFactorInfo.EnrolledAt = time.Unix(mfaInfo.EnrollmentTimestamp, 0).Format("2006-01-02T15:04:05Z07:00Z")
172176
}
177+
if mfaInfo.UID != "" {
178+
authFactorInfo.MFAEnrollmentID = mfaInfo.UID
179+
}
173180
if mfaInfo.FactorID == phoneMultiFactorID {
174181
authFactorInfo.PhoneInfo = mfaInfo.PhoneNumber
175-
authFactorInfo.DisplayName = mfaInfo.DisplayName
176-
authFactorInfo.MFAEnrollmentID = mfaInfo.UID
177182
return authFactorInfo, nil
178183
}
179184
out, _ := json.Marshal(mfaInfo)
180-
return multiFactorInfoResponse{}, fmt.Errorf("Unsupported second factor %s provided", string(out))
185+
return multiFactorInfoResponse{}, fmt.Errorf("unsupported second factor %s provided", string(out))
181186
}
182187

183188
func (u *UserToCreate) validatedRequest() (map[string]interface{}, error) {
@@ -333,7 +338,9 @@ func (u *UserToUpdate) validatedRequest() (map[string]interface{}, error) {
333338
if err != nil {
334339
return nil, err
335340
}
336-
req["mfaInfo"] = mfaInfo
341+
342+
// https://cloud.google.com/identity-platform/docs/reference/rest/v1/accounts/update
343+
req["mfa"] = multiFactorEnrollments{mfaInfo}
337344
} else {
338345
req[k] = v
339346
}
@@ -665,9 +672,6 @@ func validateAndFormatMfaSettings(mfaSettings MultiFactorSettings, methodType st
665672
return nil, fmt.Errorf("\"uid\" is not supported when adding second factors via \"createUser()\"")
666673
}
667674
case updateUserMethod:
668-
if multiFactorInfo.UID == "" {
669-
return nil, fmt.Errorf("the second factor \"uid\" must be a valid non-empty string when adding second factors via \"updateUser()\"")
670-
}
671675
default:
672676
return nil, fmt.Errorf("unsupported methodType: %s", methodType)
673677
}

auth/user_mgt_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -894,17 +894,6 @@ func TestInvalidUpdateUser(t *testing.T) {
894894
},
895895
}),
896896
`the second factor "phoneNumber" for "invalid" must be a non-empty E.164 standard compliant identifier string`,
897-
}, {
898-
(&UserToUpdate{}).MFASettings(MultiFactorSettings{
899-
EnrolledFactors: []*MultiFactorInfo{
900-
{
901-
PhoneNumber: "+11234567890",
902-
FactorID: "phone",
903-
DisplayName: "Spouse's phone number",
904-
},
905-
},
906-
}),
907-
`the second factor "uid" must be a valid non-empty string when adding second factors via "updateUser()"`,
908897
}, {
909898
(&UserToUpdate{}).ProviderToLink(&UserProvider{UID: "google_uid"}),
910899
"user provider must specify a provider ID",
@@ -1059,10 +1048,14 @@ var updateUserCases = []struct {
10591048
PhoneNumber: "+11234567890",
10601049
DisplayName: "Spouse's phone number",
10611050
FactorID: "phone",
1051+
}, {
1052+
PhoneNumber: "+11234567890",
1053+
DisplayName: "Spouse's phone number",
1054+
FactorID: "phone",
10621055
},
10631056
},
10641057
}),
1065-
map[string]interface{}{"mfaInfo": []*multiFactorInfoResponse{
1058+
map[string]interface{}{"mfa": multiFactorEnrollments{Enrollments: []*multiFactorInfoResponse{
10661059
{
10671060
MFAEnrollmentID: "enrolledSecondFactor1",
10681061
PhoneInfo: "+11234567890",
@@ -1074,12 +1067,16 @@ var updateUserCases = []struct {
10741067
DisplayName: "Spouse's phone number",
10751068
PhoneInfo: "+11234567890",
10761069
},
1077-
},
1070+
{
1071+
DisplayName: "Spouse's phone number",
1072+
PhoneInfo: "+11234567890",
1073+
},
1074+
}},
10781075
},
10791076
},
10801077
{
10811078
(&UserToUpdate{}).MFASettings(MultiFactorSettings{}),
1082-
map[string]interface{}{"mfaInfo": nil},
1079+
map[string]interface{}{"mfa": multiFactorEnrollments{Enrollments: nil}},
10831080
},
10841081
{
10851082
(&UserToUpdate{}).ProviderToLink(&UserProvider{

0 commit comments

Comments
 (0)