-
Notifications
You must be signed in to change notification settings - Fork 0
/
EchoBackplane.cs
199 lines (167 loc) · 6.13 KB
/
EchoBackplane.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Web;
using System.Net;
using System.IO;
namespace EchoAPI
{
public class BackplaneJSON
{
#region Properties
public string PostMessage
{
get
{
return "[{{" +
"\"source\": \"{0}\"," +
"\"type\": \"{1}\"," +
"\"payload\": {{" +
"\"context\": \"{2}\"," +
"\"identities\": {{" +
"\"startIndex\": 0," +
"\"itemsPerPage\": 1," +
"\"totalResults\": 1," +
"\"entry\": {{" +
"\"id\": \"{3}\"," +
"\"displayName\": \"{4}\"," +
"\"accounts\": [{{" +
"\"identityUrl\": \"{5}\"," +
"\"username\": \"{6}\"," +
"\"photos\": [{{" +
"\"value\": \"{7}\"," +
"\"type\": \"avatar\"" +
"}}]" +
"}}]" +
"}}" +
"}}" +
"}}" +
"}}]";
}
private set {}
}
public string Source { get; set; }
public string Context { get; set; }
public string Id { get; set; }
public string DisplayName { get; set; }
public string IdentityUrl { get; set; }
public string Username { get; set; }
public string Value { get; set; }
#endregion
public string GeneratePostData(string type)
{
//Necessary: Source, Id, IdentityUrl
if (String.IsNullOrEmpty(this.Source) || String.IsNullOrEmpty(this.Id) ||
String.IsNullOrEmpty(this.IdentityUrl))
{
return "";
}
return String.Format(PostMessage,
this.Source,
type,
this.Context,
this.Id,
this.DisplayName,
this.IdentityUrl,
this.Username,
this.Value
);
}
}
public class Backplane
{
public string Authorization { get; set; }
#region Constructors
public Backplane(string busname, string secret)
{
this.Authorization = generateAutorization(busname, secret);
}
public Backplane(string encodedAutorization)
{
this.Authorization = encodedAutorization;
}
#endregion
#region private getters
private string generateAutorization(string busname, string secret)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return Convert.ToBase64String(encoding.GetBytes(busname + ":" + secret));
}
private string getChannelId()
{
return HttpContext.Current.Request.Cookies["bp_channel_id"] != null ? HttpContext.Current.Request.Cookies["bp_channel_id"].Value : "";
}
#endregion
#region Methods
public bool SendRequest(string postData)
{
if (String.IsNullOrEmpty(postData))
{
//The post message wasn't initialized correctly.
return false;
}
const string ContentType = "application/x-www-form-urlencoded";
string channelId = getChannelId(),
responseString = "";
bool result = false;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelId);
request.ContentType = ContentType;
request.Method = "POST";
request.Headers.Add("Authorization", "Basic " + this.Authorization);
using (Stream st = request.GetRequestStream())
{
using (StreamWriter stw = new StreamWriter(st))
{
stw.Write(postData);
}
}
var response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
responseString = reader.ReadToEnd();
}
result = (responseString == "{ \"result\": \"success\" }");
}
catch (Exception e)
{
//Something went wrong, check the bp_channel_id cookie or the postData.
result = false;
}
return result;
}
public bool Login(string baseUrl, string username, string userAvatar)
{
BackplaneJSON parameters = new BackplaneJSON()
{
Source = baseUrl,
Context = baseUrl,
Id = baseUrl + "/" + username,
DisplayName = username,
IdentityUrl = baseUrl + "/" + username,
Username = username,
Value = userAvatar,
};
return this.Login(parameters);
}
public bool Login(BackplaneJSON parameters)
{
return this.SendRequest(parameters.GeneratePostData("identity/login"));
}
public bool Logout(string baseUrl, string username)
{
BackplaneJSON parameters = new BackplaneJSON()
{
Source = baseUrl,
Id = baseUrl + "/" + username,
IdentityUrl = baseUrl + "/" + username
};
return this.Logout(parameters);
}
public bool Logout(BackplaneJSON parameters)
{
return this.SendRequest(parameters.GeneratePostData("identity/logout"));
}
#endregion
}
}