Skip to content

Commit

Permalink
Fix ios warnings authgear#44
Browse files Browse the repository at this point in the history
  • Loading branch information
roxk committed Jun 2, 2022
1 parent dddd89e commit 24cf74e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Authgear.Xamarin/AuthgearException.netstandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Authgear.Xamarin
{
public partial class AuthgearException
{
internal static Exception? PlatformWrap(Exception ex)
internal static Exception? PlatformWrap(Exception _)
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion Authgear.Xamarin/AuthgearSdk.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class AuthgearSdk
/// </summary>
/// <param name="app">Dummy tag argument to denote this constructor is for ios</param>
/// <param name="options"></param>
public AuthgearSdk(UIApplication app, AuthgearOptions options) : this(options)
public AuthgearSdk(UIApplication _, AuthgearOptions options) : this(options)
{
biometric = new Biometric();
keyRepo = new KeyRepo();
Expand Down
6 changes: 4 additions & 2 deletions Authgear.Xamarin/Data/Biometric.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public Task<string> AuthenticateBiometricAsync(BiometricOptions options, string
KeyType = SecKeyType.RSA,
ApplicationTag = tag,
};
var secKeyObject = SecKeyChain.QueryAsConcreteType(record, out var result);
var secKeyObjectOpt = SecKeyChain.QueryAsConcreteType(record, out var result);
if (result != SecStatusCode.Success)
{
throw AuthgearException.Wrap(new BiometricIosException(result));
}
var secKeyObject = secKeyObjectOpt!;
try
{
var secKey = (SecKey)secKeyObject;
Expand Down Expand Up @@ -90,11 +91,12 @@ public async Task<BiometricEnableResult> EnableBiometricAsync(BiometricOptions o
KeyType = SecKeyType.RSA,
KeySizeInBits = KeySize
};
var secKey = SecKey.CreateRandomKey(keyGenParameters.Dictionary, out error);
var secKeyOpt = SecKey.CreateRandomKey(keyGenParameters.Dictionary, out error);
if (error != null)
{
throw AuthgearException.Wrap(new BiometricIosException(error));
}
var secKey = secKeyOpt!;
var accessControl = new SecAccessControl(SecAccessible.WhenPasscodeSetThisDeviceOnly, flags);
var record = new SecRecord(secKey)
{
Expand Down
6 changes: 4 additions & 2 deletions Authgear.Xamarin/Data/KeyRepo.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ private static SecKey GeneratePrivateKey(string tag)
KeyType = SecKeyType.RSA,
KeySizeInBits = KeySize
};
var privateKey = SecKey.CreateRandomKey(keyGenParam.Dictionary, out var error);
var privateKeyOpt = SecKey.CreateRandomKey(keyGenParam.Dictionary, out var error);
if (error != null)
{
throw AuthgearException.Wrap(new AnonymousUserIosException(error));
}
var privateKey = privateKeyOpt!;
var secRecord = new SecRecord(privateKey)
{
ApplicationTag = tag
Expand All @@ -95,13 +96,14 @@ private static SecKey GeneratePrivateKey(string tag)
KeyType = SecKeyType.RSA,
ApplicationTag = tag
};
var privateKey = SecKeyChain.QueryAsConcreteType(secRecord, out var result);
var privateKeyOpt = SecKeyChain.QueryAsConcreteType(secRecord, out var result);
if (result != SecStatusCode.Success)
{
return null;
}
try
{
var privateKey = privateKeyOpt!;
return (SecKey)privateKey;
}
catch
Expand Down
4 changes: 2 additions & 2 deletions Authgear.Xamarin/Jwk.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal partial class Jwk
{
public static Jwk FromPrivateKey(string kid, SecKey secKey)
{
var publicKey = secKey.GetPublicKey();
var data = publicKey.GetExternalRepresentation();
var publicKey = secKey.GetPublicKey()!;
var data = publicKey.GetExternalRepresentation()!;
var size = data.Length;
// Copy and pasted from flutter. TODO: Document what these magic numbers are.
var modulus = data.Subdata(new NSRange(size > 269 ? 9 : 8, 256));
Expand Down
26 changes: 12 additions & 14 deletions Authgear.Xamarin/Jwt.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ public static string Sign(SecKey privateKey, JwtHeader header, JwtPayload payloa
{
return Sign(header, payload, (input) =>
{
using (var sha256 = SHA256.Create())
using var sha256 = SHA256.Create();
sha256.Initialize();
var hash = sha256.ComputeHash(input);
var hashNsData = NSData.FromArray(hash);
var signedNsData = privateKey.CreateSignature(SecKeyAlgorithm.RsaSignatureDigestPkcs1v15Sha256, hashNsData, out var error);
if (error != null)
{
sha256.Initialize();
var hash = sha256.ComputeHash(input);
var hashNsData = NSData.FromArray(hash);
var signedNsData = privateKey.CreateSignature(SecKeyAlgorithm.RsaSignatureDigestPkcs1v15Sha256, hashNsData, out var error);
if (error != null)
{
throw new BiometricIosException(error);
}
// According to the following ref, this is faster than .ToArray()
// https://stackoverflow.com/questions/6239636/how-to-go-from-nsdata-to-byte?msclkid=6c02bbb8d11b11ecab5688675cf7d0a3
var signedData = new byte[signedNsData.Length];
System.Runtime.InteropServices.Marshal.Copy(signedNsData.Bytes, signedData, 0, Convert.ToInt32(signedNsData.Length));
return signedData;
throw new BiometricIosException(error);
}
// According to the following ref, this is faster than .ToArray()
// https://stackoverflow.com/questions/6239636/how-to-go-from-nsdata-to-byte?msclkid=6c02bbb8d11b11ecab5688675cf7d0a3
var signedData = new byte[signedNsData!.Length];
System.Runtime.InteropServices.Marshal.Copy(signedNsData.Bytes, signedData, 0, Convert.ToInt32(signedData.Length));
return signedData;
});
}
}
Expand Down

0 comments on commit 24cf74e

Please sign in to comment.