public void TestCallWithBadAuthToken()
 {
     RestClient unauthenticatedRestClient = new RestClient(TestCredentials.INSTANCE_SERVER, BAD_TOKEN, null);
     RestResponse response = unauthenticatedRestClient.SendSync(RestRequest.GetRequestForResources(TestCredentials.API_VERSION));
     Assert.IsFalse(response.Success, "Success not expected");
     Assert.IsNotNull(response.Error, "Expected error");
     Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "Expected 401");
 }
 public void SetUp()
 {
     var loginOptions = new LoginOptions(TestCredentials.LOGIN_URL, TestCredentials.CLIENT_ID, null, null);
     _accessToken =
         OAuth2.RefreshAuthTokenRequest(loginOptions, TestCredentials.REFRESH_TOKEN).Result.AccessToken;
     _restClient = new RestClient(TestCredentials.INSTANCE_SERVER, _accessToken, null);
     _unauthenticatedRestClient = new RestClient(TestCredentials.INSTANCE_SERVER);
 }
 public void TestCallWithBadAuthTokenAndTokenProvider()
 {
     RestClient unauthenticatedRestClient = new RestClient(TestCredentials.INSTANCE_SERVER, BAD_TOKEN, () => Task.Factory.StartNew(() => _accessToken));
     Assert.AreEqual(BAD_TOKEN, unauthenticatedRestClient.AccessToken, "RestClient should be using the bad token initially");
     RestResponse response = unauthenticatedRestClient.SendSync(RestRequest.GetRequestForResources(TestCredentials.API_VERSION));
     Assert.IsTrue(response.Success, "Success expected");
     Assert.IsNull(response.Error, "Expected error");
     Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Expected 200");
     Assert.AreEqual(_accessToken, unauthenticatedRestClient.AccessToken, "RestClient should now using the good token");
 }
 public void TestGetVersions()
 {
     // We don't need to be authenticated
     RestClient unauthenticatedRestClient = new RestClient(TestCredentials.INSTANCE_SERVER, BAD_TOKEN, null);
     RestResponse response = unauthenticatedRestClient.SendSync(RestRequest.GetRequestForVersions());
     CheckResponse(response, HttpStatusCode.OK, true);
 }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _client = SDKManager.GlobalClientManager.PeekRestClient();

            // Not logged in
            if (_client == null)
            {
                OnResumeNotLoggedIn();
            }
                // Logged in
            else
            {
                // Web app never loaded
                if (!_webAppLoaded)
                {
                    OnResumeLoggedInNotLoaded();
                }
                // Web app already loaded
            }
        }
 public JSONCredentials(Account account, RestClient client)
 {
     AccessToken = client.AccessToken;
     LoginUrl = account.LoginUrl;
     InstanceUrl = account.InstanceUrl;
     ClientId = account.ClientId;
     RefreshToken = account.RefreshToken;
     UserAgent = "SalesforceMobileSDK/2.0 windows phone"; // FIXME
     // TODO wire through the other fields
 }
 /// <summary>
 ///     Launch login flow if not authenticated or refresh auth token if already authenticated
 /// </summary>
 /// <param name="plugin"></param>
 public void Authenticate(SalesforceOAuthPlugin plugin)
 {
     _client = SDKManager.GlobalClientManager.GetRestClient();
     if (_client == null)
     {
         // login flow will get started, when page is eventually reloaded, we will be called again and _client will not be null
     }
     else
     {
         RefreshSession(plugin);
     }
 }
        /// <summary>
        ///     Called when bringing up page and user is not authenticated
        /// </summary>
        protected void OnResumeNotLoggedIn()
        {
            PlatformAdapter.SendToCustomLogger("HybridMainPage.OnResumeNotLoggedIn called", LoggingLevel.Verbose);

            // Need to be authenticated
            if (_bootConfig.ShouldAuthenticate)
            {
                // Online
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    Log("Starting authentication flow");
                    _client = SDKManager.GlobalClientManager.GetRestClient();
                    // After login, we will end up in OnResumeLoggedInNotLoaded
                }
                    // Offline
                else
                {
                    Log("Error:Can't start application that requires authentication while offline");
                    LoadErrorPage();
                }
            }

                // Does not need to be authenticated
            else
            {
                // Local
                if (_bootConfig.IsLocal)
                {
                    Log("Success:Loading local application - no authentication required");
                    LoadLocalStartPage();
                }
                    // Remote
                else
                {
                    Log("Success:Loading remote application - no authentication required");
                    LoadRemoteStartPage();
                }
            }
        }
 public async Task TestCallWithBadAuthTokenAndTokenProvider()
 {
     var unauthenticatedRestClient = new RestClient(TestCredentials.InstanceServer, BadToken,
         () => Task.Factory.StartNew(() => _accessToken));
     Assert.AreEqual(BadToken, unauthenticatedRestClient.AccessToken,
         "RestClient should be using the bad token initially");
     var response =
         await
             unauthenticatedRestClient.SendAsync(RestRequest.GetRequestForResources(new TestCredentials().ApiVersion));
     Assert.IsTrue(response.Success, "Success expected");
     Assert.IsNull(response.Error, "Expected error");
     Assert.AreEqual(HttpStatusCode.OK.ToString().ToLower(), response.StatusCode.ToString().ToLower(), "Expected 200");
     Assert.AreEqual(_accessToken, unauthenticatedRestClient.AccessToken,
         "RestClient should now using the good token");
 }
 public async Task TestCallWithBadAuthToken()
 {
     var unauthenticatedRestClient = new RestClient(TestCredentials.InstanceServer, BadToken, null);
     var response =
         await
             unauthenticatedRestClient.SendAsync(RestRequest.GetRequestForResources(new TestCredentials().ApiVersion));
     Assert.IsFalse(response.Success, "Success not expected");
     Assert.IsNotNull(response.Error, "Expected error");
     Assert.AreEqual(HttpStatusCode.Unauthorized.ToString().ToLower(), response.StatusCode.ToString().ToLower(), "Expected 401");
 }
 public static async Task TestSetup(TestContext context)
 {
     var settings = new EncryptionSettings(new HmacSHA256KeyGenerator());
     Encryptor.init(settings);
     var options = new LoginOptions(TestCredentials.LoginUrl, TestCredentials.ClientId, TestCallbackUrl, "mobile",
         TestScopes);
     var response = new AuthResponse
     {
         RefreshToken = TestCredentials.RefreshToken,
         AccessToken = TestAuthToken,
         InstanceUrl = TestCredentials.InstanceUrl,
         IdentityUrl = TestCredentials.IdentityUrl,
         Scopes = TestScopes,
     };
     Account account = await AccountManager.CreateNewAccount(options, response);
     account.UserId = TestCredentials.UserId;
     account.UserName = TestCredentials.Username;
     await OAuth2.RefreshAuthToken(account);
     _smartStore = SmartStore.GetGlobalSmartStore();
     _smartStore.ResetDatabase();
     _syncManager = SyncManager.GetInstance();
     _restClient = new RestClient(account.InstanceUrl, account.AccessToken,
         async () =>
         {
             account = AccountManager.GetAccount();
             AuthResponse authResponse =
                 await OAuth2.RefreshAuthTokenRequest(account.GetLoginOptions(), account.RefreshToken);
             account.AccessToken = authResponse.AccessToken;
             return account.AccessToken;
         }
         );
     CreateAccountsSoup();
     _idToNames = await CreateTestAccountsOnServer(CountTestAccounts);
 }
 public async Task TestCallWithBadAuthToken()
 {
     var unauthenticatedRestClient = new RestClient(TestCredentials.INSTANCE_SERVER, BAD_TOKEN, null);
     RestResponse response =
         await
             unauthenticatedRestClient.SendAsync(RestRequest.GetRequestForResources(TestCredentials.API_VERSION));
     Assert.IsFalse(response.Success, "Success not expected");
     Assert.IsNotNull(response.Error, "Expected error");
     Assert.AreEqual(HttpStatusCode.Unauthorized.ToString().ToLower(), response.StatusCode.ToString().ToLower(), "Expected 401");
 }
 public static async Task<IdentityResponse> CallIdentityService(string idUrl, RestClient client)
 {
     var request = new RestRequest(HttpMethod.Get, new Uri(idUrl).AbsolutePath);
     RestResponse response = await client.SendAsync(request);
     if (response.Success)
     {
         PlatformAdapter.SendToCustomLogger("OAuth2.CallIdentityService - success", LoggingLevel.Verbose);
         return JsonConvert.DeserializeObject<IdentityResponse>(response.AsString);
     }
     else
     {
         PlatformAdapter.SendToCustomLogger("OAuth2.CallIdentityService - Error occured:", LoggingLevel.Critical);
         PlatformAdapter.SendToCustomLogger(response.Error, LoggingLevel.Critical);
     }
     throw response.Error;
 }