Example #1
0
        public void GET_Add_QueryString_Params()
        {
            using( SimulatedServer.Create( AccessConstants.SimulatedServerUrl, QueryString_Echo ) ) {

                // All Params added, GetOrPost & QueryString, should be present in the result set
                SteamClient client = new SteamClient( AccessConstants.SimulatedServerUrl );
                var request = new SteamRequest( "/resource" );

                request.AddParameter( "param1", "1234", ParameterType.GetOrPost );
                request.AddParameter( "param2", "5678", ParameterType.QueryString );

                var response = client.Execute( request );

                Assert.IsNotNull( response.Content );

                string[] temp = response.Content.Split( '|' );

                NameValueCollection coll = new NameValueCollection();
                foreach( string s in temp ) {
                    var split = s.Split( '=' );
                    coll.Add( split[0], split[1] );
                }

                Assert.IsNotNull( coll["param1"] );
                Assert.AreEqual( "1234", coll["param1"] );

                Assert.IsNotNull( coll["param2"] );
                Assert.AreEqual( "5678", coll["param2"] );

            }
        }
Example #2
0
        public void Detect_Malformed_BaseApi()
        {
            var request = new SteamRequest( "resource" );

            var client = new SteamClient(  "Definitely isn't a URI... How sad :(" );

            AssertException.Throws<FormatException>( () => { client.BuildUri( request ); } );
        }
Example #3
0
        public void Add_Two_Of_Same_Parameter()
        {
            var request = new SteamRequest( "/resource" );
            request.AddParameter( "MyFancyParam", 1234, ParameterType.GetOrPost );
            request.AddParameter( "MyFancyParam", 5678, ParameterType.GetOrPost );

            Assert.AreEqual( request.Parameters.Count( p => p.Name == "MyFancyParam" ), 1 );
            Assert.AreEqual( request.Parameters.FirstOrDefault( p => p.Name == "MyFancyParam" ).Value, 5678 );
        }
Example #4
0
        public void GET_GetNewsForApp_NoValues()
        {
            SteamClient client = new SteamClient();
            SteamRequest request = new SteamRequest( "ISteamNews/GetNewsForApp/v0002/" );

            var response = client.Execute( request );

            Assert.AreEqual( HttpStatusCode.BadRequest, response.StatusCode );
        }
Example #5
0
        public void GET_GetGlobalAchievementPercentagesForApp_NoValues()
        {
            SteamClient client = new SteamClient();
            SteamRequest request = new SteamRequest( "ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/" );

            var response = client.Execute( request );

            Assert.AreEqual( HttpStatusCode.BadRequest, response.StatusCode );
        }
Example #6
0
        public void NonExistant_Base_Returns_Error()
        {
            SteamClient client = new SteamClient( "http://hopefullyth-is-domain-nev-rexists.com" );

            SteamRequest request = new SteamRequest( "/resource" );
            var response = client.Execute( request );

            Assert.AreEqual( ResponseStatus.Error, response.ResponseStatus );
        }
Example #7
0
        public void GET_GetPlayerSummaries_BadAuth()
        {
            SteamClient client = new SteamClient();
            client.Authenticator = SteamSharp.Authenticators.APIKeyAuthenticator.ForProtectedResource( "8675309" );

            SteamRequest request = new SteamRequest( "ISteamUser/GetPlayerSummaries/v0002/" );
            request.AddParameter( "steamids", "76561197960435530", ParameterType.QueryString );

            var response = client.Execute( request );

            Assert.AreEqual( HttpStatusCode.Unauthorized, response.StatusCode );
        }
Example #8
0
        public void GET_GetNewsForApp_ByEnums()
        {
            SteamClient client = new SteamClient();
            SteamRequest request = new SteamRequest( SteamAPIInterface.ISteamNews, "GetNewsForApp", SteamMethodVersion.v0002 );

            request.AddParameter( "appid", 440 );
            request.AddParameter( "count", 2 );
            request.AddParameter( "maxlength", 100 );

            var response = client.Execute( request );

            Assert.AreEqual( HttpStatusCode.OK, response.StatusCode );
            Assert.IsNotNull( response.Content );
        }
Example #9
0
        public void GET_GetNewsForApp_ByUri()
        {
            SteamClient client = new SteamClient();
            SteamRequest request = new SteamRequest( "ISteamNews/GetNewsForApp/v0002/" );

            request.AddParameter( "appid", 440 );
            request.AddParameter( "count", 2 );
            request.AddParameter( "maxlength", 100 );

            var response = client.Execute( request );

            Assert.AreEqual( HttpStatusCode.OK, response.StatusCode );
            Assert.IsNotNull( response.Content );
        }
Example #10
0
        public void ClientContext_Request_Correctly_Times_Out()
        {
            using( SimulatedServer.Create( AccessConstants.SimulatedServerUrl, Timeout_Simulator ) ) {

                SteamClient client = new SteamClient( AccessConstants.SimulatedServerUrl );
                client.Timeout = 1000;

                SteamRequest request = new SteamRequest( "/404" );
                var response = client.Execute( request );

                Assert.AreEqual( ResponseStatus.TimedOut, response.ResponseStatus );

            }
        }
Example #11
0
        public void Verify_APIKey_Added()
        {
            // Did you remember to add your API Token?
            Assert.IsNotNull( AccessConstants.APIKey );
            Assert.AreNotEqual( AccessConstants.APIKey, "" );

            using( SimulatedServer.Create( AccessConstants.SimulatedServerUrl, ApiKeyEchoHandler ) ) {

                var client = new SteamClient( AccessConstants.SimulatedServerUrl );

                client.Authenticator = SteamSharp.Authenticators.APIKeyAuthenticator.ForProtectedResource( AccessConstants.APIKey );

                SteamRequest request = new SteamRequest( "/resource" );
                var response = client.Execute( request );

                Assert.AreEqual( "key|" + AccessConstants.APIKey, response.Content );

            }
        }
Example #12
0
        public void POST_Can_Add_Body_NoParams_Raw()
        {
            using( SimulatedServer.Create( AccessConstants.SimulatedServerUrl, Post_Body_Echo ) ) {

                SteamClient client = new SteamClient( AccessConstants.SimulatedServerUrl );
                var request = new SteamRequest( "/resource", HttpMethod.Post );
                request.DataFormat = PostDataFormat.Raw;

                string payload = "myContent 123 abc's fun!";

                request.AddBody( payload );

                var response = client.Execute( request );

                Assert.IsNotNull( response );
                Assert.IsNotNull( response.Content );
                Assert.AreEqual( payload, response.Content );

            }
        }
Example #13
0
        public void POST_Add_QueryString_Params_Json()
        {
            using( SimulatedServer.Create( AccessConstants.SimulatedServerUrl, QueryString_Echo ) ) {

                // Query String params should be in the URI, GetOrPost should not
                SteamClient client = new SteamClient( AccessConstants.SimulatedServerUrl );
                var request = new SteamRequest( "/resource", HttpMethod.Post );
                request.DataFormat = PostDataFormat.Json;

                request.AddParameter( "param1", "1234", ParameterType.GetOrPost );
                request.AddParameter( "param2", "5678", ParameterType.QueryString );

                var response = client.Execute( request );

                Assert.IsNotNull( response.Content );

                string[] temp = response.Content.Split( '|' );

                NameValueCollection coll = new NameValueCollection();
                foreach( string s in temp ) {
                    var split = s.Split( '=' );
                    coll.Add( split[0], split[1] );
                }

                Assert.IsNull( coll["param1"] );

                Assert.IsNotNull( coll["param2"] );
                Assert.AreEqual( "5678", coll["param2"] );

            }
        }
Example #14
0
        public void POST_Can_Add_DataStructure_Json()
        {
            using( SimulatedServer.Create( AccessConstants.SimulatedServerUrl, Post_Body_Echo ) ) {

                SteamClient client = new SteamClient( AccessConstants.SimulatedServerUrl );
                var request = new SteamRequest( "/resource", HttpMethod.Post );
                request.DataFormat = PostDataFormat.Json;

                object payload = new {
                    appids_filter = new int[] { 10, 20, 30, 40, 50 }
                };

                request.AddBody( payload );

                var response = client.Execute( request );

                Assert.IsNotNull( response );
                Assert.IsNotNull( response.Content );

                DataStructureResponseTest obj = JsonConvert.DeserializeObject<DataStructureResponseTest>( response.Content );

                Assert.AreEqual( 5, obj.appids_filter.Length );

            }
        }
Example #15
0
        /// <summary>
        /// Queries Steam API with user credentials and returns a valid access token for use in API calls.
        /// </summary>
        /// <param name="username">Username of the user requesting authentication.</param>
        /// <param name="password">Password for the user requesting authentication.</param>
        /// <param name="steamGuardAnswer"></param>
        /// <param name="captchaAnswer"></param>
        /// <returns>Access token which can then be used with the UserAuthenticator.ForProtectedResource method.</returns>
        public static SteamAccessRequestResult GetAccessTokenForUser( string username, string password, SteamGuardAnswer steamGuardAnswer = null, CaptchaAnswer captchaAnswer = null )
        {
            RSAValues publicKey = GetRSAKeyValues( username );

            // RSA Encryption
            RSAHelper rsa = new RSAHelper();
            rsa.ImportParameters( new RSAParameters {
                E = publicKey.PublicKeyExponent.HexToByteArray(),
                N = publicKey.PublicKeyModulus.HexToByteArray()
            } );

            byte[] cipherPassword = rsa.Encrypt( Encoding.UTF8.GetBytes( password ) );
            string encodedCipherPassword = Convert.ToBase64String( cipherPassword );

            SteamClient client = new SteamClient( "https://steamcommunity.com/" );
            SteamRequest request = new SteamRequest( "mobilelogin/dologin", HttpMethod.Post );

            request.AddParameter( "username", username, ParameterType.QueryString );
            request.AddParameter( "password", encodedCipherPassword, ParameterType.QueryString );
            request.AddParameter( "rsatimestamp", publicKey.Timestamp, ParameterType.QueryString );
            request.AddParameter( "oauth_client_id", "DE45CD61", ParameterType.QueryString );
            request.AddParameter( "oauth_scope", "read_profile write_profile read_client write_client", ParameterType.QueryString );

            if( captchaAnswer != null ) {
                request.AddParameter( "captchagid", captchaAnswer.GID, ParameterType.QueryString );
                request.AddParameter( "captcha_text", captchaAnswer.SolutionText, ParameterType.QueryString );
            }

            if( steamGuardAnswer != null ) {
                request.AddParameter( "emailsteamid", steamGuardAnswer.ID, ParameterType.QueryString );
                request.AddParameter( "emailauth", steamGuardAnswer.SolutionText, ParameterType.QueryString );
            }

            ISteamResponse response = client.Execute( request );
            if( !response.IsSuccessful )
                throw new SteamRequestException( "User authentication failed. Request to procure Steam access token failed (HTTP request not successful).", response ) {
                    IsRequestIssue = true
                };

            SteamTokenResult result;

            try {
                result = JsonConvert.DeserializeObject<SteamTokenResult>( response.Content );
            } catch( Exception e ) {
                throw new SteamRequestException( "Unable to deserialize the token response from Steam.", e ) {
                    IsDeserializationIssue = true
                };
            }

            if( !result.IsSuccessful ){
                return new SteamAccessRequestResult {
                    IsSuccessful = false,
                    SteamResponseMessage = result.Message,
                    IsCaptchaNeeded = result.IsCaptchaNeeded,
                    CaptchaURL = ( String.IsNullOrEmpty( result.CaptchaGID ) ) ? null : "https://steamcommunity.com/public/captcha.php?gid=" + result.CaptchaGID,
                    CaptchaGID = ( String.IsNullOrEmpty( result.CaptchaGID ) ) ? null : result.CaptchaGID,
                    IsSteamGuardNeeded = result.IsEmailAuthNeeded,
                    SteamGuardID = ( String.IsNullOrEmpty( result.EmailSteamID ) ) ? null : result.EmailSteamID,
                    SteamGuardEmailDomain = ( String.IsNullOrEmpty( result.EmailDomain ) ) ? null : result.EmailDomain
                };
            }

            if( result.OAuthParams == null )
                throw new SteamRequestException( "Login was successful did the response did not contain expected OAuth access information.", response );

            OAuthParameters oauthParams = JsonConvert.DeserializeObject<OAuthParameters>( result.OAuthParams );

            SteamUser user = new SteamUser {
                SteamID = new SteamID( oauthParams.SteamID ),
                OAuthAccessToken = oauthParams.OAuthToken
            };

            return new SteamAccessRequestResult {
                IsSuccessful = true,
                IsLoginComplete = result.IsLoginComplete,
                User = user
            };
        }
Example #16
0
        private static RSAValues GetRSAKeyValues( string username )
        {
            SteamClient client = new SteamClient( "https://steamcommunity.com/" );
            SteamRequest request = new SteamRequest( "login/getrsakey" );
            request.AddParameter( "username", username, ParameterType.QueryString );

            ISteamResponse response = client.Execute( request );

            if( !response.IsSuccessful )
                throw new SteamRequestException( "User authentication failed. Request to procure Steam RSA Key failed (HTTP request not successful).", response ) {
                    IsRequestIssue = true
                };

            RSAValues result = JsonConvert.DeserializeObject<RSAValues>( response.Content );

            if( !result.Success || String.IsNullOrEmpty( result.PublicKeyModulus ) || String.IsNullOrEmpty( result.PublicKeyExponent ) )
                throw new SteamAuthenticationException( "Unable to authenticate user. Likely the username supplied is invalid." );

            return result;
        }
Example #17
0
 public void GET_As_Default()
 {
     var request = new SteamRequest( "/resource" );
     Assert.AreEqual( request.Method, HttpMethod.Get );
 }
Example #18
0
        public void GET_GetPlayerSummaries_Unauthenticated()
        {
            SteamClient client = new SteamClient();

            SteamRequest request = new SteamRequest( "ISteamUser/GetPlayerSummaries/v0002/" );
            request.AddParameter( "steamids", "76561197960435530", ParameterType.QueryString );

            var response = client.Execute( request );

            Assert.AreEqual( HttpStatusCode.BadRequest, response.StatusCode );
        }
Example #19
0
        public void GET_GetPlayerSummaries_NoValues()
        {
            SteamClient client = new SteamClient();
            client.Authenticator = SteamSharp.Authenticators.APIKeyAuthenticator.ForProtectedResource( AccessConstants.APIKey );

            SteamRequest request = new SteamRequest( "ISteamUser/GetPlayerSummaries/v0002/" );

            var response = client.Execute( request );

            Assert.AreEqual( HttpStatusCode.BadRequest, response.StatusCode );
        }
Example #20
0
        public void GET_With_NoSlash_Base_And_Resource_Leading_Slash()
        {
            var request = new SteamRequest( "/resource" );
            var client = new SteamClient( "http://iforgottoslash.com" );

            var expected = new Uri( "http://iforgottoslash.com/resource" );
            var output = client.BuildUri( request );

            Assert.AreEqual( expected, output );
        }
Example #21
0
        public void GET_With_Leading_Slash()
        {
            var request = new SteamRequest( "/resource" );
            var client = new SteamClient( "http://steamapiurl.com/" );

            var expected = new Uri( "http://steamapiurl.com/resource" );
            var output = client.BuildUri( request );

            Assert.AreEqual( expected, output );
        }
        public static string GetAccessTokenForUser( SteamUser user )
        {
            SecureRandom sr = new SecureRandom();
            var sessionKey = new byte[32];
            sr.NextBytes( sessionKey );

            RSAHelper rsa = new RSAHelper();
            rsa.ImportParameters( new RSAParameters {
                N = UniversePublicKeys.GetPublicKey( SteamUniverse.Public )
            } );

            byte[] encSessionKey = rsa.Encrypt( sessionKey );

            byte[] loginKey = Encoding.UTF8.GetBytes( user.AuthCookieLoginKey );

            byte[] encLoginKey = AESHelper.Encrypt( loginKey, sessionKey );

            SteamClient client = new SteamClient( "http://api.steampowered.com/" );
            SteamRequest request = new SteamRequest( "ISteamUserAuth/AuthenticateUser/v0001", HttpMethod.Post );

            request.DataFormat = PostDataFormat.FormUrlEncoded;

            request.AddParameter( "steamid", user.SteamID.ToString(), ParameterType.GetOrPost );
            request.AddParameter( "sessionkey", StringFormat.UrlEncode( encSessionKey ), ParameterType.GetOrPost, true );
            request.AddParameter( "encrypted_loginkey", StringFormat.UrlEncode( encLoginKey ), ParameterType.GetOrPost, true );
            request.AddParameter( "format", "json", ParameterType.GetOrPost );

            var response = client.Execute( request );

            return null;
        }
Example #23
0
        public void POST_Can_Add_Body_WithParams_Json()
        {
            using( SimulatedServer.Create( AccessConstants.SimulatedServerUrl, Post_Body_Echo ) ) {

                SteamClient client = new SteamClient( AccessConstants.SimulatedServerUrl );
                var request = new SteamRequest( "/resource", HttpMethod.Post );
                request.DataFormat = PostDataFormat.Json;

                request.AddBody( "myContent 123 abc's fun!" );

                request.AddParameter( "howdy", "doody" );
                request.AddParameter( "foo", "bar" );

                var response = client.Execute( request );

                Assert.IsNotNull( response );
                Assert.IsNotNull( response.Content );

                JObject obj = JObject.Parse( response.Content );

                Assert.AreEqual( "doody", obj["howdy"] );
                Assert.AreEqual( "bar", obj["foo"] );
                Assert.AreEqual( "myContent 123 abc's fun!", obj["application/json"] );

            }
        }
Example #24
0
        public void POST_With_Resource_Containing_Tokens()
        {
            var request = new SteamRequest( "resource/{foo}", HttpMethod.Post );
            request.AddUrlSegment( "foo", "bar" );

            var client = new SteamClient( "http://steamapiurl.com/" );

            var expected = new Uri( "http://steamapiurl.com/resource/bar" );
            var output = client.BuildUri( request );

            Assert.AreEqual( expected, output );
        }