Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add example credential add to extension (#771)

Leendert de Borst1652a4184021
diff --git a/mobile-app/app/(tabs)/index.tsx b/mobile-app/app/(tabs)/index.tsx
index 886b07960..ad7cbc46d 100644
--- a/mobile-app/app/(tabs)/index.tsx
+++ b/mobile-app/app/(tabs)/index.tsx
@@ -1,4 +1,5 @@
-import { Image, StyleSheet, Platform } from 'react-native';
+import { Image, StyleSheet, Platform, Button, View } from 'react-native';
+import { NativeModules } from 'react-native';
import { HelloWave } from '@/components/HelloWave';
import ParallaxScrollView from '@/components/ParallaxScrollView';
@@ -6,6 +7,21 @@ import { ThemedText } from '@/components/ThemedText';
import { ThemedView } from '@/components/ThemedView';
export default function HomeScreen() {
+ const handleInsertEntry = () => {
+ // Generate a random credential
+ const randomUsername = `user${Math.floor(Math.random() * 1000)}`;
+ const randomPassword = `pass${Math.floor(Math.random() * 1000)}`;
+ const randomService = `service${Math.floor(Math.random() * 1000)}`;
+
+ // Call native module to add credential
+ NativeModules.CredentialManager.addCredential(randomUsername, randomPassword, randomService);
+ };
+
+ const handleClearVault = () => {
+ // Call native module to clear credentials
+ NativeModules.CredentialManager.clearCredentials();
+ };
+
return (
<ParallaxScrollView
headerBackgroundColor={{ light: '#A1CEDC', dark: '#1D3D47' }}
@@ -19,6 +35,13 @@ export default function HomeScreen() {
<ThemedText type="title">Welcome!</ThemedText>
<HelloWave />
</ThemedView>
+
+ <ThemedView style={styles.buttonContainer}>
+ <Button title="Insert Random Entry" onPress={handleInsertEntry} />
+ <View style={styles.buttonSpacer} />
+ <Button title="Clear Vault" onPress={handleClearVault} color="red" />
+ </ThemedView>
+
<ThemedView style={styles.stepContainer}>
<ThemedText type="subtitle">Step 1: Try it</ThemedText>
<ThemedText>
@@ -64,6 +87,13 @@ const styles = StyleSheet.create({
gap: 8,
marginBottom: 8,
},
+ buttonContainer: {
+ marginVertical: 16,
+ paddingHorizontal: 16,
+ },
+ buttonSpacer: {
+ height: 8,
+ },
reactLogo: {
height: 178,
width: 290,
diff --git a/mobile-app/ios/SharedCredentialStore.swift b/mobile-app/ios/SharedCredentialStore.swift
new file mode 100644
index 000000000..95c844f3e
--- /dev/null
+++ b/mobile-app/ios/SharedCredentialStore.swift
@@ -0,0 +1,39 @@
+import Foundation
+
+struct Credential: Codable {
+ let username: String
+ let password: String
+ let service: String
+}
+
+class SharedCredentialStore {
+ static let shared = SharedCredentialStore()
+ private let userDefaults: UserDefaults
+ private let credentialsKey = "storedCredentials"
+
+ private init() {
+ // Use the app group identifier
+ userDefaults = UserDefaults(suiteName: "group.net.aliasvault.autofill")!
+ }
+
+ func getAllCredentials() -> [Credential] {
+ guard let data = userDefaults.data(forKey: credentialsKey) else { return [] }
+ return (try? JSONDecoder().decode([Credential].self, from: data)) ?? []
+ }
+
+ func addCredential(_ credential: Credential) {
+ var credentials = getAllCredentials()
+ credentials.append(credential)
+ saveCredentials(credentials)
+ }
+
+ func clearAllCredentials() {
+ userDefaults.removeObject(forKey: credentialsKey)
+ }
+
+ private func saveCredentials(_ credentials: [Credential]) {
+ if let data = try? JSONEncoder().encode(credentials) {
+ userDefaults.set(data, forKey: credentialsKey)
+ }
+ }
+}
\ No newline at end of file
diff --git a/mobile-app/ios/aliasvault.xcodeproj/project.pbxproj b/mobile-app/ios/aliasvault.xcodeproj/project.pbxproj
index 027a2e797..ff8391276 100644
--- a/mobile-app/ios/aliasvault.xcodeproj/project.pbxproj
+++ b/mobile-app/ios/aliasvault.xcodeproj/project.pbxproj
@@ -14,6 +14,8 @@
96905EF65AED1B983A6B3ABC /* libPods-aliasvault.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 58EEBF8E8E6FB1BC6CAF49B5 /* libPods-aliasvault.a */; };
B18059E884C0ABDD17F3DC3D /* ExpoModulesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAC715A2D49A985799AEE119 /* ExpoModulesProvider.swift */; };
BB2F792D24A3F905000567C9 /* Expo.plist in Resources */ = {isa = PBXBuildFile; fileRef = BB2F792C24A3F905000567C9 /* Expo.plist */; };
+ CE2F00E62DA56B6A00A5C16E /* SharedCredentialStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE2F00E52DA56B6A00A5C16E /* SharedCredentialStore.swift */; };
+ CE2F00E72DA56B6A00A5C16E /* SharedCredentialStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE2F00E52DA56B6A00A5C16E /* SharedCredentialStore.swift */; };
CEE909802DA548C7008D568F /* AuthenticationServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CEE9097F2DA548C7008D568F /* AuthenticationServices.framework */; };
CEE9098B2DA548C7008D568F /* autofill.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = CEE9097E2DA548C7008D568F /* autofill.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
D6E19ADE0A608FA46984C60D /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = BA88D6CB4FD656C7180FE9F7 /* PrivacyInfo.xcprivacy */; };
@@ -59,6 +61,7 @@
AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = SplashScreen.storyboard; path = aliasvault/SplashScreen.storyboard; sourceTree = "<group>"; };
BA88D6CB4FD656C7180FE9F7 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = aliasvault/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
BB2F792C24A3F905000567C9 /* Expo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Expo.plist; sourceTree = "<group>"; };
+ CE2F00E52DA56B6A00A5C16E /* SharedCredentialStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedCredentialStore.swift; sourceTree = "<group>"; };
CEE9097E2DA548C7008D568F /* autofill.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = autofill.appex; sourceTree = BUILT_PRODUCTS_DIR; };
CEE9097F2DA548C7008D568F /* AuthenticationServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AuthenticationServices.framework; path = System/Library/Frameworks/AuthenticationServices.framework; sourceTree = SDKROOT; };
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
@@ -102,6 +105,7 @@
13B07FAE1A68108700A75B9A /* aliasvault */ = {
isa = PBXGroup;
children = (
+ CE2F00E52DA56B6A00A5C16E /* SharedCredentialStore.swift */,
BB2F792B24A3F905000567C9 /* Supporting */,
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.mm */,
@@ -420,6 +424,7 @@
files = (
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */,
13B07FC11A68108700A75B9A /* main.m in Sources */,
+ CE2F00E72DA56B6A00A5C16E /* SharedCredentialStore.swift in Sources */,
B18059E884C0ABDD17F3DC3D /* ExpoModulesProvider.swift in Sources */,
E8FD7000523F4549908941A2 /* noop-file.swift in Sources */,
);
@@ -429,6 +434,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ CE2F00E62DA56B6A00A5C16E /* SharedCredentialStore.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/mobile-app/ios/aliasvault/CredentialManager.swift b/mobile-app/ios/aliasvault/CredentialManager.swift
new file mode 100644
index 000000000..77d597aba
--- /dev/null
+++ b/mobile-app/ios/aliasvault/CredentialManager.swift
@@ -0,0 +1,21 @@
+import Foundation
+import aliasvault
+
+@objc(CredentialManager)
+class CredentialManager: NSObject {
+ @objc
+ func addCredential(_ username: String, password: String, service: String) {
+ let credential = Credential(username: username, password: password, service: service)
+ SharedCredentialStore.shared.addCredential(credential)
+ }
+
+ @objc
+ func clearCredentials() {
+ SharedCredentialStore.shared.clearAllCredentials()
+ }
+
+ @objc
+ static func requiresMainQueueSetup() -> Bool {
+ return false
+ }
+}
\ No newline at end of file
diff --git a/mobile-app/ios/aliasvault/SharedCredentialStore.swift b/mobile-app/ios/aliasvault/SharedCredentialStore.swift
new file mode 100644
index 000000000..c3e842e2c
--- /dev/null
+++ b/mobile-app/ios/aliasvault/SharedCredentialStore.swift
@@ -0,0 +1,45 @@
+import Foundation
+
+public struct Credential: Codable {
+ public let username: String
+ public let password: String
+ public let service: String
+
+ public init(username: String, password: String, service: String) {
+ self.username = username
+ self.password = password
+ self.service = service
+ }
+}
+
+public class SharedCredentialStore {
+ public static let shared = SharedCredentialStore()
+ private let userDefaults: UserDefaults
+ private let credentialsKey = "storedCredentials"
+
+ private init() {
+ // Use the app group identifier
+ userDefaults = UserDefaults(suiteName: "group.net.aliasvault.autofill")!
+ }
+
+ public func getAllCredentials() -> [Credential] {
+ guard let data = userDefaults.data(forKey: credentialsKey) else { return [] }
+ return (try? JSONDecoder().decode([Credential].self, from: data)) ?? []
+ }
+
+ public func addCredential(_ credential: Credential) {
+ var credentials = getAllCredentials()
+ credentials.append(credential)
+ saveCredentials(credentials)
+ }
+
+ public func clearAllCredentials() {
+ userDefaults.removeObject(forKey: credentialsKey)
+ }
+
+ private func saveCredentials(_ credentials: [Credential]) {
+ if let data = try? JSONEncoder().encode(credentials) {
+ userDefaults.set(data, forKey: credentialsKey)
+ }
+ }
+}
\ No newline at end of file
diff --git a/mobile-app/ios/aliasvault/aliasvault-Bridging-Header.h b/mobile-app/ios/aliasvault/aliasvault-Bridging-Header.h
index e11d920b1..84805a30e 100644
--- a/mobile-app/ios/aliasvault/aliasvault-Bridging-Header.h
+++ b/mobile-app/ios/aliasvault/aliasvault-Bridging-Header.h
@@ -1,3 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
+
+#import <React/RCTBridgeModule.h>
diff --git a/mobile-app/ios/autofill/CredentialProviderViewController.swift b/mobile-app/ios/autofill/CredentialProviderViewController.swift
index 42b7efcde..d052ca513 100644
--- a/mobile-app/ios/autofill/CredentialProviderViewController.swift
+++ b/mobile-app/ios/autofill/CredentialProviderViewController.swift
@@ -6,53 +6,111 @@
//
import AuthenticationServices
+import UIKit
class CredentialProviderViewController: ASCredentialProviderViewController {
-
- /*
- Prepare your UI to list available credentials for the user to choose from. The items in
- 'serviceIdentifiers' describe the service the user is logging in to, so your extension can
- prioritize the most relevant credentials in the list.
- */
- override func prepareCredentialList(for serviceIdentifiers: [ASCredentialServiceIdentifier]) {
+ private var credentials: [Credential] = []
+ private let tableView = UITableView()
+ private let addButton = UIButton(type: .system)
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+ setupUI()
+ loadCredentials()
}
-
- /*
- Implement this method if your extension supports showing credentials in the QuickType bar.
- When the user selects a credential from your app, this method will be called with the
- ASPasswordCredentialIdentity your app has previously saved to the ASCredentialIdentityStore.
- Provide the password by completing the extension request with the associated ASPasswordCredential.
- If using the credential would require showing custom UI for authenticating the user, cancel
- the request with error code ASExtensionError.userInteractionRequired.
-
- override func provideCredentialWithoutUserInteraction(for credentialIdentity: ASPasswordCredentialIdentity) {
- let databaseIsUnlocked = true
- if (databaseIsUnlocked) {
- let passwordCredential = ASPasswordCredential(user: "j_appleseed", password: "apple1234")
- self.extensionContext.completeRequest(withSelectedCredential: passwordCredential, completionHandler: nil)
- } else {
- self.extensionContext.cancelRequest(withError: NSError(domain: ASExtensionErrorDomain, code:ASExtensionError.userInteractionRequired.rawValue))
+
+ private func setupUI() {
+ view.backgroundColor = .systemBackground
+
+ // Setup TableView
+ tableView.delegate = self
+ tableView.dataSource = self
+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: "CredentialCell")
+ view.addSubview(tableView)
+ tableView.translatesAutoresizingMaskIntoConstraints = false
+
+ // Setup Add Button
+ addButton.setTitle("Add Credential", for: .normal)
+ addButton.addTarget(self, action: #selector(addCredentialTapped), for: .touchUpInside)
+ view.addSubview(addButton)
+ addButton.translatesAutoresizingMaskIntoConstraints = false
+
+ // Setup Constraints
+ NSLayoutConstraint.activate([
+ tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
+ tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
+ tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
+ tableView.bottomAnchor.constraint(equalTo: addButton.topAnchor),
+
+ addButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
+ addButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
+ addButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16),
+ addButton.heightAnchor.constraint(equalToConstant: 44)
+ ])
+ }
+
+ private func loadCredentials() {
+ credentials = SharedCredentialStore.shared.getAllCredentials()
+ tableView.reloadData()
+ }
+
+ @objc private func addCredentialTapped() {
+ let alert = UIAlertController(title: "Add Credential", message: nil, preferredStyle: .alert)
+
+ alert.addTextField { textField in
+ textField.placeholder = "Username"
+ }
+ alert.addTextField { textField in
+ textField.placeholder = "Password"
+ textField.isSecureTextEntry = true
}
+ alert.addTextField { textField in
+ textField.placeholder = "Service"
+ }
+
+ alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
+ alert.addAction(UIAlertAction(title: "Add", style: .default) { [weak self] _ in
+ guard let username = alert.textFields?[0].text,
+ let password = alert.textFields?[1].text,
+ let service = alert.textFields?[2].text,
+ !username.isEmpty, !password.isEmpty, !service.isEmpty else { return }
+
+ let credential = Credential(username: username, password: password, service: service)
+ SharedCredentialStore.shared.addCredential(credential)
+ self?.loadCredentials()
+ })
+
+ present(alert, animated: true)
}
- */
-
- /*
- Implement this method if provideCredentialWithoutUserInteraction(for:) can fail with
- ASExtensionError.userInteractionRequired. In this case, the system may present your extension's
- UI and call this method. Show appropriate UI for authenticating the user then provide the password
- by completing the extension request with the associated ASPasswordCredential.
-
- override func prepareInterfaceToProvideCredential(for credentialIdentity: ASPasswordCredentialIdentity) {
+
+ override func prepareCredentialList(for serviceIdentifiers: [ASCredentialServiceIdentifier]) {
+ loadCredentials()
}
- */
-
+
@IBAction func cancel(_ sender: AnyObject?) {
self.extensionContext.cancelRequest(withError: NSError(domain: ASExtensionErrorDomain, code: ASExtensionError.userCanceled.rawValue))
}
+
+ override func prepareInterfaceForUserChoosingTextToInsert() {
+ loadCredentials()
+ }
+}
- @IBAction func passwordSelected(_ sender: AnyObject?) {
- let passwordCredential = ASPasswordCredential(user: "j_appleseed", password: "apple1234")
+extension CredentialProviderViewController: UITableViewDelegate, UITableViewDataSource {
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+ return credentials.count
+ }
+
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+ let cell = tableView.dequeueReusableCell(withIdentifier: "CredentialCell", for: indexPath)
+ let credential = credentials[indexPath.row]
+ cell.textLabel?.text = "\(credential.service) - \(credential.username)"
+ return cell
+ }
+
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+ let credential = credentials[indexPath.row]
+ let passwordCredential = ASPasswordCredential(user: credential.username, password: credential.password)
self.extensionContext.completeRequest(withSelectedCredential: passwordCredential, completionHandler: nil)
}
-
}
diff --git a/mobile-app/ios/autofill/Info.plist b/mobile-app/ios/autofill/Info.plist
index 4cea915af..d229a3c6a 100644
--- a/mobile-app/ios/autofill/Info.plist
+++ b/mobile-app/ios/autofill/Info.plist
@@ -8,6 +8,14 @@
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.authentication-services-credential-provider-ui</string>
+ <key>NSExtensionAttributes</key>
+ <dict>
+ <key>ASCredentialProviderExtensionCapabilities</key>
+ <dict>
+ <key>ProvidesTextToInsert</key>
+ <true/>
+ </dict>
+ </dict>
</dict>
</dict>
</plist>