From 24cf74eae5aeb323d86e1ce52979ae76a79d8bdc Mon Sep 17 00:00:00 2001 From: Roxk Date: Thu, 2 Jun 2022 11:09:42 +0800 Subject: [PATCH] Fix ios warnings #44 --- .../AuthgearException.netstandard.cs | 2 +- Authgear.Xamarin/AuthgearSdk.ios.cs | 2 +- Authgear.Xamarin/Data/Biometric.ios.cs | 6 +++-- Authgear.Xamarin/Data/KeyRepo.ios.cs | 6 +++-- Authgear.Xamarin/Jwk.ios.cs | 4 +-- Authgear.Xamarin/Jwt.ios.cs | 26 +++++++++---------- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Authgear.Xamarin/AuthgearException.netstandard.cs b/Authgear.Xamarin/AuthgearException.netstandard.cs index f6bacb4..b225dd6 100644 --- a/Authgear.Xamarin/AuthgearException.netstandard.cs +++ b/Authgear.Xamarin/AuthgearException.netstandard.cs @@ -6,7 +6,7 @@ namespace Authgear.Xamarin { public partial class AuthgearException { - internal static Exception? PlatformWrap(Exception ex) + internal static Exception? PlatformWrap(Exception _) { return null; } diff --git a/Authgear.Xamarin/AuthgearSdk.ios.cs b/Authgear.Xamarin/AuthgearSdk.ios.cs index ef955de..8969730 100644 --- a/Authgear.Xamarin/AuthgearSdk.ios.cs +++ b/Authgear.Xamarin/AuthgearSdk.ios.cs @@ -13,7 +13,7 @@ public partial class AuthgearSdk /// /// Dummy tag argument to denote this constructor is for ios /// - public AuthgearSdk(UIApplication app, AuthgearOptions options) : this(options) + public AuthgearSdk(UIApplication _, AuthgearOptions options) : this(options) { biometric = new Biometric(); keyRepo = new KeyRepo(); diff --git a/Authgear.Xamarin/Data/Biometric.ios.cs b/Authgear.Xamarin/Data/Biometric.ios.cs index fee07dc..045251c 100644 --- a/Authgear.Xamarin/Data/Biometric.ios.cs +++ b/Authgear.Xamarin/Data/Biometric.ios.cs @@ -49,11 +49,12 @@ public Task 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; @@ -90,11 +91,12 @@ public async Task 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) { diff --git a/Authgear.Xamarin/Data/KeyRepo.ios.cs b/Authgear.Xamarin/Data/KeyRepo.ios.cs index 1407ba8..fc43d5a 100644 --- a/Authgear.Xamarin/Data/KeyRepo.ios.cs +++ b/Authgear.Xamarin/Data/KeyRepo.ios.cs @@ -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 @@ -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 diff --git a/Authgear.Xamarin/Jwk.ios.cs b/Authgear.Xamarin/Jwk.ios.cs index c945dbb..af5c111 100644 --- a/Authgear.Xamarin/Jwk.ios.cs +++ b/Authgear.Xamarin/Jwk.ios.cs @@ -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)); diff --git a/Authgear.Xamarin/Jwt.ios.cs b/Authgear.Xamarin/Jwt.ios.cs index 7a009ba..3acc9f1 100644 --- a/Authgear.Xamarin/Jwt.ios.cs +++ b/Authgear.Xamarin/Jwt.ios.cs @@ -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; }); } }