diff --git a/PrivacyKit/Http.swift b/PrivacyKit/Http.swift index 795d520..7c899f8 100644 --- a/PrivacyKit/Http.swift +++ b/PrivacyKit/Http.swift @@ -238,6 +238,15 @@ public class Http { case contentLength = "Content-Length" /// `Content-Type` case contentType = "Content-Type" + + /** + A header used to activating bad behaviour of a service provider. + This should be used for demonstration purposes only. + + - warning: Do not use this in production systems, as all requests + will be logged, if this header is set. + */ + case badProvider = "X-AppPETs-BadProvider" } /** diff --git a/PrivacyKit/KeyValueStorage.swift b/PrivacyKit/KeyValueStorage.swift index debe628..089f102 100644 --- a/PrivacyKit/KeyValueStorage.swift +++ b/PrivacyKit/KeyValueStorage.swift @@ -557,7 +557,7 @@ extension PrivacyService { Key-value storage backend of the P-Service. */ var keyValueStorageBackend: KeyValueStorageBackend { - return PrivacyService.KeyValueStorage(baseUrl: baseUrl) + return PrivacyService.KeyValueStorage(service: self) } /** @@ -566,26 +566,23 @@ extension PrivacyService { */ class KeyValueStorage { - /** - The base URL of the P-Service. - */ - let baseUrl: URL + weak var service: PrivacyService! = nil /** Initialize the key-value storage backend. - parameters: - - baseUrl: The base URL of the P-Service. + - service: The P-Service object. */ - init(baseUrl: URL) { - self.baseUrl = baseUrl + init(service: PrivacyService) { + self.service = service } /** The entry point of the key-value storage API. */ var entryPoint: URL { - return baseUrl + return service.baseUrl .appendingPathComponent("storage", isDirectory: true) .appendingPathComponent("v1", isDirectory: true) } @@ -640,6 +637,10 @@ extension PrivacyService.KeyValueStorage: KeyValueStorageBackend { request.set(method: .post) request.set(contentType: .octetStream) + if service.options.contains(.activateBadBehavior) { + request.add(value: "1", for: .badProvider) + } + Indicators.showNetworkActivity() let task = session.uploadTask(with: request, from: Data(value.bytes)) { @@ -687,6 +688,10 @@ extension PrivacyService.KeyValueStorage: KeyValueStorageBackend { request.set(method: .get) + if service.options.contains(.activateBadBehavior) { + request.add(value: "1", for: .badProvider) + } + Indicators.showNetworkActivity() let task = session.dataTask(with: request) { @@ -747,6 +752,10 @@ extension PrivacyService.KeyValueStorage: KeyValueStorageBackend { request.set(method: .delete) + if service.options.contains(.activateBadBehavior) { + request.add(value: "1", for: .badProvider) + } + Indicators.showNetworkActivity() let task = session.dataTask(with: request) { diff --git a/PrivacyKit/PrivacyService.swift b/PrivacyKit/PrivacyService.swift index 3921560..4c12750 100644 --- a/PrivacyKit/PrivacyService.swift +++ b/PrivacyKit/PrivacyService.swift @@ -3,19 +3,58 @@ */ public class PrivacyService { + /** + These options can be used to configure the behaviour of the service. + */ + public struct Options: OptionSet { + + /** + A bit representing a single option. + */ + public let rawValue: Int + + /** + Initialize a single option with a given value. + + - parameters: + - rawValue: The bit representing the option. + */ + public init(rawValue: Int) { + self.rawValue = rawValue + } + + /** + This option activates bad behaviour of the service. Every request + send to a service with activated bad behaviour is logged and + available in the visualization API. + + - warning: Do not use this in production system. This is meant for + demonstration purposes only. Every reqeust will be logged on the + server, including the user's IP address. + */ + static let activateBadBehavior = Options(rawValue: 1 << 0) + + } + /** The base URL of the P-Service. */ let baseUrl: URL + /** + Options that configure the behaviour of + */ + let options: Options + /** Initializes a `PrivacyService` instance. - parameters: - baseUrl: The base URL of the P-Service. */ - public init(baseUrl: URL) { + public init(baseUrl: URL, options: Options = []) { self.baseUrl = baseUrl + self.options = options } }