Beispiel #1
0
        /// <inheritdoc/>
        public async Task <RequestTokenResponse> RequestTokenAsync(string clientId, string clientSecret, string requestTokenUrl, string redirectUrl, string code)
        {
            Validation.RejectNullOrEmpty(clientId, "clientId");
            Validation.RejectNullOrEmpty(clientSecret, "clientSecret");
            Validation.RejectNullOrEmpty(requestTokenUrl, "requestTokenUrl");
            Validation.RejectNullOrEmpty(redirectUrl, "redirectUrl");
            Validation.RejectNullOrEmpty(code, "code");

            try
            {
                var encodedAuthentication = BasicAuthentication.Encode(clientId, clientSecret);
                var formData = new List <BasicKeyValuePair>()
                {
                    new BasicKeyValuePair(Parameters.AUTHENTICATION_REDIRECT_URI, redirectUrl),
                    new BasicKeyValuePair(Parameters.CODE, code),
                    new BasicKeyValuePair(Parameters.GRANT_TYPE, DefaultOptions.GRANT_TYPE)
                };

                RestResponse response = await _client.PostAsync(requestTokenUrl, encodedAuthentication, formData, null, null);

                var tokenResponse = new RequestTokenResponse(response);

                return(tokenResponse);
            }
            catch (Exception e) when(e is HttpRequestException || e is System.Net.WebException || e is TaskCanceledException)
            {
                throw new MobileConnectEndpointHttpException(e.Message, e);
            }
        }
Beispiel #2
0
        private async Task <DiscoveryResponse> CallDiscoveryEndpoint(string clientId, string clientSecret, string discoveryUrl, DiscoveryOptions options, IEnumerable <BasicKeyValuePair> currentCookies, bool useCache)
        {
            Validation.RejectNullOrEmpty(clientId, "clientId");
            Validation.RejectNullOrEmpty(clientSecret, "clientSecret");
            Validation.RejectNullOrEmpty(discoveryUrl, "discoveryUrl");
            Validation.RejectNullOrEmpty(options.RedirectUrl, "redirectUrl");

            if (useCache)
            {
                var cachedValue = await GetCachedValueAsync(options);

                if (cachedValue != null)
                {
                    return(cachedValue);
                }
            }

            try
            {
                var cookies = GetCookiesToProxy(currentCookies);
                var encodedAuthentication = BasicAuthentication.Encode(clientId, clientSecret);
                var queryParams           = GetDiscoveryQueryParams(options);

                RestResponse response;
                if (string.IsNullOrEmpty(options.MSISDN))
                {
                    response = await _client.GetAsync(discoveryUrl, encodedAuthentication, options.ClientIP, queryParams, cookies);
                }
                else
                {
                    response = await _client.PostAsync(discoveryUrl, encodedAuthentication, GetDiscoveryQueryParams(options), options.ClientIP, cookies);
                }

                var discoveryResponse = new DiscoveryResponse(response);
                if (useCache)
                {
                    await AddCachedValueAsync(options, discoveryResponse).ConfigureAwait(false);
                }

                return(discoveryResponse);
            }
            catch (Exception e) when(e is HttpRequestException || e is System.Net.WebException || e is TaskCanceledException)
            {
                throw new MobileConnectEndpointHttpException(e.Message, e);
            }
        }