Skip to content

Commit bfba2a2

Browse files
committed
Fixing some .NET 4.5 compat issues
1 parent 5c1944f commit bfba2a2

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseTokenVerifier.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ internal sealed class FirebaseTokenVerifier
3737
private const string FirebaseAudience ="https://identitytoolkit.googleapis.com/"
3838
+ "google.identity.identitytoolkit.v1.IdentityToolkit";
3939

40+
// See http://oid-info.com/get/2.16.840.1.101.3.4.2.1
41+
private const string Sha256Oid = "2.16.840.1.101.3.4.2.1";
42+
4043
private static readonly IReadOnlyList<string> StandardClaims =
4144
ImmutableList.Create<string>("iss", "aud", "exp", "iat", "sub", "uid");
4245

@@ -172,8 +175,16 @@ private async Task VerifySignatureAsync(
172175
var keys = await _keySource.GetPublicKeysAsync(cancellationToken)
173176
.ConfigureAwait(false);
174177
var verified = keys.Any(key =>
178+
#if NETSTANDARD1_5 || NETSTANDARD2_0
175179
key.Id == keyId && key.RSA.VerifyHash(
176-
hash, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
180+
hash, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)
181+
#elif NET45
182+
key.Id == keyId &&
183+
((RSACryptoServiceProvider) key.RSA).VerifyHash(hash, Sha256Oid, signature)
184+
#else
185+
#error Unsupported target
186+
#endif
187+
);
177188
if (!verified)
178189
{
179190
throw new FirebaseException($"Failed to verify {_shortName} signature.");

FirebaseAdmin/FirebaseAdmin/Auth/HttpPublicKeySource.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using System.Collections.Immutable;
1818
using System.IO;
1919
using System.Net.Http;
20-
using System.Security.Cryptography;
2120
using System.Security.Cryptography.X509Certificates;
2221
using System.Text;
2322
using System.Threading;
@@ -26,6 +25,14 @@
2625
using Google.Apis.Http;
2726
using Google.Apis.Util;
2827

28+
#if NETSTANDARD1_5 || NETSTANDARD2_0
29+
using RSAKey = System.Security.Cryptography.RSA;
30+
#elif NET45
31+
using RSAKey = System.Security.Cryptography.RSACryptoServiceProvider;
32+
#else
33+
#error Unsupported target
34+
#endif
35+
2936
namespace FirebaseAdmin.Auth
3037
{
3138
/// <summary>
@@ -104,7 +111,14 @@ private IReadOnlyList<PublicKey> ParseKeys(string json)
104111
foreach (var entry in rawKeys)
105112
{
106113
var x509cert = new X509Certificate2(Encoding.UTF8.GetBytes(entry.Value));
107-
var rsa = x509cert.GetRSAPublicKey();
114+
RSAKey rsa;
115+
#if NETSTANDARD1_5 || NETSTANDARD2_0
116+
rsa = x509cert.GetRSAPublicKey();
117+
#elif NET45
118+
rsa = (RSAKey) x509cert.PublicKey.Key;
119+
#else
120+
#error Unsupported target
121+
#endif
108122
builder.Add(new PublicKey(entry.Key, rsa));
109123
}
110124
return builder.ToImmutableList();

FirebaseAdmin/FirebaseAdmin/Auth/PublicKey.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using System.Security.Cryptography;
15+
#if NETSTANDARD1_5 || NETSTANDARD2_0
16+
using RSAKey = System.Security.Cryptography.RSA;
17+
#elif NET45
18+
using RSAKey = System.Security.Cryptography.RSACryptoServiceProvider;
19+
#else
20+
#error Unsupported target
21+
#endif
1622

1723
namespace FirebaseAdmin.Auth
1824
{
@@ -27,12 +33,12 @@ internal sealed class PublicKey
2733
public string Id { get; }
2834

2935
/// <summary>
30-
/// A <see cref="System.Security.Cryptography.RSA"/> instance containing the contents of
36+
/// A <see cref="RSAKey"/> instance containing the contents of
3137
/// the public key.
3238
/// </summary>
33-
public RSA RSA { get; }
39+
public RSAKey RSA { get; }
3440

35-
public PublicKey(string keyId, RSA rsa)
41+
public PublicKey(string keyId, RSAKey rsa)
3642
{
3743
Id = keyId;
3844
RSA = rsa;

0 commit comments

Comments
 (0)