Skip to content

Commit af9e80b

Browse files
committed
Merged with hkj-stylecop
2 parents b901e62 + 65905e7 commit af9e80b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1571
-798
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ matrix:
88
script:
99
- dotnet build FirebaseAdmin/FirebaseAdmin
1010
- dotnet build FirebaseAdmin/FirebaseAdmin.Snippets
11+
- dotnet build FirebaseAdmin/FirebaseAdmin.IntegrationTests
1112
- dotnet test FirebaseAdmin/FirebaseAdmin.Tests

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Unreleased
22

3+
-
4+
5+
# v1.1.0
6+
7+
- [added] Implemented the `SetCustomUserClaimsAsync()` API in the
8+
`FirebaseAuth` class.
9+
310
# v1.0.0
411

512
- [added] Initial release of the Admin .NET SDK. See
@@ -13,4 +20,4 @@
1320

1421
- [added] The initial release includes the `CreateCustomTokenAsync()`,
1522
and `VerifyIdTokenAsync()` methods for minting custom
16-
authentication tokens and verifying Firebase ID tokens.
23+
authentication tokens and verifying Firebase ID tokens.

FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAdmin.IntegrationTests.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.0</TargetFramework>
5-
65
<IsPackable>false</IsPackable>
6+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
7+
<CodeAnalysisRuleSet>../../stylecop_test.ruleset</CodeAnalysisRuleSet>
78
</PropertyGroup>
89

910
<ItemGroup>
@@ -12,6 +13,9 @@
1213
<PackageReference Include="xunit" Version="2.3.1" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
1415
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
16+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-beta.61">
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
1519
</ItemGroup>
1620

1721
<ItemGroup>

FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAuthTest.cs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717
using System.Net.Http;
1818
using System.Text;
1919
using System.Threading.Tasks;
20-
using Xunit;
20+
using FirebaseAdmin;
2121
using FirebaseAdmin.Auth;
2222
using Google.Apis.Auth.OAuth2;
2323
using Google.Apis.Util;
24+
using Xunit;
2425

2526
namespace FirebaseAdmin.IntegrationTests
2627
{
2728
public class FirebaseAuthTest
2829
{
29-
private const string VerifyCustomTokenUrl =
30+
private const string VerifyCustomTokenUrl =
3031
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken";
3132

3233
public FirebaseAuthTest()
3334
{
3435
IntegrationTestUtils.EnsureDefaultApp();
3536
}
36-
37+
3738
[Fact]
3839
public async Task CreateCustomToken()
3940
{
@@ -49,9 +50,9 @@ public async Task CreateCustomTokenWithClaims()
4950
{
5051
var developerClaims = new Dictionary<string, object>()
5152
{
52-
{"admin", true},
53-
{"package", "gold"},
54-
{"magicNumber", 42L},
53+
{ "admin", true },
54+
{ "package", "gold" },
55+
{ "magicNumber", 42L },
5556
};
5657
var customToken = await FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(
5758
"testuser", developerClaims);
@@ -71,13 +72,14 @@ public async Task CreateCustomTokenWithClaims()
7172
public async Task CreateCustomTokenWithoutServiceAccount()
7273
{
7374
var googleCred = FirebaseApp.DefaultInstance.Options.Credential;
74-
var serviceAcct = (ServiceAccountCredential) googleCred.UnderlyingCredential;
75-
var token = await ((ITokenAccess) googleCred).GetAccessTokenForRequestAsync();
76-
var app = FirebaseApp.Create(new AppOptions()
77-
{
78-
Credential = GoogleCredential.FromAccessToken(token),
79-
ServiceAccountId = serviceAcct.Id,
80-
}, "IAMSignApp");
75+
var serviceAcct = (ServiceAccountCredential)googleCred.UnderlyingCredential;
76+
var token = await ((ITokenAccess)googleCred).GetAccessTokenForRequestAsync();
77+
var app = FirebaseApp.Create(
78+
new AppOptions()
79+
{
80+
Credential = GoogleCredential.FromAccessToken(token),
81+
ServiceAccountId = serviceAcct.Id,
82+
}, "IAMSignApp");
8183
try
8284
{
8385
var customToken = await FirebaseAuth.GetAuth(app).CreateCustomTokenAsync(
@@ -92,17 +94,46 @@ public async Task CreateCustomTokenWithoutServiceAccount()
9294
}
9395
}
9496

97+
[Fact]
98+
public async Task SetCustomUserClaims()
99+
{
100+
var customClaims = new Dictionary<string, object>()
101+
{
102+
{ "admin", true },
103+
};
104+
105+
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync("testuser", customClaims);
106+
}
107+
108+
[Fact]
109+
public async Task SetCustomUserClaimsWithEmptyClaims()
110+
{
111+
var customClaims = new Dictionary<string, object>();
112+
113+
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync("testuser", customClaims);
114+
}
115+
116+
[Fact]
117+
public async Task SetCustomUserClaimsWithWrongUid()
118+
{
119+
var customClaims = new Dictionary<string, object>();
120+
121+
await Assert.ThrowsAsync<FirebaseException>(
122+
async () => await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync("mock-uid", customClaims));
123+
}
124+
95125
private static async Task<string> SignInWithCustomTokenAsync(string customToken)
96126
{
97127
var rb = new Google.Apis.Requests.RequestBuilder()
98128
{
99129
Method = Google.Apis.Http.HttpConsts.Post,
100-
BaseUri = new Uri(VerifyCustomTokenUrl),
130+
BaseUri = new Uri(VerifyCustomTokenUrl),
101131
};
102132
rb.AddParameter(RequestParameterType.Query, "key", IntegrationTestUtils.GetApiKey());
103133
var request = rb.CreateRequest();
104134
var jsonSerializer = Google.Apis.Json.NewtonsoftJsonSerializer.Instance;
105-
var payload = jsonSerializer.Serialize(new SignInRequest{
135+
var payload = jsonSerializer.Serialize(new SignInRequest
136+
{
106137
CustomToken = customToken,
107138
ReturnSecureToken = true,
108139
});
@@ -130,6 +161,6 @@ internal class SignInRequest
130161
internal class SignInResponse
131162
{
132163
[Newtonsoft.Json.JsonProperty("idToken")]
133-
public String IdToken { get; set; }
164+
public string IdToken { get; set; }
134165
}
135166
}

FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ internal static class IntegrationTestUtils
2626
private const string ServiceAccountFile = "./resources/integration_cert.json";
2727
private const string ApiKeyFile = "./resources/integration_apikey.txt";
2828

29-
private static readonly Lazy<FirebaseApp> DefaultFirebaseApp = new Lazy<FirebaseApp>(() => {
30-
var options = new AppOptions()
29+
private static readonly Lazy<FirebaseApp> DefaultFirebaseApp = new Lazy<FirebaseApp>(
30+
() =>
3131
{
32-
Credential = GoogleCredential.FromFile(ServiceAccountFile),
33-
};
34-
return FirebaseApp.Create(options);
35-
}, true);
32+
var options = new AppOptions()
33+
{
34+
Credential = GoogleCredential.FromFile(ServiceAccountFile),
35+
};
36+
return FirebaseApp.Create(options);
37+
}, true);
3638

3739
public static FirebaseApp EnsureDefaultApp()
3840
{

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAdmin.Snippets.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.0</TargetFramework>
55
<IsPackable>false</IsPackable>
6+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
7+
<CodeAnalysisRuleSet>../../stylecop_test.ruleset</CodeAnalysisRuleSet>
68
</PropertyGroup>
79

810
<ItemGroup>
@@ -11,6 +13,9 @@
1113

1214
<ItemGroup>
1315
<ProjectReference Include="..\FirebaseAdmin\FirebaseAdmin.csproj" />
16+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-beta.61">
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
1419
</ItemGroup>
1520

1621
</Project>

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAppSnippets.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
namespace FirebaseAdmin.Snippets
2323
{
24-
class FirebaseAppSnippets
24+
internal class FirebaseAppSnippets
2525
{
26-
static void InitSdkWithServiceAccount()
26+
internal static void InitSdkWithServiceAccount()
2727
{
2828
// [START initialize_sdk_with_service_account]
2929
FirebaseApp.Create(new AppOptions()
@@ -33,7 +33,7 @@ static void InitSdkWithServiceAccount()
3333
// [END initialize_sdk_with_service_account]
3434
}
3535

36-
static void InitSdkWithApplicationDefault()
36+
internal static void InitSdkWithApplicationDefault()
3737
{
3838
// [START initialize_sdk_with_application_default]
3939
FirebaseApp.Create(new AppOptions()
@@ -43,7 +43,7 @@ static void InitSdkWithApplicationDefault()
4343
// [END initialize_sdk_with_application_default]
4444
}
4545

46-
static void InitSdkWithRefreshToken()
46+
internal static void InitSdkWithRefreshToken()
4747
{
4848
// [START initialize_sdk_with_refresh_token]
4949
FirebaseApp.Create(new AppOptions()
@@ -53,14 +53,14 @@ static void InitSdkWithRefreshToken()
5353
// [END initialize_sdk_with_refresh_token]
5454
}
5555

56-
static void InitSdkWithDefaultConfig()
56+
internal static void InitSdkWithDefaultConfig()
5757
{
5858
// [START initialize_sdk_with_default_config]
5959
FirebaseApp.Create();
6060
// [END initialize_sdk_with_default_config]
6161
}
6262

63-
static void InitDefaultApp()
63+
internal static void InitDefaultApp()
6464
{
6565
// [START access_services_default]
6666
// Initialize the default app
@@ -78,7 +78,7 @@ static void InitDefaultApp()
7878
// [END access_services_default]
7979
}
8080

81-
static void InitCustomApp()
81+
internal static void InitCustomApp()
8282
{
8383
var defaultOptions = new AppOptions()
8484
{
@@ -107,7 +107,7 @@ static void InitCustomApp()
107107
// [END access_services_nondefault]
108108
}
109109

110-
static void InitWithServiceAccountId()
110+
internal static void InitWithServiceAccountId()
111111
{
112112
// [START initialize_sdk_with_service_account_id]
113113
FirebaseApp.Create(new AppOptions()

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAuthSnippets.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
namespace FirebaseAdmin.Snippets
2121
{
22-
class FirebaseAuthSnippets
22+
internal class FirebaseAuthSnippets
2323
{
24-
static async Task CreateCustomTokenAsync()
24+
internal static async Task CreateCustomTokenAsync()
2525
{
2626
// [START custom_token]
2727
var uid = "some-uid";
@@ -32,7 +32,7 @@ static async Task CreateCustomTokenAsync()
3232
Console.WriteLine("Created custom token: {0}", customToken);
3333
}
3434

35-
static async Task CreateCustomTokenWithClaimsAsync()
35+
internal static async Task CreateCustomTokenWithClaimsAsync()
3636
{
3737
// [START custom_token_with_claims]
3838
var uid = "some-uid";
@@ -48,7 +48,7 @@ static async Task CreateCustomTokenWithClaimsAsync()
4848
Console.WriteLine("Created custom token: {0}", customToken);
4949
}
5050

51-
static async Task VeridyIdTokenAsync(string idToken)
51+
internal static async Task VeridyIdTokenAsync(string idToken)
5252
{
5353
// [START verify_id_token]
5454
FirebaseToken decodedToken = await FirebaseAuth.DefaultInstance

0 commit comments

Comments
 (0)