From c7be8bf61ac9cd3c12830f92371b6eea87fd4868 Mon Sep 17 00:00:00 2001 From: MaxchilKH Date: Sun, 13 Sep 2020 00:59:10 +0200 Subject: [PATCH] move client --- .../Controllers/AccessController.cs | 68 ----------------- .../Controllers/GutCasClient.cs | 75 +++++++++++++++++++ 2 files changed, 75 insertions(+), 68 deletions(-) create mode 100644 src/InternshipSystem.Api/Controllers/GutCasClient.cs diff --git a/src/InternshipSystem.Api/Controllers/AccessController.cs b/src/InternshipSystem.Api/Controllers/AccessController.cs index 3aa02bb..69164e3 100644 --- a/src/InternshipSystem.Api/Controllers/AccessController.cs +++ b/src/InternshipSystem.Api/Controllers/AccessController.cs @@ -1,11 +1,7 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net.Http; -using System.Net.Http.Headers; using System.Security.Claims; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; using InternshipSystem.Api.Options; @@ -120,68 +116,4 @@ namespace InternshipSystem.Api.Controllers return Student.CreateStudent(id, firstName, lastName, email, albumNumber); } } - - public class GutCasClient - { - private readonly HttpClient _client; - private readonly SecurityOptions _securityOptions; - - public GutCasClient(HttpClient client, IOptions options) - { - _securityOptions = options.Value; - - client.BaseAddress = _securityOptions.BaseUrl; - _client = client; - } - - public async Task GetCasTokenAsync(string code, CancellationToken cancellationToken) - { - var request = new HttpRequestMessage - { - Method = HttpMethod.Post, - Content = new FormUrlEncodedContent(new Dictionary - { - { "grant_type", "authorization_code" }, - { "client_id", _securityOptions.ClientId }, - { "client_secret", _securityOptions.Secret }, - { "redirect_uri", _securityOptions.RedirectUrl.ToString() }, - { "code", code } - }), - RequestUri = _securityOptions.TokenPath - }; - - var response = await _client.SendAsync(request, cancellationToken); - await using var stream = await response.Content.ReadAsStreamAsync(); - - var value = await JsonSerializer.DeserializeAsync>(stream); - - return value["access_token"].ToString(); - } - - - public async Task GetProfileAsync(string token, CancellationToken cancellationToken) - { - var request = new HttpRequestMessage - { - Method = HttpMethod.Get, - Content = new StringContent(string.Empty), - RequestUri = _securityOptions.ProfilePath - }; - - request.Headers.Authorization = AuthenticationHeaderValue.Parse($"Bearer {token}"); - request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); - - var response = await _client.SendAsync(request, cancellationToken); - await using var stream = await response.Content.ReadAsStreamAsync(); - - var result = await JsonSerializer.DeserializeAsync( - stream, - new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true - }); - - return result.Attributes; - } - } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/GutCasClient.cs b/src/InternshipSystem.Api/Controllers/GutCasClient.cs new file mode 100644 index 0000000..08f0714 --- /dev/null +++ b/src/InternshipSystem.Api/Controllers/GutCasClient.cs @@ -0,0 +1,75 @@ +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using InternshipSystem.Api.Options; +using Microsoft.Extensions.Options; + +namespace InternshipSystem.Api.Controllers +{ + public class GutCasClient + { + private readonly HttpClient _client; + private readonly SecurityOptions _securityOptions; + + public GutCasClient(HttpClient client, IOptions options) + { + _securityOptions = options.Value; + + client.BaseAddress = _securityOptions.BaseUrl; + _client = client; + } + + public async Task GetCasTokenAsync(string code, CancellationToken cancellationToken) + { + var request = new HttpRequestMessage + { + Method = HttpMethod.Post, + Content = new FormUrlEncodedContent(new Dictionary + { + { "grant_type", "authorization_code" }, + { "client_id", _securityOptions.ClientId }, + { "client_secret", _securityOptions.Secret }, + { "redirect_uri", _securityOptions.RedirectUrl.ToString() }, + { "code", code } + }), + RequestUri = _securityOptions.TokenPath + }; + + var response = await _client.SendAsync(request, cancellationToken); + await using var stream = await response.Content.ReadAsStreamAsync(); + + var value = await JsonSerializer.DeserializeAsync>(stream); + + return value["access_token"].ToString(); + } + + + public async Task GetProfileAsync(string token, CancellationToken cancellationToken) + { + var request = new HttpRequestMessage + { + Method = HttpMethod.Get, + Content = new StringContent(string.Empty), + RequestUri = _securityOptions.ProfilePath + }; + + request.Headers.Authorization = AuthenticationHeaderValue.Parse($"Bearer {token}"); + request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); + + var response = await _client.SendAsync(request, cancellationToken); + await using var stream = await response.Content.ReadAsStreamAsync(); + + var result = await JsonSerializer.DeserializeAsync( + stream, + new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + return result.Attributes; + } + } +} \ No newline at end of file