Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Refactoring/optimization2 (#114) #222

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down
48 changes: 33 additions & 15 deletions Covid19Radar/Covid19Radar.Android/Services/DeviceCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,63 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Covid19Radar.Services;
using Android.Gms.SafetyNet;
using Covid19Radar.Droid.Services;
using Xamarin.Forms;
using Covid19Radar.Model;
using Covid19Radar.Common;
using Android.Gms.SafetyNet;
using Covid19Radar.Services;
using Xamarin.Forms;

[assembly: Dependency(typeof(DeviceCheckService))]
namespace Covid19Radar.Droid.Services
{
public class DeviceCheckService : IDeviceVerifier
{

public Task<string> VerifyAsync(DiagnosisSubmissionParameter submission)
{
var nonce = submission.GetAndroidNonce();
var nonce = GetNonce(submission);
return GetSafetyNetAttestationAsync(nonce);
}

/// <summary>
/// Verification device information required for positive submissions
/// </summary>
/// <returns>Device Verification Payload</returns>
async Task<string> GetSafetyNetAttestationAsync(byte[] nonce)
private static async Task<string> GetSafetyNetAttestationAsync(byte[] nonce)
{
using var client = SafetyNetClass.GetClient(Android.App.Application.Context);
using var response = await client.AttestAsync(nonce, AppSettings.Instance.AndroidSafetyNetApiKey);
return response.JwsResult;
}

private static byte[] GetNonce(DiagnosisSubmissionParameter submission)
{
var cleartext = GetNonceClearText(submission);
var nonce = GetSha256(cleartext);
return nonce;

static string GetNonceClearText(DiagnosisSubmissionParameter submission) =>
string.Join("|", submission.AppPackageName, GetKeyString(submission.Keys), GetRegionString(submission.Regions), submission.VerificationPayload);

static string GetKeyString(IEnumerable<DiagnosisSubmissionParameter.Key> keys) =>
string.Join(",", keys.OrderBy(k => k.KeyData).Select(k => GetKeyStringCore(k)));

static string GetKeyStringCore(DiagnosisSubmissionParameter.Key k) =>
string.Join(".", k.KeyData, k.RollingStartNumber, k.RollingPeriod);

static string GetRegionString(IEnumerable<string> regions) =>
string.Join(",", regions.Select(r => r.ToUpperInvariant()).OrderBy(r => r));

static byte[] GetSha256(string text)
{
using var sha = SHA256.Create();
var textBytes = Encoding.UTF8.GetBytes(text);
return sha.ComputeHash(textBytes);
}
}
}
}
}
22 changes: 11 additions & 11 deletions Covid19Radar/Covid19Radar/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System;
using System.Threading.Tasks;
using CommonServiceLocator;
using Covid19Radar.Common;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using Covid19Radar.ViewModels;
using Covid19Radar.Views;
using DryIoc;
using Prism;
using Prism.DryIoc;
using Prism.Ioc;
using Covid19Radar.ViewModels;
using Covid19Radar.Views;
using Prism.Navigation;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using DryIoc;
using System.Threading.Tasks;
using Prism.Navigation;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using System;
using CommonServiceLocator;
using Covid19Radar.Common;

/*
* Our mission...is
Expand Down
3 changes: 0 additions & 3 deletions Covid19Radar/Covid19Radar/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,5 @@ public AppSettings()
public string LogStorageEndpoint { get; }
public string LogStorageContainerName { get; }
public string LogStorageAccountName { get; }

internal Dictionary<string, ulong> GetDefaultBatch() =>
Instance.SupportedRegions.ToDictionary(r => r, r => (ulong)0);
}
}
11 changes: 2 additions & 9 deletions Covid19Radar/Covid19Radar/Common/AppConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@ namespace Covid19Radar.Common
{
public static class AppConstants
{
public static readonly int NumberOfGroup = 86400;
/// <summary>
/// Number of days covered from the date of diagnosis or onset
/// </summary>
public const int DaysToSendTek = -3;
/// <summary>
/// Cache Timeout
/// </summary>
public const int CacheTimeout = 60;
/// <summary>
/// Active Rolling Period
/// </summary>
public const uint ActiveRollingPeriod = 144;

/// <summary>
/// Max Error Count
/// </summary>
public const int MaxErrorCount = 3;

/// <summary>
/// Max diagnosis UID Count
/// </summary>
Expand Down
10 changes: 1 addition & 9 deletions Covid19Radar/Covid19Radar/Common/AppUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ namespace Covid19Radar.Common
{
static class AppUtils
{
public static async void CheckPermission()
{
var status = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
if (status != PermissionStatus.Granted)
{
status = await Permissions.RequestAsync<Permissions.LocationAlways>();
}
}
public static async void PopUpShare()
{
if (Device.RuntimePlatform == Device.iOS)
Expand Down Expand Up @@ -85,4 +77,4 @@ public static async void CheckVersion(ILoggerService loggerService)
}
}
}
}
}
8 changes: 7 additions & 1 deletion Covid19Radar/Covid19Radar/Common/DateTimeUtility.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using System;
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System;

namespace Covid19Radar.Common
{
public interface IDateTimeUtility
{
DateTime UtcNow { get; }
}

public class DateTimeUtility : IDateTimeUtility
{
public static IDateTimeUtility Instance = new DateTimeUtility();
Expand Down
66 changes: 0 additions & 66 deletions Covid19Radar/Covid19Radar/Common/HoursTimer.cs

This file was deleted.

33 changes: 0 additions & 33 deletions Covid19Radar/Covid19Radar/Common/JumpHash.cs

This file was deleted.

66 changes: 0 additions & 66 deletions Covid19Radar/Covid19Radar/Common/SecondsTimer.cs

This file was deleted.

5 changes: 0 additions & 5 deletions Covid19Radar/Covid19Radar/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System;
using Newtonsoft.Json;

namespace Covid19Radar.Common
{
public static class Utils
{
#region Other Public Methods

public static string SerializeToJson(object obj) => JsonConvert.SerializeObject(obj);

public static T DeserializeFromJson<T>(string jsonObj) => JsonConvert.DeserializeObject<T>(jsonObj);

public static DateTime JstNow()
{
return TimeZoneInfo.ConvertTime(DateTime.Now, JstTimeZoneInfo());
Expand Down
7 changes: 1 addition & 6 deletions Covid19Radar/Covid19Radar/Controls/CustomDatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@

namespace Covid19Radar.Controls
{
public class CustomDatePicker : DatePicker
{
public CustomDatePicker()
{
}
}
public class CustomDatePicker : DatePicker { }
}
Loading