Skip to content

Commit

Permalink
Feature/context cron (eu-digital-green-certificates#70)
Browse files Browse the repository at this point in the history
* Fetch context every 24h.

* Resolve packages.

* Fix hashes.

* Fix pubkey adding.

* Clear context file when app version changes.
  • Loading branch information
yspreen authored May 15, 2021
1 parent b817b56 commit 9b61fe6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
"repositoryURL": "https://github.com/SCENEE/FloatingPanel",
"state": {
"branch": null,
"revision": "16fea625be25d9a713630a4a43cbc0778740ebf4",
"version": "2.3.0"
"revision": "da4668aa2b8277dd8ce2741ac5c39394431400fc",
"version": "2.3.1"
}
},
{
"package": "JSONSchema",
"repositoryURL": "https://github.com/jnewc/JSONSchema.swift",
"repositoryURL": "https://github.com/eu-digital-green-certificates/JSONSchema.swift",
"state": {
"branch": "master",
"revision": "4637ac1cf57745c29003738f36e1b6c232fbd1a6",
"revision": "4809e8a105b3fd2818a08caf9e3cbcdd7d319af2",
"version": null
}
},
Expand All @@ -42,8 +42,8 @@
"repositoryURL": "https://github.com/kylef/Spectre.git",
"state": {
"branch": null,
"revision": "f79d4ecbf8bc4e1579fbd86c3e1d652fb6876c53",
"version": "0.9.2"
"revision": "d02129a9af77729de049d328dd61e530b6f2bb2b",
"version": null
}
},
{
Expand Down
8 changes: 6 additions & 2 deletions DGCAVerifier/Models/LocalData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct LocalData: Codable {
}
}
var config = Config.load()
var lastLaunchedAppVersion = Self.appVersion

mutating func add(encodedPublicKey: String) {
let kid = KID.from(encodedPublicKey)
Expand All @@ -54,7 +55,7 @@ struct LocalData: Codable {
if list.contains(encodedPublicKey) {
return
}
encodedPublicKeys[kidStr]?.append(encodedPublicKey)
encodedPublicKeys[kidStr] = list + [encodedPublicKey]
}

static func set(resumeToken: String) {
Expand All @@ -69,11 +70,14 @@ struct LocalData: Codable {

static func initialize(completion: @escaping () -> Void) {
storage.loadOverride(fallback: LocalData.sharedInstance) { success in
guard let result = success else {
guard var result = success else {
return
}
let format = l10n("log.keys-loaded")
print(String.localizedStringWithFormat(format, result.encodedPublicKeys.count))
if result.lastLaunchedAppVersion != Self.appVersion {
result.config = LocalData.sharedInstance.config
}
LocalData.sharedInstance = result
completion()
GatewayConnection.fetchContext()
Expand Down
8 changes: 6 additions & 2 deletions DGCAVerifier/Services/GatewayConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ struct GatewayConnection: ContextConnection {
guard LocalData.sharedInstance.lastFetch.timeIntervalSinceNow < -24 * 60 * 60 else {
return
}
update()
fetchContext {
update()
}
}

static func update(completion: (() -> Void)? = nil) {
Expand Down Expand Up @@ -125,19 +127,21 @@ struct GatewayConnection: ContextConnection {
}
}

public static func fetchContext() {
public static func fetchContext(completion: (() -> Void)? = nil) {
request(
["context"]
).response {
guard
let data = $0.data,
let string = String(data: data, encoding: .utf8)
else {
completion?()
return
}
let json = JSON(parseJSONC: string)
LocalData.sharedInstance.config.merge(other: json)
LocalData.sharedInstance.save()
completion?()
}
}
static var config: JSON {
Expand Down
15 changes: 12 additions & 3 deletions context.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@
"context": {
// Currently this url is empty still, subject to change:
"url": "https://dgca-verifier-service.cfapps.eu10.hana.ondemand.com/context",
"pubKeys": ["Ef6tLK887tpTdkiVkSG7ioXCgNEJsbIgKcAU+dxTTag="]
"pubKeys": [
"lKdU1EbQubxyDDm2q3N8KclZ2C94Num3xXjG0pk+3eI=",
"r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E="
]
},
"endpoints": {
"status": {
"url": "https://dgca-verifier-service.cfapps.eu10.hana.ondemand.com/signercertificateStatus",
"pubKeys": ["Ef6tLK887tpTdkiVkSG7ioXCgNEJsbIgKcAU+dxTTag="]
"pubKeys": [
"lKdU1EbQubxyDDm2q3N8KclZ2C94Num3xXjG0pk+3eI=",
"r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E="
]
},
"update": {
"url": "https://dgca-verifier-service.cfapps.eu10.hana.ondemand.com/signercertificateUpdate",
"pubKeys": ["Ef6tLK887tpTdkiVkSG7ioXCgNEJsbIgKcAU+dxTTag="]
"pubKeys": [
"lKdU1EbQubxyDDm2q3N8KclZ2C94Num3xXjG0pk+3eI=",
"r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E="
]
}
}
},
Expand Down

0 comments on commit 9b61fe6

Please sign in to comment.