Skip to content

Commit

Permalink
feat: added encryption fix and app icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Dandin committed Dec 30, 2024
1 parent 9237844 commit 0c401c3
Show file tree
Hide file tree
Showing 54 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GitHub Profile Switcher</title>
<title>Git Profile Switcher</title>
</head>

<body>
Expand Down
Binary file modified src-tauri/icons/128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/128x128@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square107x107Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square142x142Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square150x150Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square284x284Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square30x30Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square310x310Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square44x44Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square71x71Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/Square89x89Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/StoreLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src-tauri/icons/icon.icns
Binary file not shown.
Binary file modified src-tauri/icons/icon.ico
Binary file not shown.
Binary file modified src-tauri/icons/icon.png
Binary file added src-tauri/icons/ios/AppIcon-20x20@1x.png
Binary file added src-tauri/icons/ios/AppIcon-20x20@2x-1.png
Binary file added src-tauri/icons/ios/AppIcon-20x20@2x.png
Binary file added src-tauri/icons/ios/AppIcon-20x20@3x.png
Binary file added src-tauri/icons/ios/AppIcon-29x29@1x.png
Binary file added src-tauri/icons/ios/AppIcon-29x29@2x-1.png
Binary file added src-tauri/icons/ios/AppIcon-29x29@2x.png
Binary file added src-tauri/icons/ios/AppIcon-29x29@3x.png
Binary file added src-tauri/icons/ios/AppIcon-40x40@1x.png
Binary file added src-tauri/icons/ios/AppIcon-40x40@2x-1.png
Binary file added src-tauri/icons/ios/AppIcon-40x40@2x.png
Binary file added src-tauri/icons/ios/AppIcon-40x40@3x.png
Binary file added src-tauri/icons/ios/AppIcon-512@2x.png
Binary file added src-tauri/icons/ios/AppIcon-60x60@2x.png
Binary file added src-tauri/icons/ios/AppIcon-60x60@3x.png
Binary file added src-tauri/icons/ios/AppIcon-76x76@1x.png
Binary file added src-tauri/icons/ios/AppIcon-76x76@2x.png
Binary file added src-tauri/icons/ios/AppIcon-83.5x83.5@2x.png
26 changes: 7 additions & 19 deletions src-tauri/src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use aes_gcm::{
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::fs;
use once_cell::sync::Lazy;
use std::sync::Mutex;

Expand All @@ -26,11 +25,6 @@ pub fn set_encryption_key(key: &str) -> Result<(), String> {
Ok(())
}

pub fn get_encryption_key() -> Result<String, String> {
let key = ENCRYPTION_KEY.lock().map_err(|e| e.to_string())?;
String::from_utf8(key.clone()).map_err(|e| e.to_string())
}

#[derive(Serialize, Deserialize)]
pub struct EncryptedData {
pub nonce: String,
Expand All @@ -39,6 +33,9 @@ pub struct EncryptedData {

pub fn encrypt_string(data: &str) -> Result<String, String> {
let key_data = ENCRYPTION_KEY.lock().map_err(|e| e.to_string())?;
if key_data.is_empty() {
return Err("Encryption key not set".to_string());
}
let key = Key::<Aes256Gcm>::from_slice(&key_data);
let cipher = Aes256Gcm::new(key);

Expand All @@ -61,6 +58,10 @@ pub fn encrypt_string(data: &str) -> Result<String, String> {
}

pub fn decrypt_string(encrypted_json: &str) -> Result<String, String> {
let key_data = ENCRYPTION_KEY.lock().map_err(|e| e.to_string())?;
if key_data.is_empty() {
return Err("Encryption key not set".to_string());
}
// Parse encrypted data
let encrypted_data: EncryptedData = serde_json::from_str(encrypted_json)
.map_err(|e| format!("Failed to parse encrypted data: {}", e))?;
Expand All @@ -74,7 +75,6 @@ pub fn decrypt_string(encrypted_json: &str) -> Result<String, String> {
.map_err(|e| format!("Failed to decode ciphertext: {}", e))?;

// Get the current encryption key
let key_data = ENCRYPTION_KEY.lock().map_err(|e| e.to_string())?;
let key = Key::<Aes256Gcm>::from_slice(&key_data);
let cipher = Aes256Gcm::new(key);
let nonce = Nonce::from_slice(&nonce_bytes);
Expand All @@ -86,16 +86,4 @@ pub fn decrypt_string(encrypted_json: &str) -> Result<String, String> {

String::from_utf8(plaintext)
.map_err(|e| format!("Failed to convert decrypted data to string: {}", e))
}

pub fn write_encrypted_file(path: &std::path::Path, content: &str) -> Result<(), String> {
let encrypted = encrypt_string(content)?;
fs::write(path, encrypted)
.map_err(|e| format!("Failed to write encrypted file: {}", e))
}

pub fn read_encrypted_file(path: &std::path::Path) -> Result<String, String> {
let encrypted = fs::read_to_string(path)
.map_err(|e| format!("Failed to read encrypted file: {}", e))?;
decrypt_string(&encrypted)
}
41 changes: 32 additions & 9 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::Mutex;
use once_cell::sync::Lazy;
use opener;
mod encryption; // Add this at the top
use crate::encryption::{write_encrypted_file, read_encrypted_file, set_encryption_key};
use crate::encryption::{encrypt_string, decrypt_string, set_encryption_key};

// App configuration structure
#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -56,7 +56,10 @@ async fn save_profiles(profiles: Vec<GitProfile>) -> Result<(), String> {
let json = serde_json::to_string_pretty(&profiles)
.map_err(|e| format!("Failed to serialize profiles: {}", e))?;

write_encrypted_file(&profiles_file, &json)
// Encrypt the profiles data
let encrypted = encrypt_string(&json)?;
fs::write(&profiles_file, encrypted)
.map_err(|e| format!("Failed to write profiles file: {}", e))
}

#[command]
Expand All @@ -70,7 +73,11 @@ async fn load_profiles() -> Result<Vec<GitProfile>, String> {
return Ok(Vec::new());
}

let json = read_encrypted_file(&profiles_file)?;
let encrypted = fs::read_to_string(&profiles_file)
.map_err(|e| format!("Failed to read profiles file: {}", e))?;

// Decrypt the profiles data
let json = decrypt_string(&encrypted)?;

serde_json::from_str(&json)
.map_err(|e| format!("Failed to deserialize profiles: {}", e))
Expand Down Expand Up @@ -172,10 +179,19 @@ fn load_config() -> Result<AppConfig, String> {
return Ok(config);
}

let config_str = read_encrypted_file(&config_path)?;
// Read config as plain JSON
let config_str = fs::read_to_string(&config_path)
.map_err(|e| format!("Failed to read config file: {}", e))?;

serde_json::from_str(&config_str)
.map_err(|e| format!("Failed to parse config file: {}", e))
let config: AppConfig = serde_json::from_str(&config_str)
.map_err(|e| format!("Failed to parse config file: {}", e))?;

// If there's an encryption key, set it
if let Some(key) = &config.encryption_key {
set_encryption_key(key)?;
}

Ok(config)
}

fn save_config(config: &AppConfig) -> Result<(), String> {
Expand All @@ -187,10 +203,12 @@ fn save_config(config: &AppConfig) -> Result<(), String> {
.map_err(|e| format!("Failed to create config directory: {}", e))?;
}

// Save config as plain JSON
let config_str = serde_json::to_string_pretty(config)
.map_err(|e| format!("Failed to serialize config: {}", e))?;

write_encrypted_file(&config_path, &config_str)
fs::write(&config_path, config_str)
.map_err(|e| format!("Failed to write config file: {}", e))
}
// Add this function to initialize encryption on startup
fn initialize_encryption() -> Result<(), String> {
Expand Down Expand Up @@ -243,9 +261,14 @@ async fn get_profiles_dir() -> Result<String, String> {
#[command]
async fn update_encryption_key(key: String) -> Result<(), String> {
let mut config = APP_CONFIG.lock().map_err(|e| e.to_string())?;
config.encryption_key = Some(key.clone());
save_config(&config)?;

// Try to set the new encryption key
set_encryption_key(&key)?;

// Update config with the new key
config.encryption_key = Some(key);
save_config(&config)?;

Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "github-profile-selector",
"version": "0.1.0",
"productName": "Git Profile Switcher",
"version": "1.0.0",
"identifier": "com.github-profile-selector.app",
"build": {
"beforeDevCommand": "npm run dev",
Expand All @@ -12,7 +12,7 @@
"app": {
"windows": [
{
"title": "github-profile-selector",
"title": "Git Profile Switcher",
"width": 800,
"height": 600
}
Expand Down
10 changes: 1 addition & 9 deletions src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Settings = () => {
const { theme, setTheme } = useTheme();
const [config, setConfig] = useState<AppConfig | null>(null);
const [profilesPath, setProfilesPath] = useState("");
const [backupEnabled, setBackupEnabled] = useState<Boolean>(true);
const [backupEnabled, setBackupEnabled] = useState<boolean>(true);
const [encryptionKey, setEncryptionKey] = useState("");
const [isEncryptionSetup, setIsEncryptionSetup] = useState(false);
const [showKey, setShowKey] = useState(false);
Expand All @@ -44,14 +44,6 @@ const Settings = () => {
}
};

const checkEncryptionStatus = async () => {
try {
const isSetup = await invoke<boolean>("is_encryption_setup");
setIsEncryptionSetup(isSetup);
} catch (error) {
toast.error("Failed to check encryption status");
}
};

const handleUpdateEncryption = async () => {
if (!encryptionKey) {
Expand Down

0 comments on commit 0c401c3

Please sign in to comment.