Skip to content

Commit

Permalink
Add configuration option to enable tracking for demonstration purposes
Browse files Browse the repository at this point in the history
This resolves #9.
  • Loading branch information
blochberger committed Sep 27, 2018
1 parent 6361cf0 commit d9c3ed5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
9 changes: 9 additions & 0 deletions PrivacyKit/Http.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
27 changes: 18 additions & 9 deletions PrivacyKit/KeyValueStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 40 additions & 1 deletion PrivacyKit/PrivacyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

0 comments on commit d9c3ed5

Please sign in to comment.