Ejemplo n.º 1
0
        /// <summary>
        ///     This method is responsible for retrieving Oauth tokens from the Github API.
        /// </summary>
        /// <param name="code">The code which is used to retrieve the Oauth tokens.</param>
        /// <returns>This method returns the Oauth tokens.</returns>
        /// <exception cref="ExternalException">This method throws the External Exception whenever the response is not successful.</exception>
        public async Task <OauthTokens> FetchOauthTokens(string code)
        {
            Uri          baseUriGitlab = new Uri("https://gitlab.com/");
            IRestClient  client        = restClientFactory.Create(baseUriGitlab);
            IRestRequest request       = new RestRequest("oauth/token", Method.POST);

            client.Authenticator = new HttpBasicAuthenticator(clientId, clientSecret);
            request.AddParameter("code", code);
            request.AddParameter("grant_type", "authorization_code");
            request.AddParameter("redirect_uri", RedirectUri);
            IRestResponse response = await client.ExecuteAsync(request);

            if (!response.IsSuccessful)
            {
                throw new ExternalException(response.ErrorMessage);
            }
            if (string.IsNullOrEmpty(response.Content))
            {
                return(null);
            }

            OauthTokens tokens = JsonConvert.DeserializeObject <OauthTokens>(response.Content);

            return(tokens);
        }
Ejemplo n.º 2
0
        public async Task FetchOauthTokens_GoodFlow()
        {
            OauthTokens oauthTokens = new OauthTokens {
                AccessToken = "token"
            };

            MockRestClient(oauthTokens, HttpStatusCode.OK);
            DataSourceAdaptee = new GitlabDataSourceAdaptee(ConfigurationMock, ClientFactoryMock.Object, Mapper);

            // Act
            Action      act = () => DataSourceAdaptee.FetchOauthTokens(It.IsAny <string>());
            OauthTokens retrievedOauthTokens = await DataSourceAdaptee.FetchOauthTokens(It.IsAny <string>());

            // Assert
            act.Should().NotThrow();
            retrievedOauthTokens.Should().BeEquivalentTo(oauthTokens);
            retrievedOauthTokens.Should().NotBeNull();
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> DataProviderCallback(string provider, string code)
        {
            IDataSourceAdaptee dataSourceAdaptee;

            if (Guid.TryParse(provider, out Guid _))
            {
                dataSourceAdaptee = await dataProviderService.RetrieveDataSourceByGuid(provider);
            }
            else
            {
                dataSourceAdaptee = await dataProviderService.RetrieveDataSourceByName(provider);
            }

            if (dataSourceAdaptee == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Data source not found.",
                    Detail   = "Data source could not be found with specified data source guid.",
                    Instance = "5B4E11A6-8209-4F49-B76A-1EF4297D990F"
                };
                return(NotFound(problem));
            }

            IAuthorizedDataSourceAdaptee authorizedDataSourceAdaptee =
                dataSourceAdaptee as IAuthorizedDataSourceAdaptee;

            if (authorizedDataSourceAdaptee == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "The specified provider does not allowed authorization.",
                    Detail   = "The specified provider is not able to verify the code.",
                    Instance = "EB1F47B2-5526-41F3-8C69-8068F12A92D1"
                };
                return(BadRequest(problem));
            }

            try
            {
                OauthTokens tokens = await authorizedDataSourceAdaptee.GetTokens(code);

                OauthTokensResourceResult resourceResult = mapper.Map <OauthTokens, OauthTokensResourceResult>(tokens);
                return(Ok(resourceResult));
            } catch (NotSupportedByExternalApiException e)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "External API does not support the functionality from the method.",
                    Detail   = e.Message,
                    Instance = "CDFFA448-38B1-450F-8D14-FB89FB7B5462"
                };
                return(BadRequest(problem));
            } catch (ExternalException e)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "An problem encountered while using the external API.",
                    Detail   = e.Message,
                    Instance = "7D445CB5-7C19-449C-B9FF-4214E4BE4CF0"
                };
                return(BadRequest(problem));
            } catch (NotSupportedException e)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "The specified data source is not supported.",
                    Detail   = e.Message,
                    Instance = "7F2C173E-F001-49CA-8DF8-C18A0837B4AF"
                };
                return(BadRequest(problem));
            }
        }