Example #1
0
        public static bool Deauth(RiotAuthToken token, RiotAuthOpenIdConfiguration config, RegionData regionData)
        {
            //Create the Webrequest and make it look like it is coming from the RiotClient
            var client = (HttpWebRequest)WebRequest.Create(config.RevocationEndpoint);

            client.Method = WebRequestMethods.Http.Post;
            if (token.Proxy != null)
            {
                client.Proxy = token.Proxy;
            }
            //Lazy Hack
            client.Host = config.RevocationEndpoint.Replace("//", "/").Split('/')[1];
            client.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            client.UserAgent       = "RiotClient/18.0.0 (rso-auth)";
            client.CachePolicy     = new RequestCachePolicy(RequestCacheLevel.BypassCache);
            client.ProtocolVersion = HttpVersion.Version11;
            client.ContentType     = "application/x-www-form-urlencoded";
            //Use the DSID from RiotAuthToekn
            client.Headers.Set("X-Riot-DSID", token.Dsid);
            client.Accept = "application/json";


            //The information to Post in the webrequest
            var postString = "client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&" +
                             $"client_assertion={regionData.Rso.Token}&" +
                             "grant_type=password&" +
                             $"token={token.AccessTokenJson.RefreshToken}&" +
                             "token_type_hint=refresh_token";

            //Convert to bytes
            var postBytes = Encoding.UTF8.GetBytes(postString);

            client.ContentLength = postBytes.Length;

            //More things to try to make it look like it is coming from the riot client
            client.ServicePoint.Expect100Continue = false;
            client.Headers.Remove(HttpRequestHeader.Pragma);

            //Send the POST request
            var requestStream = client.GetRequestStream();

            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();


            //Retrieve the response
            try
            {
                var response = (HttpWebResponse)client.GetResponse();
                using (var rdr =
                           new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException()))
                {
                    //Success
                    return(true);
                }
            }
            catch
            {
                //Something happened
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Retrieve the User Login Token
        /// </summary>
        /// <param name="username">The username of the player</param>
        /// <param name="password">The password of the player</param>
        /// <param name="regionData">The region the player wants to connect to</param>
        /// <param name="config">The OpenId Config from <see cref="GetOpenIdConfig"/></param>
        /// <returns>The login token or failure information as <see cref="RiotAuthToken"/></returns>
        public static RiotAuthToken GetLoginToken(string username, string password, RegionData regionData, RiotAuthOpenIdConfiguration config)
        {
            //Create the Webrequest and make it look like it is coming from the RiotClient
            var client = (HttpWebRequest)WebRequest.Create(config.TokenEndpoint);

            client.Method = WebRequestMethods.Http.Post;
            if (StaticHelper.SettingsSave.Proxy)
            {
                client.Proxy = ProxyHelper.GetProxy();
            }
            //Lazy Hack
            client.Host = config.TokenEndpoint.Replace("//", "/").Split('/')[1];
            client.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            client.UserAgent       = "RiotClient/18.0.0 (rso-auth)";
            client.CachePolicy     = new RequestCachePolicy(RequestCacheLevel.BypassCache);
            client.ProtocolVersion = HttpVersion.Version11;
            client.ContentType     = "application/x-www-form-urlencoded";
            //No idea what this dsid, but just generate a guid for now because it is the same length and random
            var dsid = Guid.NewGuid().ToString("N");

            client.Headers.Set("X-Riot-DSID", dsid);
            client.Accept = "application/json";

            //The information to Post in the webrequest
            var postString = "client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&" +
                             $"client_assertion={regionData.Rso.Token}&" +
                             "grant_type=password&" +
                             $"username={regionData.PlatformId}|{username}&" +
                             $"password={password}&" +
                             "scope=openid offline_access lol ban profile email phone";

            //Convert to bytes
            var postBytes = Encoding.UTF8.GetBytes(postString);

            client.ContentLength = postBytes.Length;

            //More things to try to make it look like it is coming from the riot client
            client.ServicePoint.Expect100Continue = false;
            client.Headers.Remove(HttpRequestHeader.Pragma);

            try
            {
                //Send the POST request
                var requestStream = client.GetRequestStream();
                requestStream.Write(postBytes, 0, postBytes.Length);
                requestStream.Close();
            }
            catch
            {
                return(StaticHelper.SettingsSave.Proxy
                    ? new RiotAuthToken(RiotAuthResult.BadProxy, null, dsid, regionData)
                    : new RiotAuthToken(RiotAuthResult.ConProblem, null, dsid, regionData));
            }

            //Retrieve the response
            try
            {
                var response = (HttpWebResponse)client.GetResponse();
                using (var rdr =
                           new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException()))
                {
                    //Success, the credentials are correct, allow the login to continue and create a RiotAuthToken
                    var result = rdr.ReadToEnd();
                    return(new RiotAuthToken(RiotAuthResult.Success, result, dsid, regionData));
                }
            }
            catch (WebException e)
            {
                //Credentials are wrong, fail the login request and tell the error to the user
                using (var response = e.Response)
                {
                    //var httpResponse = (HttpWebResponse) response;
                    using (var data = response.GetResponseStream())
                        using (var reader = new StreamReader(data ?? throw new InvalidOperationException()))
                        {
                            var text = reader.ReadToEnd();

                            return(text.Contains("invalid_credentials")
                            ? new RiotAuthToken(RiotAuthResult.InvalidCredentials, null, dsid, regionData)
                            : new RiotAuthToken(RiotAuthResult.UnknownReason, null, dsid, regionData));
                        }
                }
            }
        }