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

Commit

Permalink
Merge pull request #902 from cocoa-mhlw/feature/jwt-error
Browse files Browse the repository at this point in the history
Fixed to retry if there is an error in Payload
  • Loading branch information
cocoa-dev004 authored Mar 8, 2022
2 parents 2a78ae6 + 22ac59a commit 993b59d
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 4 deletions.
26 changes: 26 additions & 0 deletions COPYRIGHT_THIRD_PARTY_SOFTWARE_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,32 @@ of the input file used when generating it. This code is not
standalone and requires a support library to be linked with it. This
support library is itself covered by the above license.

---
## System.IdentityModel.Tokens.Jwt
---

The MIT License (MIT)

Copyright (c) Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---
## (Sample Code) Exposure Notifications API: Android Reference Design
---
Expand Down
20 changes: 20 additions & 0 deletions Covid19Radar/Covid19Radar.Android/Assets/license.html
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,26 @@ <h2>
standalone and requires a support library to be linked with it. This
support library is itself covered by the above license.</p>
<hr>
<h2 id="toc_18">System.IdentityModel.Tokens.Jwt</h2>
<hr>
<p>The MIT License (MIT)</p>
<p>Copyright (c) Microsoft Corporation</p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the &quot;Software&quot;), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
<hr>
<h2>
<a id="user-content-sample-code-exposure-notifications-api-android-reference-design" class="anchor" href="#sample-code-exposure-notifications-api-android-reference-design" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>(Sample Code) Exposure Notifications API: Android Reference Design</h2>
<hr>
Expand Down
3 changes: 3 additions & 0 deletions Covid19Radar/Covid19Radar.Android/Covid19Radar.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
<PackageReference Include="Chino.Android">
<Version>1.0.0-rc11</Version>
</PackageReference>
<PackageReference Include="System.IdentityModel.Tokens.Jwt">
<Version>6.16.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
Expand Down
22 changes: 22 additions & 0 deletions Covid19Radar/Covid19Radar.Android/Services/DeviceCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* 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.IdentityModel.Tokens.Jwt;
using System.Threading.Tasks;
using Android.Gms.SafetyNet;
using Covid19Radar.Common;
using Covid19Radar.Model;
using Covid19Radar.Services;
using Newtonsoft.Json.Linq;

namespace Covid19Radar.Droid.Services
{
Expand All @@ -24,6 +26,26 @@ public Task<string> VerifyAsync(V1EventLogRequest eventLogRequest)
return GetSafetyNetAttestationAsync(nonce);
}

public bool IsErrorPayload(string token)
{
try
{
var jwt = new JwtSecurityToken(token);
var payloadJson = jwt.Payload.SerializeToJson();

var error = JObject.Parse(payloadJson).Value<string>("error");
if (!string.IsNullOrEmpty(error))
{
return true;
}
}
catch
{
return true;
}
return false;
}

/// <summary>
/// Verification device information required for positive submissions
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions Covid19Radar/Covid19Radar.iOS/Resources/license.html
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,26 @@ <h2>
standalone and requires a support library to be linked with it. This
support library is itself covered by the above license.</p>
<hr>
<h2 id="toc_18">System.IdentityModel.Tokens.Jwt</h2>
<hr>
<p>The MIT License (MIT)</p>
<p>Copyright (c) Microsoft Corporation</p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the &quot;Software&quot;), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
<hr>
<h2>
<a id="user-content-sample-code-exposure-notifications-api-android-reference-design" class="anchor" href="#sample-code-exposure-notifications-api-android-reference-design" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>(Sample Code) Exposure Notifications API: Android Reference Design</h2>
<hr>
Expand Down
7 changes: 5 additions & 2 deletions Covid19Radar/Covid19Radar.iOS/Services/DeviceCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public async Task<string> VerifyAsync(V1EventLogRequest _)
var token = await DeviceCheck.DCDevice.CurrentDevice.GenerateTokenAsync();
return Convert.ToBase64String(token.ToArray());
}
}
}

public bool IsErrorPayload(string token)
=> false;
}
}
12 changes: 12 additions & 0 deletions Covid19Radar/Covid19Radar/Resources/AppResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Covid19Radar/Covid19Radar/Resources/AppResources.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1274,4 +1274,13 @@
<comment>TODO:{0:#}年{1:#}月 午前{2:#}日{3:#}時</comment>
</data>

<data name="GeneralErrorTitle" xml:space="preserve">
<value>エラーが発生しました</value>
<comment>エラーが発生しました</comment>
</data>
<data name="GeneralErrorRegisterAgainMessage" xml:space="preserve">
<value>時間をおいてから再度登録を行ってください。</value>
<comment>時間をおいてから再度登録を行ってください。</comment>
</data>

</root>
9 changes: 9 additions & 0 deletions Covid19Radar/Covid19Radar/Resources/AppResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1385,4 +1385,13 @@ Note: this app does not collect users’ location information.</value>
<comment>TODO:{0:#}年{1:#}月{2:#}日 午前{3:#}時</comment>
</data>

<data name="GeneralErrorTitle" xml:space="preserve">
<value>An error has occurred</value>
<comment>エラーが発生しました</comment>
</data>
<data name="GeneralErrorRegisterAgainMessage" xml:space="preserve">
<value>Please try to register again after a while.</value>
<comment>時間をおいてから再度登録を行ってください。</comment>
</data>

</root>
9 changes: 9 additions & 0 deletions Covid19Radar/Covid19Radar/Resources/AppResources.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1283,4 +1283,13 @@ However, if you have any symptoms, please consider receiving a consultation..
<comment>TODO:{0:#}年{1:#}月{2:#}日 午前{3:#}時</comment>
</data>

<data name="GeneralErrorTitle" xml:space="preserve">
<value>发生错误</value>
<comment>エラーが発生しました</comment>
</data>
<data name="GeneralErrorRegisterAgainMessage" xml:space="preserve">
<value>请稍后再登记</value>
<comment>時間をおいてから再度登録を行ってください。</comment>
</data>

</root>
3 changes: 3 additions & 0 deletions Covid19Radar/Covid19Radar/Services/DeviceVerifierMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public Task<string> VerifyAsync(DiagnosisSubmissionParameter _)

public Task<string> VerifyAsync(V1EventLogRequest _)
=> Task.Run(() => "DUMMY RESPONSE");

public bool IsErrorPayload(string payload)
=> false;
}
}
41 changes: 40 additions & 1 deletion Covid19Radar/Covid19Radar/Services/DiagnosisKeyRegisterServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,30 @@ string idempotencyKey
Padding = padding
};

submission.DeviceVerificationPayload = await _deviceVerifier.VerifyAsync(submission);
// Create device verification payload
var tries = 0;
var delay = 4 * 1000;
while (true) {
var deviceVerificationPayload = await _deviceVerifier.VerifyAsync(submission);
if (!_deviceVerifier.IsErrorPayload(deviceVerificationPayload))
{
_loggerService.Info("Payload creation successful.");
submission.DeviceVerificationPayload = deviceVerificationPayload;
break;
}
else if (tries >= 3)
{
_loggerService.Error("Payload creation failed all.");
throw new DiagnosisKeyRegisterException(DiagnosisKeyRegisterException.Codes.FailedCreatePayload);
}

_loggerService.Warning($"Payload creation failed. {tries + 1} time(s).");
_loggerService.Info($"delay {delay} msec");
await Task.Delay(delay);
delay *= 2;

tries++;
}

_loggerService.Info($"DeviceVerificationPayload is {(string.IsNullOrEmpty(submission.DeviceVerificationPayload) ? "null or empty" : "set")}.");
_loggerService.Info($"VerificationPayload is {(string.IsNullOrEmpty(submission.VerificationPayload) ? "null or empty" : "set")}.");
Expand All @@ -131,4 +154,20 @@ private static string GetPadding(Random random = null)
return Convert.ToBase64String(padding);
}
}

public class DiagnosisKeyRegisterException : Exception
{
private const string DataKeyCode = "code";

public enum Codes {
FailedCreatePayload,
}

public Codes Code => (Codes)Data[DataKeyCode];

public DiagnosisKeyRegisterException(Codes code) : base("Failed to register the diagnostic key.")
{
Data[DataKeyCode] = code;
}
}
}
4 changes: 3 additions & 1 deletion Covid19Radar/Covid19Radar/Services/IDeviceVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Covid19Radar.Services
/// </summary>
/// <returns>Device Verification Payload</returns>
/// <remarks>
/// see deviceVerificationPayload
/// see deviceVerificationPayload
/// https://github.com/google/exposure-notifications-server/blob/master/docs/server_functional_requirements.md
/// </remarks>

Expand All @@ -21,5 +21,7 @@ public interface IDeviceVerifier
Task<string> VerifyAsync(DiagnosisSubmissionParameter submission);

Task<string> VerifyAsync(V1EventLogRequest eventLogRequest);

bool IsErrorPayload(string token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,22 @@ await UserDialogs.Instance.AlertAsync(
}

}
catch (DiagnosisKeyRegisterException ex)
{
loggerService.Exception("Failed to register", ex);

errorCount++;
UserDialogs.Instance.HideLoading();

if (ex.Code == DiagnosisKeyRegisterException.Codes.FailedCreatePayload)
{
await UserDialogs.Instance.AlertAsync(
AppResources.GeneralErrorRegisterAgainMessage,
AppResources.GeneralErrorTitle,
AppResources.ButtonOk
);
}
}
catch (Exception ex)
{
errorCount++;
Expand Down

0 comments on commit 993b59d

Please sign in to comment.