· 5 years ago · Jan 26, 2020, 04:22 AM
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Net;
6using System.Security.Cryptography;
7using System.Text;
8using System.Threading.Tasks;
9using System.Web;
10
11namespace ESI_Test
12{
13 class Program
14 {
15 class ServerInterfacer : WebClient
16 {
17 protected override WebRequest GetWebRequest(Uri address)
18 {
19 HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
20 request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
21 return request;
22 }
23
24 public ServerInterfacer()
25 {
26 Proxy = null;
27 }
28 }
29 static void Main(string[] args)
30 {
31 Program p = new Program();
32 }
33
34 private static Random random = new Random();
35 public static string RandomString(int length)
36 {
37 const string chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
38 return new string(Enumerable.Repeat(chars, length)
39 .Select(s => s[random.Next(s.Length)]).ToArray());
40 }
41 static public string EncodeTo64(string toEncode)
42 {
43 byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
44 string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
45 return returnValue;
46 }
47 static string sha256(string randomString)
48 {
49 var crypt = new SHA256Managed();
50 string hash = String.Empty;
51 byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(randomString));
52 foreach (byte theByte in crypto)
53 {
54 hash += theByte.ToString("x2");
55 }
56 return hash;
57 }
58
59 public Program()
60 {
61 ServerInterfacer wc = new ServerInterfacer();
62 string clientID = "blahblahblah";
63 string secretKey = "blahblahblah";
64 string callbackURL = "http://localhost/callback/";
65 string corpWalletScope = "esi-wallet.read_corporation_wallets.v1";
66 string baseURL = "https://login.eveonline.com/v2/oauth/authorize/";
67 string responseTypeqp = "?response_type=code";
68 string callbackqp = "&redirect_uri=" + HttpUtility.UrlEncode(callbackURL);
69 string clientIDqp = "&client_id=" + clientID;
70 string scopeqp = "&scope=" + HttpUtility.UrlEncode(corpWalletScope);
71 string r32Bstring = RandomString(32);
72 string firstUrlSafeB64String = EncodeTo64(r32Bstring).Trim().Replace("+", "-").Replace("/", "_").Replace("=", "");
73 string SHA256str = sha256(firstUrlSafeB64String);
74 string secondUrlSafeB64String = EncodeTo64(SHA256str).Trim().Replace("+", "-").Replace("/", "_").Replace("=", "");
75 string codeChallengeqp = "&code_challenge=" + secondUrlSafeB64String;
76 string codeChallengeMethodqp = "&code_challenge_method=S256";
77 string stateqp = "&state=" + RandomString(8);
78 StringBuilder loginURL = new StringBuilder();
79 loginURL
80 .Append(baseURL)
81 .Append(responseTypeqp)
82 .Append(callbackqp)
83 .Append(clientIDqp)
84 .Append(scopeqp)
85 .Append(codeChallengeqp)
86 .Append(codeChallengeMethodqp)
87 .Append(stateqp);
88
89 string loginURLString = loginURL.ToString();
90
91 string authorizationCode = RetrieveAuthCodeFromHD();
92
93 try
94 {
95 // trying to login with assembles URL
96 //string response = wc.DownloadString(loginURL.ToString());
97 }
98 catch (WebException we)
99 {
100
101 }
102 catch (Exception ex)
103 {
104
105 }
106
107
108 // attempting POST request
109 string basePOSTURL = "https://login.eveonline.com/v2/oauth/token";
110 string grantTypeqp = "grant_type=authorization_code";
111 string codeqp = "&code=" + authorizationCode;
112 string clientidqp = "&client_id=" + clientID;
113 string codeVerifierqp = "&code_verifier=" + firstUrlSafeB64String;
114
115 StringBuilder postparams = new StringBuilder();
116 postparams
117 .Append(grantTypeqp)
118 .Append(codeqp)
119 .Append(clientidqp)
120 .Append(codeVerifierqp);
121
122 string postParamsString = postparams.ToString();
123
124 try
125 {
126 wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
127 wc.Headers.Add(HttpRequestHeader.Host, "login.eveonline.com");
128 var response = wc.UploadString(basePOSTURL, postParamsString);
129 }
130 catch (WebException we)
131 {
132
133 }
134 catch (Exception ex)
135 {
136
137 }
138 }
139
140 private string RetrieveAuthCodeFromHD()
141 {
142 string authCode = "";
143
144 using (StreamReader s = new StreamReader("authCode.txt"))
145 {
146 authCode = s.ReadToEnd();
147 s.Close();
148 }
149
150 return authCode;
151 }
152 }
153}