Skip to content

Commit

Permalink
Add JWT provider (#1655)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev authored Dec 13, 2017
1 parent fbb9694 commit b499b88
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ x.y.z (TBD)
- Added an `IsDynamic` property to `RealmConfigurationBase`, allowing you to open a Realm file and read its schema from disk. ([#1637](https://github.com/realm/realm-dotnet/pull/1637))
- Added a new `InMemoryConfiguration` class that allows you to create an in-memory Realm instance. ([#1638](https://github.com/realm/realm-dotnet/pull/1638))
- Allow setting elements of a list directly - e.g. `foo.Bars[2] = new Bar()` or `foo.Integers[3] = 5`. ([#1641](https://github.com/realm/realm-dotnet/pull/1641))
- Added Json Web Token (JWT) credentials provider. ([#1654](https://github.com/realm/realm-dotnet/pull/1638))

### Bug fixes

Expand Down
16 changes: 16 additions & 0 deletions Platform.PCL/Realm.Sync.PCL/CredentialsPCL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static class Provider
/// The Azure Active Directory provider, associated with <see cref="Credentials.AzureAD"/>.
/// </summary>
public const string AzureAD = "azuread";

/// <summary>
/// The Json Web Token provider, associated with <see cref="Credentials.JWT"/>.
/// </summary>
public const string JWT = "jwt";
}

/// <summary>
Expand Down Expand Up @@ -120,6 +125,17 @@ public static Credentials AzureAD(string adToken)
return null;
}

/// <summary>
/// Creates <see cref="Credentials"/> based on a Facebook login.
/// </summary>
/// <param name="token">A Json Web Token, obtained by logging into Facebook.</param>
/// <returns>An instance of <see cref="Credentials"/> that can be used in <see cref="User.LoginAsync"/></returns>
public static Credentials JWT(string token)
{
RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
return null;
}

/// <summary>
/// Gets the identity provider for the credentials.
/// </summary>
Expand Down
54 changes: 39 additions & 15 deletions Realm/Realm.Sync/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
//
////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Realms.Helpers;

namespace Realms.Sync
{
Expand Down Expand Up @@ -55,6 +55,11 @@ public static class Provider
/// The Azure Active Directory provider, associated with <see cref="Credentials.AzureAD"/>.
/// </summary>
public const string AzureAD = "azuread";

/// <summary>
/// The Json Web Token provider, associated with <see cref="Credentials.JWT"/>.
/// </summary>
public const string JWT = "jwt";
}

internal static class Keys
Expand Down Expand Up @@ -105,12 +110,13 @@ public static Credentials Debug()
/// <returns>An instance of <see cref="Credentials"/> that can be used in <see cref="User.LoginAsync"/></returns>
public static Credentials Facebook(string facebookToken)
{
if (facebookToken == null)
{
throw new ArgumentNullException(nameof(facebookToken));
}
Argument.NotNull(facebookToken, nameof(facebookToken));

return new Credentials { IdentityProvider = Provider.Facebook, Token = facebookToken };
return new Credentials
{
IdentityProvider = Provider.Facebook,
Token = facebookToken
};
}

/// <summary>
Expand All @@ -120,12 +126,13 @@ public static Credentials Facebook(string facebookToken)
/// <returns>An instance of <see cref="Credentials"/> that can be used in <see cref="User.LoginAsync"/></returns>
public static Credentials Google(string googleToken)
{
if (googleToken == null)
{
throw new ArgumentNullException(nameof(googleToken));
}
Argument.NotNull(googleToken, nameof(googleToken));

return new Credentials { IdentityProvider = Provider.Google, Token = googleToken };
return new Credentials
{
IdentityProvider = Provider.Google,
Token = googleToken
};
}

/// <summary>
Expand All @@ -152,12 +159,29 @@ public static Credentials UsernamePassword(string username, string password, boo
/// <returns>An instance of <see cref="Credentials"/> that can be used in <see cref="User.LoginAsync"/></returns>
public static Credentials AzureAD(string adToken)
{
if (adToken == null)
Argument.NotNull(adToken, nameof(adToken));

return new Credentials
{
throw new ArgumentNullException(nameof(adToken));
}
IdentityProvider = Provider.AzureAD,
Token = adToken
};
}

/// <summary>
/// Creates <see cref="Credentials"/> based on a Facebook login.
/// </summary>
/// <param name="token">A Json Web Token, obtained by logging into Facebook.</param>
/// <returns>An instance of <see cref="Credentials"/> that can be used in <see cref="User.LoginAsync"/></returns>
public static Credentials JWT(string token)
{
Argument.NotNull(token, nameof(token));

return new Credentials { IdentityProvider = Provider.AzureAD, Token = adToken };
return new Credentials
{
IdentityProvider = Provider.JWT,
Token = token
};
}

internal static Credentials AdminToken(string token)
Expand Down
1 change: 1 addition & 0 deletions Settings.StyleCop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Value>Xamarin</Value>
<Value>deallocated</Value>
<Value>nullable</Value>
<Value>Json</Value>
</CollectionProperty>
</GlobalSettings>
<Analyzers>
Expand Down

0 comments on commit b499b88

Please sign in to comment.