Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 Copyright 2014 Google Inc | |
3 | |
4 Licensed under the Apache License, Version 2.0 (the "License"); | |
5 you may not use this file except in compliance with the License. | |
6 You may obtain a copy of the License at | |
7 | |
8 http://www.apache.org/licenses/LICENSE-2.0 | |
9 | |
10 Unless required by applicable law or agreed to in writing, software | |
11 distributed under the License is distributed on an "AS IS" BASIS, | |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 See the License for the specific language governing permissions and | |
14 limitations under the License. | |
15 */ | |
16 | |
17 using System; | |
18 using System.Threading.Tasks; | |
19 using Windows.Security.Credentials; | |
20 | |
21 using Google.Apis.Json; | |
22 using Google.Apis.Util.Store; | |
23 | |
24 namespace Google.Apis.Util.Store | |
25 { | |
26 /// <summary> | |
27 /// Credentials store that implements <see cref="IDataStore"/>. | |
28 /// This store saves all keys encrypted in a <see cref="PasswordVault"/> | |
29 /// </summary> | |
30 public class PasswordVaultDataStore : IDataStore | |
31 { | |
32 public static PasswordVaultDataStore Default = new PasswordVaultDataStor e(); | |
33 PasswordVault passwordVault = new PasswordVault(); | |
34 | |
35 /// <summary>Adds a new key to the password vault.</summary> | |
36 public Task StoreAsync<T>(string key, T value) | |
37 { | |
38 passwordVault.Add(new PasswordCredential( | |
39 typeof(T).ToString(), | |
40 key, | |
41 NewtonsoftJsonSerializer.Instance.Serialize(value))); | |
42 return TaskEx.Delay(0); | |
jmcgrew
2014/11/25 20:46:59
If available in this framework version, Task.Compl
peleyal
2014/11/26 02:54:01
Task.CompletedTask isn't exposed.
It looks OK to m
| |
43 } | |
44 | |
45 /// <summary>Deletes a given key from the password vault.</summary> | |
46 public Task DeleteAsync<T>(string key) | |
47 { | |
48 try | |
49 { | |
50 PasswordCredential credential = passwordVault.Retrieve(typeof(T) .ToString(), key); | |
51 passwordVault.Remove(credential); | |
52 } | |
53 // PasswordVault.Retrieve might throw a general exception. DO NOTHIN G, there is no value for this key. | |
54 catch (Exception) { } | |
55 return TaskEx.Delay(0); | |
56 } | |
57 | |
58 /// <summary> | |
59 /// Gets a specific key from the password vault. Returns <c>default(T)</ c> if there is no matching entry. | |
60 /// </summary> | |
61 public Task<T> GetAsync<T>(string key) | |
62 { | |
63 TaskCompletionSource<T> tcs = new TaskCompletionSource<T>(); | |
64 try | |
65 { | |
66 PasswordCredential credential = passwordVault.Retrieve(typeof(T) .ToString(), key); | |
67 // No matching entry. | |
68 if (credential == null) | |
69 { | |
70 tcs.SetResult(default(T)); | |
71 } | |
72 else | |
73 { | |
74 credential.RetrievePassword(); | |
75 tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize< T>(credential.Password)); | |
76 } | |
77 } | |
78 // PasswordVault.Retrieve might throw a general exception, which mea n there isn't a matching entry. | |
79 catch (Exception) | |
80 { | |
81 tcs.SetResult(default(T)); | |
82 } | |
83 return tcs.Task; | |
84 } | |
85 | |
86 /// <summary>Removes all values from the password vault.</summary> | |
87 public Task ClearAsync() | |
88 { | |
89 try | |
90 { | |
91 foreach (var credential in passwordVault.RetrieveAll()) | |
92 { | |
93 passwordVault.Remove(credential); | |
94 } | |
95 } | |
96 // PasswordVault.RetrieveAll might throw a general exception - DO NO THING, there are no values to remove. | |
97 catch (Exception) { } | |
98 return TaskEx.Delay(0); | |
99 } | |
100 } | |
101 } | |
OLD | NEW |