@@ -32,6 +32,9 @@ internal partial class UWPAccountStore
32
32
{
33
33
public override async Task < List < Account > > FindAccountsForServiceAsync ( string serviceId )
34
34
{
35
+ if ( serviceId ? . Length == 0 )
36
+ throw new ArgumentNullException ( nameof ( serviceId ) ) ;
37
+
35
38
var localFolder = ApplicationData . Current . LocalFolder ;
36
39
37
40
var files = await localFolder . GetFilesAsync ( ) . AsTask ( ) . ConfigureAwait ( false ) ;
@@ -59,6 +62,13 @@ public override async Task<List<Account>> FindAccountsForServiceAsync(string ser
59
62
60
63
public override async Task DeleteAsync ( Account account , string serviceId )
61
64
{
65
+ if ( account == null )
66
+ throw new ArgumentNullException ( nameof ( account ) ) ;
67
+ if ( account . Username ? . Length == 0 )
68
+ throw new ArgumentOutOfRangeException ( nameof ( account ) , "Username cannot be empty" ) ;
69
+ if ( serviceId ? . Length == 0 )
70
+ throw new ArgumentNullException ( nameof ( serviceId ) ) ;
71
+
62
72
var path = GetAccountPath ( account , serviceId ) ;
63
73
try
64
74
{
@@ -72,26 +82,21 @@ public override async Task DeleteAsync(Account account, string serviceId)
72
82
}
73
83
}
74
84
75
- public override async Task SaveAsync ( Account account , string serviceId )
85
+ public override Task SaveAsync ( Account account , string serviceId )
76
86
{
77
- byte [ ] data = Encoding . UTF8 . GetBytes ( account . Serialize ( ) ) ;
78
- byte [ ] prot = ( await DataProtectionExtensions . ProtectAsync ( data . AsBuffer ( ) ) . ConfigureAwait ( false ) ) . ToArray ( ) ;
79
-
80
- var path = GetAccountPath ( account , serviceId ) ;
81
-
82
- var localFolder = ApplicationData . Current . LocalFolder ;
83
- var file = await localFolder . CreateFileAsync ( path , CreationCollisionOption . OpenIfExists ) . AsTask ( ) . ConfigureAwait ( false ) ;
84
- using ( var stream = await file . OpenStreamForWriteAsync ( ) . ConfigureAwait ( false ) )
85
- using ( var writer = new BinaryWriter ( stream ) )
86
- {
87
- writer . Write ( ( Int32 ) prot . Length ) ;
88
- writer . Write ( prot ) ;
89
- }
87
+ return SaveAsync ( account , serviceId , null ) ;
90
88
}
91
89
92
90
public async Task SaveAsync ( Account account , string serviceId , Uri uri )
93
91
{
94
- byte [ ] data = Encoding . UTF8 . GetBytes ( account . Serialize ( uri ) ) ;
92
+ if ( account == null )
93
+ throw new ArgumentNullException ( nameof ( account ) ) ;
94
+ if ( account . Username ? . Length == 0 )
95
+ throw new ArgumentOutOfRangeException ( nameof ( account ) , "Username cannot be empty" ) ;
96
+ if ( serviceId ? . Length == 0 )
97
+ throw new ArgumentNullException ( nameof ( serviceId ) ) ;
98
+
99
+ byte [ ] data = Encoding . UTF8 . GetBytes ( uri == null ? account . Serialize ( ) : account . Serialize ( uri ) ) ;
95
100
byte [ ] prot = ( await DataProtectionExtensions . ProtectAsync ( data . AsBuffer ( ) ) . ConfigureAwait ( false ) ) . ToArray ( ) ;
96
101
97
102
var path = GetAccountPath ( account , serviceId ) ;
@@ -107,8 +112,21 @@ public async Task SaveAsync(Account account, string serviceId, Uri uri)
107
112
}
108
113
109
114
private static string GetAccountPath ( Account account , string serviceId )
110
- {
111
- return String . Format ( "xamarin.auth.{0}.{1}" , account . Username , serviceId ) ;
115
+ {
116
+ if ( account == null )
117
+ throw new ArgumentNullException ( nameof ( account ) ) ;
118
+ if ( account . Username ? . Length == 0 )
119
+ throw new ArgumentOutOfRangeException ( nameof ( account ) , "Username cannot be empty" ) ;
120
+ if ( serviceId ? . Length == 0 )
121
+ throw new ArgumentNullException ( nameof ( serviceId ) ) ;
122
+
123
+ string fileName = account . Username ;
124
+ foreach ( char c in Path . GetInvalidFileNameChars ( ) )
125
+ {
126
+ fileName = fileName . Replace ( c , '_' ) ;
127
+ }
128
+
129
+ return $ "xamarin.auth.{ fileName } .{ serviceId } ";
112
130
}
113
131
}
114
132
}
0 commit comments