Skip to content

Commit dab643a

Browse files
committed
refactor: remove unused package and improve credential management implementation
1 parent 6841b56 commit dab643a

6 files changed

Lines changed: 75 additions & 27 deletions

File tree

QuotaBarWindows/QuotaBarWindows.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
<ItemGroup>
1616
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
17-
<PackageReference Include="AdysTech.CredentialManager" Version="2.0.0" />
1817
</ItemGroup>
1918

2019
<ItemGroup>
Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,90 @@
1-
using AdysTech.CredentialManager;
1+
using System.Runtime.InteropServices;
2+
using System.Text;
23

34
namespace QuotaBarWindows.Services;
45

56
/// <summary>
6-
/// Stores and retrieves auth tokens securely via Windows Credential Manager.
7+
/// Stores and retrieves auth tokens securely via Windows Credential Manager (P/Invoke).
8+
/// No third-party NuGet dependency required.
79
/// </summary>
810
public static class CredentialService
911
{
1012
private const string AppPrefix = "QuotaBar_";
1113

1214
public static void SaveToken(string accountId, string token)
1315
{
14-
var credential = new NetworkCredential(AppPrefix + accountId, token);
15-
CredentialManager.SaveCredentials(AppPrefix + accountId, credential);
16+
var targetName = AppPrefix + accountId;
17+
var tokenBytes = Encoding.UTF8.GetBytes(token);
18+
var blobPtr = Marshal.AllocHGlobal(tokenBytes.Length);
19+
Marshal.Copy(tokenBytes, 0, blobPtr, tokenBytes.Length);
20+
21+
var cred = new NativeMethods.CREDENTIAL
22+
{
23+
Type = 1, // CRED_TYPE_GENERIC
24+
TargetName = targetName,
25+
CredentialBlobSize = tokenBytes.Length,
26+
CredentialBlob = blobPtr,
27+
Persist = 2, // CRED_PERSIST_LOCAL_MACHINE
28+
UserName = targetName
29+
};
30+
31+
try { NativeMethods.CredWrite(ref cred, 0); }
32+
finally { Marshal.FreeHGlobal(blobPtr); }
1633
}
1734

1835
public static string? GetToken(string accountId)
1936
{
37+
if (!NativeMethods.CredRead(AppPrefix + accountId, 1, 0, out var credPtr))
38+
return null;
39+
2040
try
2141
{
22-
var credential = CredentialManager.GetCredentials(AppPrefix + accountId);
23-
return credential?.Password;
42+
var cred = Marshal.PtrToStructure<NativeMethods.CREDENTIAL>(credPtr);
43+
if (cred.CredentialBlobSize == 0) return null;
44+
var bytes = new byte[cred.CredentialBlobSize];
45+
Marshal.Copy(cred.CredentialBlob, bytes, 0, cred.CredentialBlobSize);
46+
return Encoding.UTF8.GetString(bytes);
2447
}
25-
catch
48+
finally
2649
{
27-
return null;
50+
NativeMethods.CredFree(credPtr);
2851
}
2952
}
3053

3154
public static void DeleteToken(string accountId)
3255
{
33-
try
56+
NativeMethods.CredDelete(AppPrefix + accountId, 1, 0);
57+
}
58+
59+
private static class NativeMethods
60+
{
61+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
62+
public struct CREDENTIAL
3463
{
35-
CredentialManager.RemoveCredentials(AppPrefix + accountId);
64+
public int Flags;
65+
public int Type;
66+
public string TargetName;
67+
public string? Comment;
68+
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
69+
public int CredentialBlobSize;
70+
public IntPtr CredentialBlob;
71+
public int Persist;
72+
public int AttributeCount;
73+
public IntPtr Attributes;
74+
public string? TargetAlias;
75+
public string? UserName;
3676
}
37-
catch { /* ignore if not found */ }
77+
78+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
79+
public static extern bool CredWrite([In] ref CREDENTIAL userCredential, [In] uint flags);
80+
81+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
82+
public static extern bool CredRead(string target, int type, int flags, out IntPtr credentialPtr);
83+
84+
[DllImport("advapi32.dll", SetLastError = true)]
85+
public static extern void CredFree([In] IntPtr buffer);
86+
87+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
88+
public static extern bool CredDelete(string target, int type, int flags);
3889
}
3990
}

QuotaBarWindows/Views/AddAccountWindow.xaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,10 @@
8888
</StackPanel>
8989
</Border>
9090

91-
<Button Content="← Back" Style="{StaticResource IconButtonStyle}"
91+
<Button Style="{StaticResource IconButtonStyle}"
9292
HorizontalAlignment="Left" Margin="0,16,0,0"
9393
Click="BackToStep1_Click">
94-
<Button.Content>
95-
<TextBlock Text="← Back" Foreground="#8E8E93" FontSize="12"/>
96-
</Button.Content>
94+
<TextBlock Text="← Back" Foreground="#8E8E93" FontSize="12"/>
9795
</Button>
9896
</StackPanel>
9997

@@ -115,9 +113,9 @@
115113
Margin="0,6,0,0" Visibility="Collapsed" TextWrapping="Wrap"/>
116114

117115
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right"
118-
Margin="0,16,0,0" Spacing="8">
116+
Margin="0,16,0,0">
119117
<Button Content="← Back" Style="{StaticResource SecondaryButtonStyle}"
120-
Click="BackToStep2_Click"/>
118+
Click="BackToStep2_Click" Margin="0,0,8,0"/>
121119
<Button x:Name="ApiKeyConnectButton" Content="Connect"
122120
Style="{StaticResource PrimaryButtonStyle}"
123121
Click="ApiKeyConnect_Click"/>

QuotaBarWindows/Views/PopupWindow.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@
4343
</StackPanel>
4444

4545
<!-- Header Actions -->
46-
<StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="4">
46+
<StackPanel Grid.Column="1" Orientation="Horizontal">
4747
<!-- View toggle -->
4848
<Button x:Name="ViewToggleButton" Style="{StaticResource IconButtonStyle}"
49-
Click="ViewToggle_Click" ToolTip="Toggle compact view">
49+
Click="ViewToggle_Click" ToolTip="Toggle compact view" Margin="0,0,4,0">
5050
<Path x:Name="ViewToggleIcon" Data="M3,5 H21 M3,12 H21 M3,19 H21"
5151
Stroke="#8E8E93" StrokeThickness="1.5" Width="16" Height="14"
5252
Stretch="Uniform"/>
5353
</Button>
5454

5555
<!-- Refresh -->
5656
<Button Style="{StaticResource IconButtonStyle}"
57-
Click="Refresh_Click" ToolTip="Refresh all">
57+
Click="Refresh_Click" ToolTip="Refresh all" Margin="0,0,4,0">
5858
<Path Data="M12,2 A10,10 0 1,1 2,12 M2,12 L2,6 L8,6"
5959
Stroke="#8E8E93" StrokeThickness="1.5" Width="16" Height="16"
6060
Stretch="Uniform" Fill="Transparent"/>

QuotaBarWindows/Views/RenameDialog.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
<DropShadowEffect Color="Black" BlurRadius="20" ShadowDepth="4" Opacity="0.6"/>
1313
</Border.Effect>
1414

15-
<StackPanel MinWidth="280" Spacing="12">
15+
<StackPanel MinWidth="280">
1616
<TextBlock Text="Rename Account" Foreground="White"
1717
FontSize="15" FontWeight="SemiBold"/>
1818

19-
<TextBox x:Name="NameInput" Background="#3A3A3C" Foreground="White"
19+
<TextBox x:Name="NameInput" Background="#3A3A3C" Foreground="White" Margin="0,12,0,0"
2020
BorderThickness="1" BorderBrush="#48484A"
2121
Padding="10,8" FontSize="13" CaretBrush="White"
2222
KeyDown="NameInput_KeyDown"/>
2323

24-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Spacing="8">
24+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,12,0,0">
2525
<Button Content="Cancel" Style="{StaticResource SecondaryButtonStyle}"
26-
Click="Cancel_Click"/>
26+
Click="Cancel_Click" Margin="0,0,8,0"/>
2727
<Button Content="Rename" Style="{StaticResource PrimaryButtonStyle}"
2828
Click="OK_Click"/>
2929
</StackPanel>

QuotaBarWindows/Views/SettingsWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<!-- General section -->
3030
<StackPanel Margin="16,16,16,8">
3131
<TextBlock Text="GENERAL" Foreground="#636366" FontSize="11"
32-
FontWeight="SemiBold" Margin="0,0,0,10" LetterSpacing="1"/>
32+
FontWeight="SemiBold" Margin="0,0,0,10"/>
3333

3434
<!-- Auto-refresh -->
3535
<Border Background="#2C2C2E" CornerRadius="10" Padding="14,12" Margin="0,0,0,8">
@@ -97,7 +97,7 @@
9797
<!-- Accounts section -->
9898
<StackPanel Margin="16,8,16,16">
9999
<TextBlock Text="ACCOUNTS" Foreground="#636366" FontSize="11"
100-
FontWeight="SemiBold" Margin="0,0,0,10" LetterSpacing="1"/>
100+
FontWeight="SemiBold" Margin="0,0,0,10"/>
101101

102102
<ItemsControl x:Name="AccountsList">
103103
<ItemsControl.ItemTemplate>

0 commit comments

Comments
 (0)