Ejemplo n.º 1
0
        public void OAuthAuthorizationCodeFlowTest()
        {
            // Make an API call with the token
            ApiClient apiClient = new ApiClient(BaseUrl);

            DocuSign.eSign.Client.Configuration.Default.ApiClient = apiClient;

            // Initiate the browser session to the Authentication server
            // so the user can login.
            string accountServerAuthUrl = apiClient.GetAuthorizationUri(client_id, redirect_url, true, stateOptional);

            System.Diagnostics.Process.Start(accountServerAuthUrl);

            WaitForCallbackEvent = new ManualResetEvent(false);

            // Launch a self-hosted web server to accepte the redirect_url call
            // after the user finishes authentication.
            using (WebApp.Start <Startup>("http://localhost:3000"))
            {
                Trace.WriteLine("WebServer Running. Waiting for access_token...");

                // This waits for the redirect_url to be received in the REST controller
                // (see classes below) and then sleeps a short time to allow the response
                // to be returned to the web browser before the server session ends.
                WaitForCallbackEvent.WaitOne(60000, false);
                Thread.Sleep(1000);
            }
            Assert.IsNotNull(AccessCode);

            string accessToken = apiClient.GetOAuthToken(client_id, client_secret, true, AccessCode);

            Assert.IsNotNull(accessToken);
            Trace.WriteLine("Access_token: " + accessToken);

            // we will retrieve this from the login API call
            string accountId = null;

            /////////////////////////////////////////////////////////////////
            // STEP 1: LOGIN API
            /////////////////////////////////////////////////////////////////

            // login call is available in the authentication api
            AuthenticationApi authApi   = new AuthenticationApi();
            LoginInformation  loginInfo = authApi.Login();

            // parse the first account ID that is returned (user might belong to multiple accounts)
            accountId = loginInfo.LoginAccounts[0].AccountId;

            // Update ApiClient with the new base url from login call
            apiClient = new ApiClient(loginInfo.LoginAccounts[0].BaseUrl);

            /////////////////////////////////////////////////////////////////
            // STEP 2: CREATE ACCOUNTS API
            /////////////////////////////////////////////////////////////////
            AccountsApi        accountsApi        = new AccountsApi();
            AccountInformation accountInformation = accountsApi.GetAccountInformation(accountId);

            Trace.WriteLine(accountInformation.ToString());
        }
        /*
         * <Border Background="LightGray" BorderThickness="0" CornerRadius="10" Padding="5" Width="280" Height="Auto" HorizontalAlignment="Right" Margin="0,5,-1,0"><Grid>
         *          <Grid.RowDefinitions>
         *              <RowDefinition Height="Auto"/>
         *              <RowDefinition Height="Auto"/>
         *              <RowDefinition Height="5"/>
         *              <RowDefinition Height="Auto"/>
         *              </Grid.RowDefinitions>
         *              <TextBlock FontWeight="Bold" Text="Влад"/>
         *              <TextBlock Grid.Row="1" Text="wvlejnbeihnbtinbeoibnjidfnjbeineibnibnreijnribnibrneibnreibneribnebienebnibrninrernbienbeinbrinb" TextWrapping="Wrap"/>
         *              <TextBlock Grid.Row="3" FontStyle="Italic" Text="cev" FontFamily="Times New Roman" FontSize="10"/>
         *          </Grid>
         * </Border>
         */
        void DrawingMessage(Message m)
        {
            Grid grid = new Grid();

            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            //grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
            TextBlock textBlock = new TextBlock {
                FontWeight = FontWeights.Bold
            };

            textBlock.Text = m.SenderId == AccountInformation.Id ? AccountInformation.ToString() : (UsersListBox.SelectedItem as Correspondence).Interlocutor.ToString();
            grid.Children.Add(textBlock);
            textBlock = new TextBlock {
                Text = m.TextMessage, TextWrapping = TextWrapping.Wrap
            };
            Grid.SetRow(textBlock, 1);
            grid.Children.Add(textBlock);
            textBlock = new TextBlock
            {
                FontStyle  = FontStyles.Italic,
                FontFamily = new FontFamily("Times New Roman"),
                FontSize   = 10,
                Text       = m.SendingDateTime.ToShortTimeString() + ", " + m.SendingDateTime.ToShortDateString()
            };
            Grid.SetRow(textBlock, 2);
            grid.Children.Add(textBlock);
            Border b = new Border
            {
                CornerRadius = new CornerRadius(10),
                Padding      = new Thickness(5),
                Width        = listMessagesStackPanel.ActualWidth / 2 - 5
            };

            if (m.SenderId == AccountInformation.Id)
            {
                b.Background          = Brushes.DeepSkyBlue;
                b.HorizontalAlignment = HorizontalAlignment.Right;
                b.Margin = new Thickness(0, 5, -1, 0);
            }
            else
            {
                b.Background          = Brushes.LightGray;
                b.HorizontalAlignment = HorizontalAlignment.Left;
                b.Margin = new Thickness(-1, 5, 0, 0);
            }
            b.Child = grid;
            listMessagesStackPanel.Children.Add(b);
        }
Ejemplo n.º 3
0
        public void OAuthAuthorizationCodeFlowTest()
        {
            // Initiate the browser session to the Authentication server
            // so the user can login.
            string accountServerAuthUrl = string.Format("https://{0}/oauth/auth?response_type=code&scope=all&client_id={1}&redirect_uri={2}&state=testState",
                                                        AccountServerHost,
                                                        client_id,
                                                        redirect_url,
                                                        stateOptional);

            System.Diagnostics.Process.Start(accountServerAuthUrl);

            WaitForCallbackEvent = new ManualResetEvent(false);

            // Launch a self-hosted web server to accepte the redirect_url call
            // after the user finishes authencation.
            using (WebApp.Start <Startup>("http://localhost:8090"))
            {
                Trace.WriteLine("WebServer Running- Waiting for access_token");

                // This waits for the redirect_url to be received in the REST controller
                // (see classes below) and then sleeps a short time to allow the response
                // to be returned to the web browser before the server session ends.
                WaitForCallbackEvent.WaitOne(60000, false);
                Thread.Sleep(1000);
            }

            Assert.IsNotNull(AccessCode);

            // The Authentication is completed, so now echange a code returned for
            // the access_token and refresh_token
            var webClient = new WebClient();

            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            // Add the Authorization header with client_id and client_secret as base64
            string codeAuth = client_id + ":" + client_secret;

            byte[] codeAuthBytes  = Encoding.UTF8.GetBytes(codeAuth);
            string codeAuthBase64 = Convert.ToBase64String(codeAuthBytes);

            webClient.Headers.Add("Authorization", "Basic " + codeAuthBase64);

            // Add the code returned from the Authentication site
            string tokenGrantAndCode = string.Format("grant_type=authorization_code&code={0}", AccessCode);

            // Call the token endpoint to exchange the code for an access_token
            string        tokenEndpoint = string.Format("https://{0}/oauth/token", AccountServerHost);
            string        tokenResponse = webClient.UploadString(tokenEndpoint, tokenGrantAndCode);
            TokenResponse tokenObj      = JsonConvert.DeserializeObject <TokenResponse>(tokenResponse);

            Assert.IsNotNull(tokenObj);
            Assert.IsNotNull(tokenObj.access_token);
            Trace.WriteLine("Access_token: " + tokenObj.access_token);

            // Make an API call with the token
            ApiClient apiClient = new ApiClient(BaseUrl);

            DocuSign.eSign.Client.Configuration.Default.ApiClient = apiClient;
            DocuSign.eSign.Client.Configuration.Default.AddDefaultHeader("Authorization", "Bearer " + tokenObj.access_token);

            AccountsApi        accountsApi        = new AccountsApi();
            AccountInformation accountInformation = accountsApi.GetAccountInformation("1");

            Trace.WriteLine(accountInformation.ToString());

            // Generally the refresh token is stored away and used to get a new access_token without authenticating via the browser
            // when the access_token expires (see expires_in). Here we test that the refresh_token can be
            // exchanged for a new access_token

            webClient = new WebClient();
            webClient.Headers.Add("Authorization", "Basic " + codeAuthBase64);
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            // Add the code returned from the Authentication site
            string refreshGrant = string.Format("grant_type=refresh_token&refresh_token={0}", tokenObj.refresh_token);

            tokenResponse = webClient.UploadString(tokenEndpoint, refreshGrant);
            tokenObj      = JsonConvert.DeserializeObject <TokenResponse>(tokenResponse);

            Assert.IsNotNull(tokenObj);
            Assert.IsNotNull(tokenObj.access_token);
            Trace.WriteLine("Access_token (After Refresh): " + tokenObj.access_token);

            // Try another call with new acccess token
            accountInformation = accountsApi.GetAccountInformation("1");
            Trace.WriteLine(accountInformation.ToString());
        }