Repository navigation
feat(iris): app-privacy get, declare and publish #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Sources/ASCCommand/Commands/Iris/AppPrivacy/IrisAppPrivacyCommand.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import ArgumentParser | ||
| import ASCKit | ||
|
|
||
| /// `asc iris app-privacy` — the app's App Privacy label ("nutrition label"). Iris private | ||
| /// API only: there is no public endpoint, yet App Review refuses a submission until it's published. | ||
| struct IrisAppPrivacyCommand: AsyncParsableCommand { | ||
| static let configuration = CommandConfiguration( | ||
| commandName: "app-privacy", | ||
| abstract: "Show, declare and publish an app's App Privacy label (iris session)", | ||
| subcommands: [IrisAppPrivacyGet.self, IrisAppPrivacyDeclare.self, IrisAppPrivacyPublish.self] | ||
| ) | ||
| } | ||
|
|
||
| struct IrisAppPrivacyGet: AsyncParsableCommand { | ||
| static let configuration = CommandConfiguration( | ||
| commandName: "get", | ||
| abstract: "Show what an app's privacy label declares and whether it is published" | ||
| ) | ||
|
|
||
| @OptionGroup var globals: GlobalOptions | ||
|
|
||
| @Option(name: .long, help: "App ID") | ||
| var appId: String | ||
|
|
||
| func run() async throws { | ||
| print(try await execute(cookieProvider: ClientProvider.makeIrisCookieProvider(), repo: ClientProvider.makeIrisAppPrivacyRepository())) | ||
| } | ||
|
|
||
| func execute(cookieProvider: any IrisCookieProvider, repo: any IrisAppPrivacyRepository, affordanceMode: AffordanceMode = .cli) async throws -> String { | ||
| let privacy = try await repo.fetchPrivacy(session: try cookieProvider.resolveSession(), appId: appId) | ||
| return try OutputFormatter(format: globals.outputFormat, pretty: globals.pretty).formatAgentItems([privacy], affordanceMode: affordanceMode) | ||
| } | ||
| } | ||
|
|
||
| struct IrisAppPrivacyPublish: AsyncParsableCommand { | ||
| static let configuration = CommandConfiguration( | ||
| commandName: "publish", | ||
| abstract: "Publish an app's declared privacy label so it no longer blocks review" | ||
| ) | ||
|
|
||
| @OptionGroup var globals: GlobalOptions | ||
|
|
||
| @Option(name: .long, help: "App ID") | ||
| var appId: String | ||
|
|
||
| func run() async throws { | ||
| print(try await execute(cookieProvider: ClientProvider.makeIrisCookieProvider(), repo: ClientProvider.makeIrisAppPrivacyRepository())) | ||
| } | ||
|
|
||
| func execute(cookieProvider: any IrisCookieProvider, repo: any IrisAppPrivacyRepository, affordanceMode: AffordanceMode = .cli) async throws -> String { | ||
| let session = try cookieProvider.resolveSession() | ||
| let published = try await repo.fetchPrivacy(session: session, appId: appId).publish(session: session, repo: repo) | ||
| return try OutputFormatter(format: globals.outputFormat, pretty: globals.pretty).formatAgentItems([published], affordanceMode: affordanceMode) | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
Sources/ASCCommand/Commands/Iris/AppPrivacy/IrisAppPrivacyDeclare.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import ArgumentParser | ||
| import Foundation | ||
| import ASCKit | ||
|
|
||
| struct IrisAppPrivacyDeclare: AsyncParsableCommand { | ||
| static let configuration = CommandConfiguration( | ||
| commandName: "declare", | ||
| abstract: "Replace an app's privacy label (it stays a draft until `publish`)", | ||
| discussion: """ | ||
| --not-collected declares the app collects no data. --file takes a JSON file listing what it collects: | ||
| {"collectedData": [{"category": "EMAIL_ADDRESS", "purposes": ["APP_FUNCTIONALITY"], | ||
| "isLinkedToYou": true, "isUsedToTrackYou": false}]} | ||
| the same shape `get` shows. The file may also be {"notCollected": true}. | ||
| """ | ||
| ) | ||
|
|
||
| @OptionGroup var globals: GlobalOptions | ||
|
|
||
| @Option(name: .long, help: "App ID") | ||
| var appId: String | ||
|
|
||
| @Flag(name: .long, help: "The app collects no data") | ||
| var notCollected = false | ||
|
|
||
| @Option(name: .long, help: "A JSON file of the data the app collects") | ||
| var file: String? | ||
|
|
||
| func validate() throws { | ||
| guard notCollected != (file != nil) else { | ||
| throw ValidationError("Give either --not-collected or --file <privacy.json>.") | ||
| } | ||
| } | ||
|
|
||
| func run() async throws { | ||
| print(try await execute(cookieProvider: ClientProvider.makeIrisCookieProvider(), repo: ClientProvider.makeIrisAppPrivacyRepository())) | ||
| } | ||
|
|
||
| func execute(cookieProvider: any IrisCookieProvider, repo: any IrisAppPrivacyRepository, affordanceMode: AffordanceMode = .cli) async throws -> String { | ||
| let declaration = try declaration() | ||
| try declaration.validate() // refused before signing in or reaching Apple (law 6) | ||
| let session = try cookieProvider.resolveSession() | ||
| let declared = try await repo.fetchPrivacy(session: session, appId: appId).declare(declaration, session: session, repo: repo) | ||
| return try OutputFormatter(format: globals.outputFormat, pretty: globals.pretty).formatAgentItems([declared], affordanceMode: affordanceMode) | ||
| } | ||
|
|
||
| private func declaration() throws -> PrivacyDeclaration { | ||
| guard let file else { return .notCollected } | ||
| return try JSONDecoder().decode(PrivacyDeclaration.self, from: Data(contentsOf: URL(fileURLWithPath: file))) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Sources/ASCCommand/Commands/Web/Controllers/IrisAppPrivacyController.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import ASCKit | ||
| import Foundation | ||
| import Hummingbird | ||
| import HummingbirdWebSocket | ||
|
|
||
| /// `GET /api/v1/iris/apps/:appId/app-privacy` — the app's privacy label. | ||
| /// `POST /api/v1/iris/apps/:appId/app-privacy/declare` with `{"notCollected": true}` or | ||
| /// `{"collectedData": [...]}` — replaces it (a draft until published). | ||
| /// `POST /api/v1/iris/apps/:appId/app-privacy/publish` — makes it live. | ||
| /// Iris private API only (browser-cookie auth). | ||
| struct IrisAppPrivacyController: Sendable { | ||
| let cookieProvider: any IrisCookieProvider | ||
| let repo: any IrisAppPrivacyRepository | ||
|
|
||
| func addRoutes(to group: RouterGroup<BasicWebSocketRequestContext>) { | ||
| group.get("/iris/apps/:appId/app-privacy") { _, context -> Response in | ||
| guard let appId = context.parameters.get("appId") else { return jsonError("Missing appId") } | ||
| let session = try self.cookieProvider.resolveSession() | ||
| return try restFormat(try await self.repo.fetchPrivacy(session: session, appId: appId)) | ||
| } | ||
|
|
||
| group.post("/iris/apps/:appId/app-privacy/declare") { request, context -> Response in | ||
| guard let appId = context.parameters.get("appId") else { return jsonError("Missing appId") } | ||
| let bytes = try await request.body.collect(upTo: 1_000_000) | ||
| guard let declaration = try? JSONDecoder().decode(PrivacyDeclaration.self, from: Data(buffer: bytes)) else { | ||
| return jsonError(#"Send either {"notCollected": true} or {"collectedData": [...]}"#) | ||
| } | ||
| return try await self.answering { | ||
| try declaration.validate() // refused before signing in or reaching Apple (law 6) | ||
| let session = try self.cookieProvider.resolveSession() | ||
| let privacy = try await self.repo.fetchPrivacy(session: session, appId: appId) | ||
| return try restFormat(try await privacy.declare(declaration, session: session, repo: self.repo)) | ||
| } | ||
| } | ||
|
|
||
| group.post("/iris/apps/:appId/app-privacy/publish") { _, context -> Response in | ||
| guard let appId = context.parameters.get("appId") else { return jsonError("Missing appId") } | ||
| let session = try self.cookieProvider.resolveSession() | ||
| return try await self.answering { | ||
| let privacy = try await self.repo.fetchPrivacy(session: session, appId: appId) | ||
| return try restFormat(try await privacy.publish(session: session, repo: self.repo)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A refusal the domain explains is answered with its own words and status. | ||
| private func answering(_ handler: () async throws -> Response) async throws -> Response { | ||
| do { | ||
| return try await handler() | ||
| } catch let error as AppPrivacyError { | ||
| switch error { | ||
| case .nothingDeclared: return jsonError(error.description, status: .conflict) | ||
| case .noDataTypes, .noPurposes, .unknownCategory, .unknownPurpose, .duplicateCategory, .duplicatePurpose: | ||
| return jsonError(error.description, status: .unprocessableContent) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /// REST routes for App Privacy. | ||
| extension RESTPathResolver { | ||
| static let _appPrivacyRoutes: Void = { | ||
| // One label per app: | ||
| // `asc iris app-privacy get --app-id A` → GET /api/v1/iris/apps/A/app-privacy | ||
| // `… declare --app-id A` / `… publish …` → POST /api/v1/iris/apps/A/app-privacy/{declare|publish} | ||
| registerRoute(command: "iris app-privacy", parentParam: "app-id", parentSegment: "iris/apps", segment: "app-privacy") | ||
| }() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /// An app's privacy label — the App Privacy page: "Data Not Collected" or the data types it | ||
| /// collects, and whether that answer is published. One per app; the aggregate. | ||
| public struct AppPrivacy: Sendable, Equatable { | ||
| /// Parent and identity: there is one label per app. | ||
| public let appId: String | ||
| public let dataCollection: DataCollection | ||
| public let isPublished: Bool | ||
| /// When it was last published (Apple's `lastPublished`); nil = never. | ||
| public let lastPublishedAt: String? | ||
|
|
||
| public init(appId: String, dataCollection: DataCollection, isPublished: Bool, lastPublishedAt: String?) { | ||
| self.appId = appId | ||
| self.dataCollection = dataCollection | ||
| self.isPublished = isPublished | ||
| self.lastPublishedAt = lastPublishedAt | ||
| } | ||
|
|
||
| /// The label Apple's rows make (law 1). | ||
| public init(appId: String, dataUsages: [DataUsage], isPublished: Bool, lastPublishedAt: String?) { | ||
| let collected = CollectedData.grouping(dataUsages) | ||
| let collection: DataCollection | ||
| if !collected.isEmpty { | ||
| collection = .collected(collected) | ||
| } else if dataUsages.contains(where: { $0.protection == .notCollected }) { | ||
| collection = .notCollected | ||
| } else { | ||
| collection = .notAnswered | ||
| } | ||
| self.init(appId: appId, dataCollection: collection, isPublished: isPublished, lastPublishedAt: lastPublishedAt) | ||
| } | ||
|
|
||
| public var isAnswered: Bool { dataCollection != .notAnswered } | ||
| public var isNotCollected: Bool { dataCollection == .notCollected } | ||
| /// App Review refuses a submission until the label is answered and published (law 7). | ||
| public var blocksSubmission: Bool { !(isAnswered && isPublished) } | ||
|
|
||
| /// Replaces the label with `declaration` (law 3). It stays a draft until published (law 4). | ||
| public func declare(_ declaration: PrivacyDeclaration, session: IrisSession, repo: any IrisAppPrivacyRepository) async throws -> AppPrivacy { | ||
| try declaration.validate() | ||
| let plan = DataUsagePlan(current: try await repo.listDataUsages(session: session, appId: appId), wanted: declaration) | ||
| for usage in plan.deletions { | ||
| try await repo.deleteDataUsage(session: session, dataUsageId: usage.id) | ||
| } | ||
| for entry in plan.creations { | ||
| try await repo.createDataUsage(session: session, appId: appId, entry: entry) | ||
| } | ||
| return try await repo.fetchPrivacy(session: session, appId: appId) | ||
| } | ||
|
|
||
| /// Makes the label live — only once something is declared (law 5). | ||
| public func publish(session: IrisSession, repo: any IrisAppPrivacyRepository) async throws -> AppPrivacy { | ||
| guard isAnswered else { throw AppPrivacyError.nothingDeclared(appId: appId) } | ||
| try await repo.publish(session: session, appId: appId) | ||
| return try await repo.fetchPrivacy(session: session, appId: appId) | ||
| } | ||
| } | ||
|
|
||
| extension AppPrivacy: Codable { | ||
| private enum CodingKeys: String, CodingKey { | ||
| case appId, dataCollection, collectedData, isAnswered, isPublished, blocksSubmission, lastPublishedAt | ||
| } | ||
|
|
||
| public init(from decoder: any Decoder) throws { | ||
| let c = try decoder.container(keyedBy: CodingKeys.self) | ||
| let collection: DataCollection | ||
| switch try c.decode(String.self, forKey: .dataCollection) { | ||
| case "NOT_COLLECTED": collection = .notCollected | ||
| case "COLLECTED": collection = .collected(try c.decode([CollectedData].self, forKey: .collectedData)) | ||
| default: collection = .notAnswered | ||
| } | ||
| self.init( | ||
| appId: try c.decode(String.self, forKey: .appId), | ||
| dataCollection: collection, | ||
| isPublished: try c.decode(Bool.self, forKey: .isPublished), | ||
| lastPublishedAt: try c.decodeIfPresent(String.self, forKey: .lastPublishedAt) | ||
| ) | ||
| } | ||
|
|
||
| public func encode(to encoder: any Encoder) throws { | ||
| var c = encoder.container(keyedBy: CodingKeys.self) | ||
| try c.encode(appId, forKey: .appId) | ||
| try c.encode(dataCollection.wireValue, forKey: .dataCollection) | ||
| if case .collected(let data) = dataCollection { | ||
| try c.encode(data, forKey: .collectedData) | ||
| } | ||
| try c.encode(isAnswered, forKey: .isAnswered) | ||
| try c.encode(isPublished, forKey: .isPublished) | ||
| try c.encode(blocksSubmission, forKey: .blocksSubmission) | ||
| try c.encodeIfPresent(lastPublishedAt, forKey: .lastPublishedAt) | ||
| } | ||
| } | ||
|
|
||
| extension AppPrivacy: AffordanceProviding { | ||
| public var structuredAffordances: [Affordance] { | ||
| let app = ["app-id": appId] | ||
| var items = [ | ||
| Affordance(key: "get", command: "iris app-privacy", action: "get", params: app), | ||
| Affordance(key: "declare", command: "iris app-privacy", action: "declare", params: app.merging(["file": "<privacy.json>"]) { $1 }), | ||
| ] | ||
| if !isAnswered { | ||
| items.append(Affordance(key: "declareNotCollected", command: "iris app-privacy", action: "declare", params: app, flags: ["not-collected"])) | ||
| } | ||
| if isAnswered && !isPublished { | ||
| items.append(Affordance(key: "publish", command: "iris app-privacy", action: "publish", params: app)) | ||
| } | ||
| return items | ||
| } | ||
| } | ||
|
|
||
| extension AppPrivacy: Presentable { | ||
| public static var tableHeaders: [String] { ["App ID", "Data Collection", "Published", "Blocks Submission"] } | ||
| public var tableRow: [String] { | ||
| [appId, dataCollection.wireValue, isPublished ? "yes" : "no", blocksSubmission ? "yes" : "no"] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import Foundation | ||
|
|
||
| public enum AppPrivacyError: Error, Equatable, Sendable, CustomStringConvertible, LocalizedError { | ||
| /// Publishing a label with nothing declared (law 5). | ||
| case nothingDeclared(appId: String) | ||
| /// `collectedData` is empty: that is "Data Not Collected", said another way. | ||
| case noDataTypes | ||
| case noPurposes(category: String) | ||
| case unknownCategory(String) | ||
| case unknownPurpose(String) | ||
| case duplicateCategory(String) | ||
| case duplicatePurpose(category: String, purpose: String) | ||
|
|
||
| public var description: String { | ||
| switch self { | ||
| case .nothingDeclared(let appId): | ||
| "App \(appId) has no privacy answers to publish. Declare them first: `asc iris app-privacy declare --app-id \(appId) --not-collected` or `--file <privacy.json>`." | ||
| case .noDataTypes: | ||
| "collectedData lists no data types. If the app collects nothing, declare --not-collected." | ||
| case .noPurposes(let category): | ||
| "\(category) needs at least one purpose." | ||
| case .unknownCategory(let category): | ||
| "\(category) isn't a data type App Privacy knows (e.g. EMAIL_ADDRESS, CRASH_DATA, DEVICE_ID)." | ||
| case .unknownPurpose(let purpose): | ||
| "\(purpose) isn't a purpose App Privacy knows: APP_FUNCTIONALITY, ANALYTICS, PRODUCT_PERSONALIZATION, DEVELOPERS_ADVERTISING, THIRD_PARTY_ADVERTISING or OTHER_PURPOSES." | ||
| case .duplicateCategory(let category): | ||
| "\(category) is declared twice; list each data type once with all its purposes." | ||
| case .duplicatePurpose(let category, let purpose): | ||
| "\(category) lists \(purpose) twice; list each purpose once." | ||
| } | ||
| } | ||
|
|
||
| public var errorDescription: String? { description } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.