Exemplo n.º 1
0
        private async Task <IRestResponse> CallSafraEndpoint(string accountId, string url, Method method)
        {
            var token = await userService.GetCurrentTokenByAccount(accountId);

            var headers = new Dictionary <string, string>()
            {
                { "Authorization", $"Bearer {token}" }
            };

            return(await httpService.ExecuteRequest(url, method, headers));
        }
Exemplo n.º 2
0
        public async Task <Token> Authorize(string token)
        {
            const string safraUrl  = "https://idcs-902a944ff6854c5fbe94750e48d66be5.identity.oraclecloud.com/oauth2/v1/token";
            const string safraBody = "grant_type=client_credentials&scope=urn:opc:resource:consumer::all";

            var bytes    = System.Convert.FromBase64String(token);
            var clientId = System.Text.Encoding.UTF8.GetString(bytes).Split(':').FirstOrDefault();

            var headers = new Dictionary <string, string>
            {
                { "Authorization", $"Basic {token}" },
                { "Content-Type", "application/x-www-form-urlencoded" }
            };

            var result = await httpService.ExecuteRequest(safraUrl, RestSharp.Method.POST, headers, safraBody, "application/x-www-form-urlencoded");

            if (result.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var accessToken = httpService.GetObjectFromJson <Token>(result.Content);
                var tokenSaved  = await userRepository.SaveToken(clientId, accessToken.AccessToken);

                if (tokenSaved)
                {
                    return(accessToken);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        public async override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            const string safraHealthUrl = "https://af3tqle6wgdocsdirzlfrq7w5m.apigateway.sa-saopaulo-1.oci.customer-oci.com/fiap-sandbox/health";

            var mandatoryHeaders = new string[] { "Authorization" };
            var headers          = new Dictionary <string, string>();

            foreach (var header in context.HttpContext.Request.Headers)
            {
                foreach (var expectedHeader in mandatoryHeaders)
                {
                    if (expectedHeader == header.Key)
                    {
                        headers.Add(header.Key, header.Value);
                    }
                }
            }

            var response = await httpService.ExecuteRequest(safraHealthUrl, RestSharp.Method.GET, headers);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                var errorResponse = new
                {
                    message = "Unauthorized",
                    code    = 401
                };

                context.Result = new UnauthorizedObjectResult(errorResponse);
            }
            else
            {
                await next();
            }
        }
Exemplo n.º 4
0
        public async Task <ICollection <Employee> > GetEmployees()
        {
            HttpResponseMessage result = await httpService.ExecuteRequest(httpService.CreateHttpRequestMessage("https://masglobaltestapi.azurewebsites.net/api/Employees", HttpMethod.Get, null));

            string data = await result.Content.ReadAsStringAsync();

            return(string.IsNullOrEmpty(data) ? default(List <Employee>) : JsonConvert.DeserializeObject <List <Employee> >(data));
        }