1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System;
24: using System.Collections.Generic;
25: using System.Linq;
26: using System.Text;
27: using Utilities.DataTypes;
28: using Utilities.Web.OpenID.Extensions.Enums;
29: using System.Web;
30: #endregion
31:
32: namespace Utilities.Web.OpenID.Extensions.Interfaces
33: {
34: /// <summary>
35: /// Extension interface
36: /// </summary>
37: public interface IExtension
38: {
39: #region Functions
40:
41: /// <summary>
42: /// Generates the attributes in a list of pairs
43: /// </summary>
44: /// <param name="Required">Required attributes</param>
45: /// <returns>A list of attribute pairs</returns>
46: System.Collections.Generic.List<Pair<string, string>> GenerateURLAttributes();
47:
48: /// <summary>
49: /// Parses the URL and gets any attribute values passed back
50: /// </summary>
51: /// <param name="URL">URL</param>
52: /// <param name="Pairs">Query string broken down into attribute pairs</param>
53: /// <returns>True if it's valid, false otherwise</returns>
54: bool Verify(string URL,System.Collections.Generic.List<Pair<string, string>> Pairs);
55:
56: #endregion
57: }
58: }
59:
60: /*
61: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
62:
63: Permission is hereby granted, free of charge, to any person obtaining a copy
64: of this software and associated documentation files (the "Software"), to deal
65: in the Software without restriction, including without limitation the rights
66: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
67: copies of the Software, and to permit persons to whom the Software is
68: furnished to do so, subject to the following conditions:
69:
70: The above copyright notice and this permission notice shall be included in
71: all copies or substantial portions of the Software.
72:
73: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
74: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
75: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
76: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
77: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
78: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
79: THE SOFTWARE.*/
80:
81: #region Usings
82: using System;
83: using System.Collections.Generic;
84: using System.Linq;
85: using System.Text;
86: using Utilities.DataTypes;
87: using Utilities.Web.OpenID.Extensions.Enums;
88: using System.Web;
89: using Utilities.Web.OpenID.Extensions.Interfaces;
90: #endregion
91:
92: namespace Utilities.Web.OpenID.Extensions
93: {
94: /// <summary>
95: /// Attribute exchange extension
96: /// </summary>
97: public class AttributeExchange:IExtension
98: {
99: #region Constructor
100:
101: /// <summary>
102: /// Constructor
103: /// </summary>
104: public AttributeExchange()
105: {
106: Required = Attributes.None;
107: }
108:
109: #endregion
110:
111: #region Properties
112:
113: /// <summary>
114: /// Required attributes
115: /// </summary>
116: public Attributes Required { get; set; }
117:
118: #endregion
119:
120: #region Functions
121:
122: /// <summary>
123: /// Gets a dictionary with the requested values
124: /// </summary>
125: /// <param name="Pairs">Returned attribute pairs</param>
126: /// <returns>A dictionary with the requested values</returns>
127: public Dictionary<Attributes, string> GetValues(System.Collections.Generic.List<Pair<string, string>> Pairs)
128: {
129: try
130: {
131: Dictionary<Attributes, string> ReturnValues = new Dictionary<Attributes, string>();
132: Pair<string, string> Pair = Pairs.Find(x => x.Right == "http://openid.net/srv/ax/1.0");
133: string[] Splitter = { "." };
134: string Extension = Pair.Left.Split(Splitter, StringSplitOptions.None)[2];
135:
136: if ((Required & Attributes.Address) == Attributes.Address)
137: {
138: ReturnValues.Add(Attributes.Address, Pairs.Find(x => x.Left == "openid." + Extension + ".value.address").Right);
139: }
140: if ((Required & Attributes.BirthDate) == Attributes.BirthDate)
141: {
142: ReturnValues.Add(Attributes.BirthDate, Pairs.Find(x => x.Left == "openid." + Extension + ".value.birthdate").Right);
143: }
144: if ((Required & Attributes.CompanyName) == Attributes.CompanyName)
145: {
146: ReturnValues.Add(Attributes.CompanyName, Pairs.Find(x => x.Left == "openid." + Extension + ".value.companyname").Right);
147: }
148: if ((Required & Attributes.Country) == Attributes.Country)
149: {
150: ReturnValues.Add(Attributes.Country, Pairs.Find(x => x.Left == "openid." + Extension + ".value.country").Right);
151: }
152: if ((Required & Attributes.Email) == Attributes.Email)
153: {
154: ReturnValues.Add(Attributes.Email, Pairs.Find(x => x.Left == "openid." + Extension + ".value.email").Right);
155: }
156: if ((Required & Attributes.FirstName) == Attributes.FirstName)
157: {
158: ReturnValues.Add(Attributes.FirstName, Pairs.Find(x => x.Left == "openid." + Extension + ".value.firstname").Right);
159: }
160: if ((Required & Attributes.FullName) == Attributes.FullName)
161: {
162: ReturnValues.Add(Attributes.FullName, Pairs.Find(x => x.Left == "openid." + Extension + ".value.fullname").Right);
163: }
164: if ((Required & Attributes.Gender) == Attributes.Gender)
165: {
166: ReturnValues.Add(Attributes.Gender, Pairs.Find(x => x.Left == "openid." + Extension + ".value.gender").Right);
167: }
168: if ((Required & Attributes.JobTitle) == Attributes.JobTitle)
169: {
170: ReturnValues.Add(Attributes.JobTitle, Pairs.Find(x => x.Left == "openid." + Extension + ".value.jobtitle").Right);
171: }
172: if ((Required & Attributes.Language) == Attributes.Language)
173: {
174: ReturnValues.Add(Attributes.Language, Pairs.Find(x => x.Left == "openid." + Extension + ".value.language").Right);
175: }
176: if ((Required & Attributes.LastName) == Attributes.LastName)
177: {
178: ReturnValues.Add(Attributes.LastName, Pairs.Find(x => x.Left == "openid." + Extension + ".value.lastname").Right);
179: }
180: if ((Required & Attributes.Phone) == Attributes.Phone)
181: {
182: ReturnValues.Add(Attributes.Phone, Pairs.Find(x => x.Left == "openid." + Extension + ".value.phone").Right);
183: }
184: if ((Required & Attributes.PostalCode) == Attributes.PostalCode)
185: {
186: ReturnValues.Add(Attributes.PostalCode, Pairs.Find(x => x.Left == "openid." + Extension + ".value.postalcode").Right);
187: }
188: if ((Required & Attributes.TimeZone) == Attributes.TimeZone)
189: {
190: ReturnValues.Add(Attributes.TimeZone, Pairs.Find(x => x.Left == "openid." + Extension + ".value.timezone").Right);
191: }
192: if ((Required & Attributes.UserName) == Attributes.UserName)
193: {
194: ReturnValues.Add(Attributes.UserName, Pairs.Find(x => x.Left == "openid." + Extension + ".value.username").Right);
195: }
196: return ReturnValues;
197: }
198: catch { throw; }
199: }
200:
201: public bool Verify(string URL, System.Collections.Generic.List<Pair<string, string>> Pairs)
202: {
203: try
204: {
205: Pair<string, string> Pair = Pairs.Find(x => x.Right == "http://openid.net/srv/ax/1.0");
206: if (Pair == null && Required != Attributes.None)
207: return false;
208: else if (Pair == null)
209: return true;
210: string[] Splitter = { "." };
211: string Extension = Pair.Left.Split(Splitter, StringSplitOptions.None)[2];
212:
213: if ((Required & Attributes.Address) == Attributes.Address)
214: {
215: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.address") == null)
216: return false;
217: }
218: if ((Required & Attributes.BirthDate) == Attributes.BirthDate)
219: {
220: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.birthdate") == null)
221: return false;
222: }
223: if ((Required & Attributes.CompanyName) == Attributes.CompanyName)
224: {
225: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.companyname") == null)
226: return false;
227: }
228: if ((Required & Attributes.Country) == Attributes.Country)
229: {
230: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.country") == null)
231: return false;
232: }
233: if ((Required & Attributes.Email) == Attributes.Email)
234: {
235: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.email") == null)
236: return false;
237: }
238: if ((Required & Attributes.FirstName) == Attributes.FirstName)
239: {
240: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.firstname") == null)
241: return false;
242: }
243: if ((Required & Attributes.FullName) == Attributes.FullName)
244: {
245: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.fullname") == null)
246: return false;
247: }
248: if ((Required & Attributes.Gender) == Attributes.Gender)
249: {
250: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.gender") == null)
251: return false;
252: }
253: if ((Required & Attributes.JobTitle) == Attributes.JobTitle)
254: {
255: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.jobtitle") == null)
256: return false;
257: }
258: if ((Required & Attributes.Language) == Attributes.Language)
259: {
260: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.language") == null)
261: return false;
262: }
263: if ((Required & Attributes.LastName) == Attributes.LastName)
264: {
265: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.lastname") == null)
266: return false;
267: }
268: if ((Required & Attributes.Phone) == Attributes.Phone)
269: {
270: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.phone") == null)
271: return false;
272: }
273: if ((Required & Attributes.PostalCode) == Attributes.PostalCode)
274: {
275: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.postalcode") == null)
276: return false;
277: }
278: if ((Required & Attributes.TimeZone) == Attributes.TimeZone)
279: {
280: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.timezone") == null)
281: return false;
282: }
283: if ((Required & Attributes.UserName) == Attributes.UserName)
284: {
285: if (Pairs.Find(x => x.Left == "openid." + Extension + ".value.username") == null)
286: return false;
287: }
288: return true;
289: }
290: catch { throw; }
291: }
292:
293: public System.Collections.Generic.List<Pair<string, string>> GenerateURLAttributes()
294: {
295: try
296: {
297: System.Collections.Generic.List<Pair<string, string>> ReturnValues = new System.Collections.Generic.List<Pair<string, string>>();
298: if (Required == Attributes.None)
299: return ReturnValues;
300: ReturnValues.Add(new Pair<string, string>("openid.ns.ax", HttpUtility.UrlEncode("http://openid.net/srv/ax/1.0")));
301: ReturnValues.Add(new Pair<string, string>("openid.ax.mode", "fetch_request"));
302: if ((Required & Attributes.Address) == Attributes.Address)
303: {
304: ReturnValues.Add(new Pair<string, string>("openid.ax.type.address", HttpUtility.UrlEncode("http://axschema.org/contact/postalAddress/home")));
305: }
306: if ((Required & Attributes.BirthDate) == Attributes.BirthDate)
307: {
308: ReturnValues.Add(new Pair<string, string>("openid.ax.type.birthdate", HttpUtility.UrlEncode("http://axschema.org/birthDate")));
309: }
310: if ((Required & Attributes.CompanyName) == Attributes.CompanyName)
311: {
312: ReturnValues.Add(new Pair<string, string>("openid.ax.type.companyname", HttpUtility.UrlEncode("http://axschema.org/company/name")));
313: }
314: if ((Required & Attributes.Country) == Attributes.Country)
315: {
316: ReturnValues.Add(new Pair<string, string>("openid.ax.type.country", HttpUtility.UrlEncode("http://axschema.org/contact/country/home")));
317: }
318: if ((Required & Attributes.Email) == Attributes.Email)
319: {
320: ReturnValues.Add(new Pair<string, string>("openid.ax.type.email", HttpUtility.UrlEncode("http://axschema.org/contact/email")));
321: }
322: if ((Required & Attributes.FirstName) == Attributes.FirstName)
323: {
324: ReturnValues.Add(new Pair<string, string>("openid.ax.type.firstname", HttpUtility.UrlEncode("http://axschema.org/namePerson/first")));
325: }
326: if ((Required & Attributes.FullName) == Attributes.FullName)
327: {
328: ReturnValues.Add(new Pair<string, string>("openid.ax.type.fullname", HttpUtility.UrlEncode("http://axschema.org/namePerson")));
329: }
330: if ((Required & Attributes.Gender) == Attributes.Gender)
331: {
332: ReturnValues.Add(new Pair<string, string>("openid.ax.type.gender", HttpUtility.UrlEncode("http://axschema.org/person/gender")));
333: }
334: if ((Required & Attributes.JobTitle) == Attributes.JobTitle)
335: {
336: ReturnValues.Add(new Pair<string, string>("openid.ax.type.jobtitle", HttpUtility.UrlEncode("http://axschema.org/company/title")));
337: }
338: if ((Required & Attributes.Language) == Attributes.Language)
339: {
340: ReturnValues.Add(new Pair<string, string>("openid.ax.type.language", HttpUtility.UrlEncode("http://axschema.org/pref/language")));
341: }
342: if ((Required & Attributes.LastName) == Attributes.LastName)
343: {
344: ReturnValues.Add(new Pair<string, string>("openid.ax.type.lastname", HttpUtility.UrlEncode("http://axschema.org/namePerson/last")));
345: }
346: if ((Required & Attributes.Phone) == Attributes.Phone)
347: {
348: ReturnValues.Add(new Pair<string, string>("openid.ax.type.phone", HttpUtility.UrlEncode("http://axschema.org/contact/phone/default")));
349: }
350: if ((Required & Attributes.PostalCode) == Attributes.PostalCode)
351: {
352: ReturnValues.Add(new Pair<string, string>("openid.ax.type.postalcode", HttpUtility.UrlEncode("http://axschema.org/contact/postalCode/home")));
353: }
354: if ((Required & Attributes.TimeZone) == Attributes.TimeZone)
355: {
356: ReturnValues.Add(new Pair<string, string>("openid.ax.type.timezone", HttpUtility.UrlEncode("http://axschema.org/pref/timezone")));
357: }
358: if ((Required & Attributes.UserName) == Attributes.UserName)
359: {
360: ReturnValues.Add(new Pair<string, string>("openid.ax.type.username", HttpUtility.UrlEncode("http://axschema.org/namePerson/friendly")));
361: }
362: ReturnValues.Add(new Pair<string, string>("openid.ax.required", Required.ToString().ToLower().Replace(" ", "")));
363: return ReturnValues;
364: }
365: catch { throw; }
366: }
367:
368: #endregion
369: }
370: }