|
| 1 | +using Microsoft.AspNetCore.Components; |
| 2 | +using Microsoft.JSInterop; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Net.Http.Headers; |
| 8 | +using System.Text; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Web; |
| 12 | + |
| 13 | +namespace GoogleApis.Blazor.Auth |
| 14 | +{ |
| 15 | + public class AuthService |
| 16 | + { |
| 17 | + [Inject] IJSRuntime JSRuntime { get; set; } |
| 18 | + [Inject] IHttpClientFactory HttpClientFactory { get; set; } |
| 19 | + |
| 20 | + public AuthService(IJSRuntime jsRuntime, IHttpClientFactory httpClientFactory) |
| 21 | + { |
| 22 | + JSRuntime = jsRuntime; |
| 23 | + HttpClientFactory = httpClientFactory; |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// oAuth2 Step 1. Opens "Select Google Account" page in a new tab and return the value into redirectUrl. |
| 28 | + /// </summary> |
| 29 | + /// <param name="clientId"></param> |
| 30 | + /// <param name="scope"></param> |
| 31 | + /// <param name="redirectUrl"></param> |
| 32 | + /// <returns></returns> |
| 33 | + public async Task RequestAuthorizationCode(string clientId, Scope scope, string redirectUrl) |
| 34 | + { |
| 35 | + string encodedRedirectUrl = HttpUtility.UrlEncode(redirectUrl); |
| 36 | + await JSRuntime.InvokeAsync<object>("open", $"https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?response_type=code&client_id={clientId}&scope={scope.ToDescriptionString()}&redirect_uri={encodedRedirectUrl}&flowName=GeneralOAuthFlow&access_type=offline&prompt=consent", "_blank"); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// oAuth2 Step 2. Get credentials with given authorizationCode. Returns string content. |
| 42 | + /// </summary> |
| 43 | + /// <param name="authorizationCode"></param> |
| 44 | + /// <param name="clientId"></param> |
| 45 | + /// <param name="clientSecret"></param> |
| 46 | + /// <param name="redirectUrl"></param> |
| 47 | + /// <returns></returns> |
| 48 | + public string AuthorizeCredential(string authorizationCode, string clientId, string clientSecret, string redirectUrl) |
| 49 | + { |
| 50 | + string encodedAuthorizationCode = HttpUtility.UrlEncode(authorizationCode); |
| 51 | + string encodedRedirectUrl = HttpUtility.UrlEncode(redirectUrl); |
| 52 | + |
| 53 | + var client = HttpClientFactory.CreateClient(); |
| 54 | + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); |
| 55 | + var content = new StringContent($"code={encodedAuthorizationCode}&client_id={clientId}&client_secret={clientSecret}&redirect_uri={encodedRedirectUrl}&scope=&grant_type=authorization_code", Encoding.UTF8, "application/x-www-form-urlencoded"); |
| 56 | + |
| 57 | + var request = client.PostAsync("https://oauth2.googleapis.com/token", content).Result; |
| 58 | + |
| 59 | + return request.Content.ReadAsStringAsync().Result; |
| 60 | + } |
| 61 | + |
| 62 | + public string GetValueFromCredential(string credential, CredentialValueType credentialValueType) |
| 63 | + { |
| 64 | + string accessToken = string.Empty; |
| 65 | + |
| 66 | + var jsonResult = JsonDocument.Parse(credential); |
| 67 | + accessToken = jsonResult.RootElement.GetProperty(credentialValueType.ToDescriptionString()).ToString(); |
| 68 | + |
| 69 | + return accessToken; |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | +} |
0 commit comments