Skip to content

Commit 12dab43

Browse files
authored
Fix user lookup http calls (#1573)
* Fix user lookup http calls * Changelog entry * Fix PCL build
1 parent 1875122 commit 12dab43

File tree

7 files changed

+77
-13
lines changed

7 files changed

+77
-13
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ to the new 2.x format. It can be thrown when using `Realm.GetInstance` or `Realm
1919
that allows you to open the old Realm file in a dynamic mode and migrate any required data. ([#1552](https://github.com/realm/realm-dotnet/pull/1552))
2020
- Enable encryption on Windows. ([#1570](https://github.com/realm/realm-dotnet/pull/1570))
2121
- Enable Realm compaction on Windows. ([#1571](https://github.com/realm/realm-dotnet/pull/1571))
22+
- `UserInfo` has been significantly enhanced. It now contains metadata about a user stored on the Realm Object Server, as well as a list of all user
23+
account data associated with that user. ([#1573](https://github.com/realm/realm-dotnet/pull/1573))
2224

2325
### Bug fixes
2426
- `Realm.GetInstance` will now advance the Realm to the latest version, so you no longer have to call `Refresh` manually after that. ([#1523](https://github.com/realm/realm-dotnet/pull/1523))

Platform.PCL/Realm.Sync.PCL/Realm.Sync.PCL.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<Compile Include="..\..\Realm\Realm.Sync\Dtos\UserInfo.cs">
3838
<Link>Dtos\UserInfo.cs</Link>
3939
</Compile>
40+
<Compile Include="..\..\Realm\Realm.Sync\Dtos\AccountInfo.cs">
41+
<Link>Dtos\AccountInfo.cs</Link>
42+
</Compile>
4043
<Compile Include="..\..\Realm\Realm.Sync\Exceptions\IncompatibleSyncedFileException.cs">
4144
<Link>Exceptions\IncompatibleSyncedFileException.cs</Link>
4245
</Compile>

Realm/Realm.Sync/Dtos/AccountInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright 2017 Realm Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
////////////////////////////////////////////////////////////////////////////
18+
19+
namespace Realms.Sync
20+
{
21+
/// <summary>
22+
/// An object containing information about an account associated with a user.
23+
/// </summary>
24+
public class AccountInfo
25+
{
26+
/// <summary>
27+
/// Gets the provider that manages this user account.
28+
/// </summary>
29+
public string Provider { get; internal set; }
30+
31+
/// <summary>
32+
/// Gets the user account's identity in the provider's system.
33+
/// </summary>
34+
public string ProviderUserIdentity { get; internal set; }
35+
}
36+
}

Realm/Realm.Sync/Dtos/UserInfo.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
//
1717
////////////////////////////////////////////////////////////////////////////
1818

19+
using System.Collections.Generic;
20+
1921
namespace Realms.Sync
2022
{
2123
/// <summary>
@@ -34,13 +36,13 @@ public class UserInfo
3436
public bool IsAdmin { get; internal set; }
3537

3638
/// <summary>
37-
/// Gets the provider that the user registered through.
39+
/// Gets a collection of all the user accounts associated with the user.
3840
/// </summary>
39-
public string Provider { get; internal set; }
41+
public IEnumerable<AccountInfo> Accounts { get; internal set; }
4042

4143
/// <summary>
42-
/// Gets the user's identity in the provider's system.
44+
/// Gets the metadata about this user stored on the Realm Object Server.
4345
/// </summary>
44-
public string ProviderUserIdentity { get; internal set; }
46+
public IDictionary<string, string> Metadata { get; internal set; }
4547
}
4648
}

Realm/Realm.Sync/Helpers/AuthenticationHelper.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using System.Collections.Concurrent;
2121
using System.Collections.Generic;
22+
using System.Linq;
2223
using System.Net;
2324
using System.Net.Http;
2425
using System.Net.Http.Headers;
@@ -139,18 +140,29 @@ public static Task ChangePasswordAsync(User user, string password, string otherU
139140

140141
public static async Task<UserInfo> RetrieveInfoForUserAsync(User user, string provider, string providerId)
141142
{
142-
var uri = new Uri(user.ServerUri, $"/api/providers/{provider}/accounts/{providerId}");
143+
var uri = new Uri(user.ServerUri, $"/auth/users/{provider}/{providerId}");
143144
try
144145
{
145146
var response = await MakeAuthRequestAsync(HttpMethod.Get, uri, setupRequest: request =>
146147
request.Headers.TryAddWithoutValidation("Authorization", user.RefreshToken));
147148

149+
var accounts = response["accounts"].Children<JObject>()
150+
.Select(j => new AccountInfo
151+
{
152+
Provider = j["provider"].Value<string>(),
153+
ProviderUserIdentity = j["provider_id"].Value<string>()
154+
})
155+
.ToArray();
156+
157+
var metadata = response["metadata"].Children<JObject>()
158+
.ToDictionary(j => j["key"].Value<string>(), j => j["value"].Value<string>());
159+
148160
return new UserInfo
149161
{
150-
Identity = response["user"]["id"].Value<string>(),
151-
IsAdmin = response["user"]["isAdmin"].Value<bool>(),
152-
Provider = response["provider"].Value<string>(),
153-
ProviderUserIdentity = response["provider_id"].Value<string>(),
162+
Identity = response["user_id"].Value<string>(),
163+
IsAdmin = response["is_admin"].Value<bool>(),
164+
Accounts = accounts,
165+
Metadata = metadata
154166
};
155167
}
156168
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound)

Realm/Realm.Sync/Realm.Sync.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<Compile Include="SessionState.cs" />
2525
<Compile Include="SyncConfiguration.cs" />
2626
<Compile Include="User.cs" />
27+
<Compile Include="Dtos\AccountInfo.cs" />
2728
<Compile Include="Dtos\UserInfo.cs" />
2829
<Compile Include="UserPersistenceMode.cs" />
2930
<Compile Include="UserState.cs" />

Tests/Tests.Sync.Shared/CredentialsTests.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Linq;
2122
using System.Threading.Tasks;
2223
using Nito.AsyncEx;
2324
using NUnit.Framework;
@@ -195,8 +196,12 @@ public void UserLookup_WhenTargetUserExists_ShouldReturnResponse()
195196

196197
Assert.That(lookupResponse.Identity, Is.EqualTo(alice.Identity));
197198
Assert.That(lookupResponse.IsAdmin, Is.False);
198-
Assert.That(lookupResponse.Provider, Is.EqualTo(Credentials.Provider.UsernamePassword));
199-
Assert.That(lookupResponse.ProviderUserIdentity, Is.EqualTo(aliceUsername));
199+
200+
Assert.That(lookupResponse.Accounts, Is.Not.Empty);
201+
var passwordAccount = lookupResponse.Accounts.SingleOrDefault(a => a.Provider == Credentials.Provider.UsernamePassword);
202+
203+
Assert.That(passwordAccount, Is.Not.Null);
204+
Assert.That(passwordAccount.ProviderUserIdentity, Is.EqualTo(aliceUsername));
200205
});
201206
}
202207

@@ -229,8 +234,11 @@ public void UserLookup_WhenTargetUserIsSelf_ShouldReturnResponse()
229234

230235
Assert.That(lookupResponse.Identity, Is.EqualTo(admin.Identity));
231236
Assert.That(lookupResponse.IsAdmin, Is.True);
232-
Assert.That(lookupResponse.Provider, Is.EqualTo(Credentials.Provider.UsernamePassword));
233-
Assert.That(lookupResponse.ProviderUserIdentity, Is.EqualTo(Constants.AdminUsername));
237+
Assert.That(lookupResponse.Accounts, Is.Not.Empty);
238+
var passwordAccount = lookupResponse.Accounts.SingleOrDefault(a => a.Provider == Credentials.Provider.UsernamePassword);
239+
240+
Assert.That(passwordAccount, Is.Not.Null);
241+
Assert.That(passwordAccount.ProviderUserIdentity, Is.EqualTo(Constants.AdminUsername));
234242
});
235243
}
236244

0 commit comments

Comments
 (0)