88using Akavache ;
99using GitHub . Helpers ;
1010using GitHub . Authentication . CredentialManagement ;
11+ using System . Security ;
1112
1213namespace GitHub . Caches
1314{
@@ -32,7 +33,6 @@ public IObservable<byte[]> Get(string key)
3233 using ( var credential = new Credential ( ) )
3334 {
3435 credential . Target = keyHost ;
35- credential . Type = CredentialType . Generic ;
3636 if ( credential . Load ( ) )
3737 return Observable . Return ( Encoding . Unicode . GetBytes ( credential . Password ) ) ;
3838 }
@@ -79,15 +79,33 @@ public IObservable<Unit> Vacuum()
7979 throw new NotImplementedException ( ) ;
8080 }
8181
82- // TODO: Use SecureString
8382 public IObservable < Unit > InsertObject < T > ( string key , T value , DateTimeOffset ? absoluteExpiration = default ( DateTimeOffset ? ) )
8483 {
8584 if ( disposed ) return ExceptionHelper . ObservableThrowObjectDisposedException < Unit > ( "CredentialCache" ) ;
8685
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 > ) ;
9090
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+ {
91109 var keyGit = GetKeyGit ( key ) ;
92110 if ( ! SaveKey ( keyGit , values . Item1 , values . Item2 ) )
93111 return ExceptionHelper . ObservableThrowInvalidOperationException < Unit > ( keyGit ) ;
@@ -103,6 +121,8 @@ public IObservable<T> GetObject<T>(string key)
103121 {
104122 if ( typeof ( T ) == typeof ( Tuple < string , string > ) )
105123 return ( IObservable < T > ) GetTuple ( key ) ;
124+ if ( typeof ( T ) == typeof ( Tuple < string , SecureString > ) )
125+ return ( IObservable < T > ) GetSecureTuple ( key ) ;
106126 return ExceptionHelper . ObservableThrowInvalidOperationException < T > ( key ) ;
107127 }
108128
@@ -117,6 +137,17 @@ IObservable<Tuple<string, string>> GetTuple(string key)
117137 : ExceptionHelper . ObservableThrowKeyNotFoundException < Tuple < string , string > > ( keyHost ) ;
118138 }
119139
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+
120151 public IObservable < IEnumerable < T > > GetAllObjects < T > ( )
121152 {
122153 throw new NotImplementedException ( ) ;
@@ -188,8 +219,14 @@ static bool SaveKey(string key, string user, string pwd)
188219 {
189220 using ( var credential = new Credential ( user , pwd , key ) )
190221 {
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+ {
193230 return credential . Save ( ) ;
194231 }
195232 }
@@ -199,13 +236,23 @@ static Tuple<string, string> GetKey(string key)
199236 using ( var credential = new Credential ( ) )
200237 {
201238 credential . Target = key ;
202- credential . Type = CredentialType . Generic ;
203239 return credential . Load ( )
204240 ? new Tuple < string , string > ( credential . Username , credential . Password )
205241 : null ;
206242 }
207243 }
208244
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+
209256 bool disposed ;
210257 void Dispose ( bool disposing )
211258 {
0 commit comments