From 7f6b344a1e36fac527d0614d52d58223d4d86ff4 Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Wed, 18 May 2022 16:33:12 +0100 Subject: [PATCH 1/2] allow user update to remove providers --- .../Auth/Users/FirebaseUserManagerTest.cs | 40 ++++++++++++++++++- .../FirebaseAdmin/Auth/UserRecordArgs.cs | 24 +++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs b/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs index 84a37494..0ab884d9 100644 --- a/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs @@ -1200,7 +1200,7 @@ public async Task UpdateUserRemoveAttributes(TestConfig config) [Theory] [MemberData(nameof(TestConfigs))] - public async Task UpdateUserRemoveProviders(TestConfig config) + public async Task UpdateUserRemovePhoneProviderAutomatically(TestConfig config) { var handler = new MockMessageHandler() { @@ -1227,6 +1227,44 @@ public async Task UpdateUserRemoveProviders(TestConfig config) request["deleteProvider"]); } + [Theory] + [MemberData(nameof(TestConfigs))] + public async Task UpdateUserRemoveProviders(TestConfig config) + { + const string user = @"{ + ""localId"": ""user1"", + ""providerUserInfo"": [{ + ""rawId"": ""googleuid"", + ""providerId"": ""google.com"" + }] + }"; + + var handler = new MockMessageHandler() + { + Response = new List() { CreateUserResponse, config.GetUserResponse(user) }, + }; + + var auth = config.CreateAuth(handler); + + var userRecord = await auth.UpdateUserAsync(new UserRecordArgs() + { + Uid = "user1", + ProvidersToDelete = new[] { "google.com" }, + }); + + Assert.Equal("user1", userRecord.Uid); + Assert.Equal(config.TenantId, userRecord.TenantId); + Assert.Equal(2, handler.Requests.Count); + config.AssertRequest("accounts:update", handler.Requests[0]); + config.AssertRequest("accounts:lookup", handler.Requests[1]); + var request = NewtonsoftJsonSerializer.Instance.Deserialize(handler.Requests[0].Body); + Assert.Equal(2, request.Count); + Assert.Equal("user1", request["localId"]); + Assert.Equal( + new JArray() { "google.com" }, + request["deleteProvider"]); + } + [Theory] [MemberData(nameof(TestConfigs))] public async Task UpdateUserSetCustomClaims(TestConfig config) diff --git a/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs b/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs index 433deb1b..162a7ed5 100644 --- a/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs +++ b/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Text.RegularExpressions; using FirebaseAdmin.Auth.Jwt; @@ -31,6 +32,7 @@ public sealed class UserRecordArgs private Optional photoUrl; private Optional phoneNumber; private Optional> customClaims; + private Optional> providersToDelete; private bool? disabled = null; private bool? emailVerified = null; @@ -102,6 +104,12 @@ internal IReadOnlyDictionary CustomClaims set => this.customClaims = this.Wrap(value); } + internal IEnumerable ProvidersToDelete + { + get => this.providersToDelete?.Value; + set => this.providersToDelete = this.Wrap(value); + } + internal static string CheckUid(string uid, bool required = false) { if (uid == null) @@ -361,6 +369,14 @@ internal UpdateUserRequest(UserRecordArgs args) this.PhoneNumber = CheckPhoneNumber(phoneNumber); } } + + if (args.providersToDelete != null) + { + foreach (var providerToDelete in args.providersToDelete.Value) + { + this.AddDeleteProvider(providerToDelete); + } + } } [JsonProperty("customAttributes")] @@ -411,12 +427,12 @@ private void AddDeleteAttribute(string attribute) private void AddDeleteProvider(string provider) { - if (this.DeleteProvider == null) + this.DeleteProvider ??= new List(); + + if (!this.DeleteProvider.Contains(provider)) { - this.DeleteProvider = new List(); + this.DeleteProvider.Add(provider); } - - this.DeleteProvider.Add(provider); } } From 60c21cee8501bfac1575aef4f55885b9d6ddd83c Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Wed, 18 May 2022 16:39:46 +0100 Subject: [PATCH 2/2] make the prop public --- .../FirebaseAdmin/Auth/UserRecordArgs.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs b/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs index 162a7ed5..1b1fc9ec 100644 --- a/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs +++ b/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs @@ -32,7 +32,6 @@ public sealed class UserRecordArgs private Optional photoUrl; private Optional phoneNumber; private Optional> customClaims; - private Optional> providersToDelete; private bool? disabled = null; private bool? emailVerified = null; @@ -91,6 +90,11 @@ public bool Disabled set => this.disabled = value; } + /// + /// Gets or sets the list of providers the user account should have deleted. + /// + public IEnumerable ProvidersToDelete { get; set; } + /// /// Gets or sets the password of the user. /// @@ -104,12 +108,6 @@ internal IReadOnlyDictionary CustomClaims set => this.customClaims = this.Wrap(value); } - internal IEnumerable ProvidersToDelete - { - get => this.providersToDelete?.Value; - set => this.providersToDelete = this.Wrap(value); - } - internal static string CheckUid(string uid, bool required = false) { if (uid == null) @@ -370,9 +368,9 @@ internal UpdateUserRequest(UserRecordArgs args) } } - if (args.providersToDelete != null) + if (args.ProvidersToDelete != null) { - foreach (var providerToDelete in args.providersToDelete.Value) + foreach (var providerToDelete in args.ProvidersToDelete) { this.AddDeleteProvider(providerToDelete); }