source: binary-improvements/MapRendering/Web/OpenID.cs@ 311

Last change on this file since 311 was 244, checked in by alloc, 9 years ago

Fixes intermediate state

File size: 4.2 KB
RevLine 
[244]1using System;
2using System.Collections.Generic;
3using System.Collections.Specialized;
4using System.IO;
5using System.Net;
6using System.Net.Security;
7using System.Text;
8using System.Text.RegularExpressions;
9
10namespace AllocsFixes.NetConnections.Servers.Web
11{
12 public static class OpenID {
13 private const string STEAM_LOGIN = "https://steamcommunity.com/openid/login";
14 private static Regex steamIdUrlMatcher = new Regex (@"^http:\/\/steamcommunity\.com\/openid\/id\/([0-9]{17,18})");
15
16 static OpenID () {
17 ServicePointManager.ServerCertificateValidationCallback = (srvPoint, certificate, chain, errors) => {
18 if (errors == SslPolicyErrors.None)
19 return true;
20
21 Log.Out ("Steam certificate error: {0}", errors);
22
23 return true;
24 };
25
26 }
27
28 public static string GetOpenIdLoginUrl (string _returnHost, string _returnUrl) {
29 Dictionary<string, string> queryParams = new Dictionary<string, string> ();
30
31 queryParams.Add ("openid.ns", "http://specs.openid.net/auth/2.0");
32 queryParams.Add ("openid.mode", "checkid_setup");
33 queryParams.Add ("openid.return_to", _returnUrl);
34 queryParams.Add ("openid.realm", _returnHost);
35 queryParams.Add ("openid.identity", "http://specs.openid.net/auth/2.0/identifier_select");
36 queryParams.Add ("openid.claimed_id", "http://specs.openid.net/auth/2.0/identifier_select");
37
38 return STEAM_LOGIN + '?' + buildUrlParams (queryParams);
39 }
40
41 public static ulong Validate (HttpListenerRequest _req) {
42 if (getValue (_req, "openid.mode") == "cancel") {
43 Log.Warning ("Steam OpenID login canceled");
44 return 0;
45 }
46 string steamIdString = getValue (_req, "openid.claimed_id");
47 ulong steamId = 0;
48 Match steamIdMatch = steamIdUrlMatcher.Match (steamIdString);
49 if (steamIdMatch.Success) {
50 steamId = ulong.Parse (steamIdMatch.Groups [1].Value);
51 } else {
52 Log.Warning ("Steam OpenID login result did not give a valid SteamID");
53 return 0;
54 }
55
56 Dictionary<string, string> queryParams = new Dictionary<string, string> ();
57
58 queryParams.Add ("openid.ns", "http://specs.openid.net/auth/2.0");
59
60 queryParams.Add ("openid.assoc_handle", getValue (_req, "openid.assoc_handle"));
61 queryParams.Add ("openid.signed", getValue (_req, "openid.signed"));
62 queryParams.Add ("openid.sig", getValue (_req, "openid.sig"));
63 queryParams.Add ("openid.identity", "http://specs.openid.net/auth/2.0/identifier_select");
64 queryParams.Add ("openid.claimed_id", "http://specs.openid.net/auth/2.0/identifier_select");
65
66 string[] signeds = getValue (_req, "openid.signed").Split (',');
67 foreach (string s in signeds) {
68 queryParams ["openid." + s] = getValue (_req, "openid." + s);
69 }
70
71 queryParams.Add ("openid.mode", "check_authentication");
72
73 byte[] postData = Encoding.ASCII.GetBytes (buildUrlParams (queryParams));
74 HttpWebRequest request = (HttpWebRequest)WebRequest.Create (STEAM_LOGIN);
75 request.Method = "POST";
76 request.ContentType = "application/x-www-form-urlencoded";
77 request.ContentLength = postData.Length;
78 request.Headers.Add (HttpRequestHeader.AcceptLanguage, "en");
79 using (Stream st = request.GetRequestStream ()) {
80 st.Write (postData, 0, postData.Length);
81 }
82
83 HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
84 string responseString = null;
85 using (Stream st = response.GetResponseStream ()) {
86 using (StreamReader str = new StreamReader (st)) {
87 responseString = str.ReadToEnd ();
88 }
89 }
90
91 if (responseString.ToLower ().Contains ("is_valid:true")) {
92 return steamId;
93 } else {
94 Log.Warning ("Steam OpenID login failed: {0}", responseString);
95 return 0;
96 }
97 }
98
99 private static string buildUrlParams (Dictionary<string, string> _queryParams) {
100 string[] paramsArr = new string[_queryParams.Count];
101 int i = 0;
102 foreach (KeyValuePair<string, string> kvp in _queryParams) {
103 paramsArr [i++] = kvp.Key + "=" + Uri.EscapeDataString (kvp.Value);
104 }
105 return string.Join ("&", paramsArr);
106 }
107
108 private static string getValue (HttpListenerRequest _req, string _name) {
109 NameValueCollection nvc = _req.QueryString;
110 if (nvc [_name] == null) {
111 throw new MissingMemberException ("OpenID parameter \"" + _name + "\" missing");
112 }
113 return nvc [_name];
114 }
115
116 }
117}
118
Note: See TracBrowser for help on using the repository browser.