8
8
using Akavache ;
9
9
using GitHub . Helpers ;
10
10
using GitHub . Authentication . CredentialManagement ;
11
+ using System . Security ;
11
12
12
13
namespace GitHub . Caches
13
14
{
@@ -32,7 +33,6 @@ public IObservable<byte[]> Get(string key)
32
33
using ( var credential = new Credential ( ) )
33
34
{
34
35
credential . Target = keyHost ;
35
- credential . Type = CredentialType . Generic ;
36
36
if ( credential . Load ( ) )
37
37
return Observable . Return ( Encoding . Unicode . GetBytes ( credential . Password ) ) ;
38
38
}
@@ -79,15 +79,33 @@ public IObservable<Unit> Vacuum()
79
79
throw new NotImplementedException ( ) ;
80
80
}
81
81
82
- // TODO: Use SecureString
83
82
public IObservable < Unit > InsertObject < T > ( string key , T value , DateTimeOffset ? absoluteExpiration = default ( DateTimeOffset ? ) )
84
83
{
85
84
if ( disposed ) return ExceptionHelper . ObservableThrowObjectDisposedException < Unit > ( "CredentialCache" ) ;
86
85
87
- var values = value as Tuple < string , string > ;
88
- if ( values == null )
89
- return ExceptionHelper . ObservableThrowInvalidOperationException < Unit > ( key ) ;
86
+ if ( value is Tuple < string , string > )
87
+ return InsertTuple ( key , value as Tuple < string , string > ) ;
88
+ if ( value is Tuple < string , SecureString > )
89
+ return InsertTuple ( key , value as Tuple < string , SecureString > ) ;
90
90
91
+ return ExceptionHelper . ObservableThrowInvalidOperationException < Unit > ( key ) ;
92
+ }
93
+
94
+ static IObservable < Unit > InsertTuple ( string key , Tuple < string , string > values )
95
+ {
96
+ var keyGit = GetKeyGit ( key ) ;
97
+ if ( ! SaveKey ( keyGit , values . Item1 , values . Item2 ) )
98
+ return ExceptionHelper . ObservableThrowInvalidOperationException < Unit > ( keyGit ) ;
99
+
100
+ var keyHost = GetKeyHost ( key ) ;
101
+ if ( ! SaveKey ( keyHost , values . Item1 , values . Item2 ) )
102
+ return ExceptionHelper . ObservableThrowInvalidOperationException < Unit > ( keyGit ) ;
103
+
104
+ return Observable . Return ( Unit . Default ) ;
105
+ }
106
+
107
+ static IObservable < Unit > InsertTuple ( string key , Tuple < string , SecureString > values )
108
+ {
91
109
var keyGit = GetKeyGit ( key ) ;
92
110
if ( ! SaveKey ( keyGit , values . Item1 , values . Item2 ) )
93
111
return ExceptionHelper . ObservableThrowInvalidOperationException < Unit > ( keyGit ) ;
@@ -103,6 +121,8 @@ public IObservable<T> GetObject<T>(string key)
103
121
{
104
122
if ( typeof ( T ) == typeof ( Tuple < string , string > ) )
105
123
return ( IObservable < T > ) GetTuple ( key ) ;
124
+ if ( typeof ( T ) == typeof ( Tuple < string , SecureString > ) )
125
+ return ( IObservable < T > ) GetSecureTuple ( key ) ;
106
126
return ExceptionHelper . ObservableThrowInvalidOperationException < T > ( key ) ;
107
127
}
108
128
@@ -117,6 +137,17 @@ IObservable<Tuple<string, string>> GetTuple(string key)
117
137
: ExceptionHelper . ObservableThrowKeyNotFoundException < Tuple < string , string > > ( keyHost ) ;
118
138
}
119
139
140
+ IObservable < Tuple < string , SecureString > > GetSecureTuple ( string key )
141
+ {
142
+ if ( disposed ) return ExceptionHelper . ObservableThrowObjectDisposedException < Tuple < string , SecureString > > ( "CredentialCache" ) ;
143
+
144
+ var keyHost = GetKeyHost ( key ) ;
145
+ var ret = GetSecureKey ( keyHost ) ;
146
+ return ret != null
147
+ ? Observable . Return ( ret )
148
+ : ExceptionHelper . ObservableThrowKeyNotFoundException < Tuple < string , SecureString > > ( keyHost ) ;
149
+ }
150
+
120
151
public IObservable < IEnumerable < T > > GetAllObjects < T > ( )
121
152
{
122
153
throw new NotImplementedException ( ) ;
@@ -188,8 +219,14 @@ static bool SaveKey(string key, string user, string pwd)
188
219
{
189
220
using ( var credential = new Credential ( user , pwd , key ) )
190
221
{
191
- credential . Type = CredentialType . Generic ;
192
- credential . PersistenceType = PersistenceType . LocalComputer ;
222
+ return credential . Save ( ) ;
223
+ }
224
+ }
225
+
226
+ static bool SaveKey ( string key , string user , SecureString pwd )
227
+ {
228
+ using ( var credential = new Credential ( user , pwd , key ) )
229
+ {
193
230
return credential . Save ( ) ;
194
231
}
195
232
}
@@ -199,13 +236,23 @@ static Tuple<string, string> GetKey(string key)
199
236
using ( var credential = new Credential ( ) )
200
237
{
201
238
credential . Target = key ;
202
- credential . Type = CredentialType . Generic ;
203
239
return credential . Load ( )
204
240
? new Tuple < string , string > ( credential . Username , credential . Password )
205
241
: null ;
206
242
}
207
243
}
208
244
245
+ static Tuple < string , SecureString > GetSecureKey ( string key )
246
+ {
247
+ using ( var credential = new Credential ( ) )
248
+ {
249
+ credential . Target = key ;
250
+ return credential . Load ( )
251
+ ? new Tuple < string , SecureString > ( credential . Username , credential . SecurePassword )
252
+ : null ;
253
+ }
254
+ }
255
+
209
256
bool disposed ;
210
257
void Dispose ( bool disposing )
211
258
{
0 commit comments