-
Notifications
You must be signed in to change notification settings - Fork 177
/
IAuthTokenStorage.cs
85 lines (65 loc) · 2.72 KB
/
IAuthTokenStorage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Middleware.AzureAdAuthentication
{
public interface IAuthTokenStorage
{
ConversationAuthToken LoadConfiguration(string id);
void SaveConfiguration(ConversationAuthToken state);
}
public class ConversationAuthToken
{
public ConversationAuthToken(string id)
{
Id = id;
}
public string Id { get; set; }
// Note this is stored in memory in plain text for demonstration purposes
// use your common sense when applying this in your apps! i.e. take appropriate precautions
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public DateTime ExpiresIn { get; set; }
}
public class InMemoryAuthTokenStorage : IAuthTokenStorage
{
private static readonly Dictionary<string, ConversationAuthToken> InMemoryDictionary = new Dictionary<string, ConversationAuthToken>();
public ConversationAuthToken LoadConfiguration(string id)
{
if (InMemoryDictionary.ContainsKey(id))
{
return InMemoryDictionary[id];
}
return null;
}
public void SaveConfiguration(ConversationAuthToken state)
{
InMemoryDictionary[state.Id] = state;
}
}
public class DiskAuthTokenStorage : IAuthTokenStorage
{
//private static readonly Dictionary<string, ConversationAuthToken> InMemoryDictionary = new Dictionary<string, ConversationAuthToken>();
public ConversationAuthToken LoadConfiguration(string id)
{
id = "singletonkey";
Dictionary<string, ConversationAuthToken> values = File.Exists("accessTokens.json") ? JsonConvert.DeserializeObject<Dictionary<string, ConversationAuthToken>>(File.ReadAllText("accessTokens.json")) : new Dictionary<string, ConversationAuthToken>();
if (values.ContainsKey(id))
{
return values[id];
}
return null;
}
public void SaveConfiguration(ConversationAuthToken state)
{
state.Id = "singletonkey";
Dictionary<string, ConversationAuthToken> values = File.Exists("accessTokens.json") ? JsonConvert.DeserializeObject<Dictionary<string, ConversationAuthToken>>(File.ReadAllText("accessTokens.json")) : new Dictionary<string, ConversationAuthToken>();
values[state.Id] = state;
File.WriteAllText("accessTokens.json", JsonConvert.SerializeObject(values));
}
}
// write azure key value token storage
}