SPB Git

Repository Public

DeveloperSecretSheet.swift 5.6 KB 81 lines swift

1// SP Wallet iOS2// File: Features/Security/DeveloperSecretSheet.swift3// Description: The one and only display of a secret the server issues once (an API key token, a webhook signing secret).4//              Monospaced and complete; copied to the pasteboard for 120 seconds, on this device only; never written to a cache,5//              a log or a snapshot; hidden while the screen is being recorded; dropped from memory when the sheet closes. When the6//              created key differs from the intent that was approved, the differences are shown first and revocation is offered.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import SwiftUI11import UniformTypeIdentifiers12import SPWalletCore1314struct DeveloperSecretSheet: View {15    @Environment(AppModel.self) private var model16    @Environment(\.dismiss) private var dismiss17    let issued: DeveloperIssuedSecret18    /** revokes the key at once (offered when `discrepancies` is not empty) */19    let revoke: (String) async -> Void20    /** the secret, in this view's memory only while it is on screen */21    @State private var revealed: String?22    @State private var copied = false23    @State private var confirmedSaved = false24    @State private var revoked = false25    @Environment(\.isSceneCaptured) private var captured2627    var body: some View {28        NavigationStack {29            ScrollView {30                VStack(alignment: .leading, spacing: 14) {31                    VStack(alignment: .leading, spacing: 4) {32                        Text(issued.title).font(.system(size: 26, weight: .semibold)).foregroundStyle(Theme.ink).accessibilityIdentifier("developer.secret.title")33                        Text(issued.subject).font(.spMono).foregroundStyle(Theme.mute).textSelection(.enabled)34                    }35                    if !issued.discrepancies.isEmpty {36                        Notice(tone: .danger, title: String(localized: "Ce que le serveur a créé diffère de ce que tu as approuvé"),37                               message: String(localized: "Différences : \(issued.discrepancies.joined(separator: ", ")). N’utilise pas ce secret ; révoque cette clé maintenant."))38                            .accessibilityIdentifier("developer.secret.discrepancies")39                        if let id = issued.revokeKeyId, !revoked {40                            Button { Task { await revoke(id); revoked = true } } label: { Label(String(localized: "Révoquer cette clé maintenant"), systemImage: "xmark.shield.fill") }41                                .buttonStyle(.spDestructive).accessibilityIdentifier("developer.secret.revoke")42                        }43                        if revoked { Notice(tone: .info, title: String(localized: "Révocation demandée : vérifie l’état de la clé dans la liste.")) }44                    }45                    Notice(tone: .warn, title: String(localized: "Ce secret n’est montré qu’une seule fois"),46                           message: String(localized: "Le serveur n’en garde qu’une empreinte et l’app ne l’enregistre nulle part. Range-le maintenant dans ton gestionnaire de secrets. Ne fais pas de capture d’écran : elle resterait dans tes photos et tes sauvegardes."))47                    VStack(alignment: .leading, spacing: 10) {48                        Text(String(localized: "Secret")).spEyebrow()49                        if captured {50                            Label(String(localized: "Masqué : l’écran est en cours d’enregistrement ou de recopie."), systemImage: "eye.slash.fill").font(.spCallout).foregroundStyle(Theme.warn)51                        } else if let s = revealed {52                            Text(s).font(.spMono).foregroundStyle(Theme.ink).textSelection(.enabled).fixedSize(horizontal: false, vertical: true).privacySensitive()53                                .accessibilityIdentifier("developer.secret.value")54                        }55                        Button { copy() } label: { Label(copied ? String(localized: "Copié pour 120 secondes, sur cet appareil seulement") : String(localized: "Copier (expire après 120 secondes)"), systemImage: copied ? "checkmark" : "doc.on.doc") }56                            .buttonStyle(.spSecondary).disabled(revealed == nil).accessibilityIdentifier("developer.secret.copy")57                    }.spCard(padding: 16)58                    ForEach(issued.notes, id: \.self) { n in Text(n).font(.spFootnote).foregroundStyle(Theme.mute) }59                    Toggle(isOn: $confirmedSaved) { Text(String(localized: "J’ai rangé ce secret en lieu sûr.")).font(.spCallout.weight(.semibold)) }60                        .toggleStyle(MandateCheckboxStyle()).accessibilityIdentifier("developer.secret.saved")61                    Button(String(localized: "Fermer définitivement")) { close() }.buttonStyle(.spPrimary).disabled(!confirmedSaved && issued.discrepancies.isEmpty).accessibilityIdentifier("developer.secret.close")62                }63                .padding(.horizontal, Theme.Space.gutter).padding(.vertical, 12)64                .frame(maxWidth: 620).frame(maxWidth: .infinity)65            }66            .spPage()67            .navigationTitle(String(localized: "Secret à usage unique")).navigationBarTitleDisplayMode(.inline)68        }69        .interactiveDismissDisabled()70        .onAppear { revealed = issued.secret.reveal() }71        .onDisappear { revealed = nil }72    }7374    private func copy() {75        guard let s = revealed else { return }76        UIPasteboard.general.setItems([[UTType.utf8PlainText.identifier: s]], options: [.expirationDate: Date().addingTimeInterval(120), .localOnly: true])77        copied = true78    }79    private func close() { revealed = nil; dismiss() }80}81
Files main
Menu

Theme