Skip to content

Commit d8ff723

Browse files
committed
More AuthService Features
1 parent c023632 commit d8ff723

File tree

5 files changed

+117
-13
lines changed

5 files changed

+117
-13
lines changed

GoogleApis.Blazor/AllEnums.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22
#pragma warning disable CS1591
33
namespace GoogleApis.Blazor
44
{
5-
public static class EnumExtensions
6-
{
7-
public static string ToDescriptionString(this Enum val)
8-
{
9-
var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
10-
11-
return attributes.Length > 0
12-
? attributes[0].Description
13-
: val.ToString();
14-
}
15-
}
165

176
public enum Scope
187
{

GoogleApis.Blazor/Auth/AuthService.cs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.AspNetCore.Components;
1+
using GoogleApis.Blazor.Extensions;
2+
using Microsoft.AspNetCore.Components;
3+
using Microsoft.Extensions.Configuration;
24
using Microsoft.JSInterop;
35
using System;
46
using System.Collections.Generic;
@@ -19,16 +21,19 @@ public class AuthService
1921
{
2022
[Inject] IHttpClientFactory HttpClientFactory { get; set; }
2123
[Inject] IJSRuntime JSRuntime { get; set; }
24+
[Inject] IConfiguration Configuration { get; set; }
2225

2326
/// <summary>
2427
/// Constructs the class.
2528
/// </summary>
2629
/// <param name="jsRuntime"></param>
2730
/// <param name="httpClientFactory"></param>
28-
public AuthService(IJSRuntime jsRuntime, IHttpClientFactory httpClientFactory)
31+
/// <param name="configuration"></param>
32+
public AuthService(IJSRuntime jsRuntime, IHttpClientFactory httpClientFactory, IConfiguration configuration)
2933
{
3034
JSRuntime = jsRuntime;
3135
HttpClientFactory = httpClientFactory;
36+
Configuration = configuration;
3237
}
3338

3439
/// <summary>
@@ -77,6 +82,81 @@ public string AuthorizeCredential(string authorizationCode, string clientId, str
7782
return request.Content.ReadAsStringAsync().Result;
7883
}
7984

85+
/// <summary>
86+
/// Refresh and create new access token with given resfresh token.
87+
/// </summary>
88+
/// <param name="refreshToken"></param>
89+
/// <param name="clientId"></param>
90+
/// <param name="clientSecret"></param>
91+
/// <param name="redirectUrl"></param>
92+
/// <returns></returns>
93+
public string RefreshAccessToken(string refreshToken, string clientId, string clientSecret, string redirectUrl)
94+
{
95+
string encodedRedirectUrl = HttpUtility.UrlEncode(redirectUrl);
96+
97+
var client = HttpClientFactory.CreateClient();
98+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
99+
var content = new StringContent($"refresh_token={refreshToken}&client_id={clientId}&client_secret={clientSecret}&redirect_uri={encodedRedirectUrl}&scope=&grant_type=refresh_token", Encoding.UTF8, "application/x-www-form-urlencoded");
100+
101+
var request = client.PostAsync("https://oauth2.googleapis.com/token", content).Result;
102+
103+
var result = request.Content.ReadAsStringAsync().Result;
104+
105+
var jsonResult = JsonDocument.Parse(result);
106+
string accessToken = jsonResult.RootElement.GetProperty("access_token").ToString();
107+
108+
return accessToken;
109+
}
110+
111+
/// <summary>
112+
/// Get client id. Only works when client id stored in appsettings.json. You can choose the root value that google credentials stored.
113+
/// </summary>
114+
/// <param name="rootValue"></param>
115+
/// <returns></returns>
116+
public string GetClientId(string rootValue = "GoogleClient")
117+
{
118+
return Configuration.GetSection(rootValue).GetSection("client_id").Value;
119+
}
120+
121+
/// <summary>
122+
/// Get client secret. Only works when client id stored in appsettings.json. You can choose the root value that google credentials stored.
123+
/// </summary>
124+
/// <param name="rootValue"></param>
125+
/// <returns></returns>
126+
public string GetClientSecret(string rootValue = "GoogleClient")
127+
{
128+
return Configuration.GetSection(rootValue).GetSection("client_secret").Value;
129+
}
130+
131+
/// <summary>
132+
/// Get project id. Only works when client id stored in appsettings.json. You can choose the root value that google credentials stored.
133+
/// </summary>
134+
/// <param name="rootValue"></param>
135+
/// <returns></returns>
136+
public string GetProjectId(string rootValue = "GoogleClient")
137+
{
138+
return Configuration.GetSection(rootValue).GetSection("project_id").Value;
139+
}
140+
141+
/// <summary>
142+
/// Gets access token expired or not with given content result. Most of the methods' result can be the contentResult parameter directly.
143+
/// </summary>
144+
/// <param name="contentResult"></param>
145+
/// <returns></returns>
146+
public bool IsAccessTokenExpired(string contentResult)
147+
{
148+
var jsonResult = JsonDocument.Parse(contentResult);
149+
string errorMessage = jsonResult.RootElement.GetPropertyExtension("error")?.GetProperty("errors")[0].GetProperty("message").ToString();
150+
string errorReason = jsonResult.RootElement.GetPropertyExtension("error")?.GetProperty("errors")[0].GetProperty("reason").ToString();
151+
152+
if (errorMessage == "Invalid Credentials" || errorReason == "authError")
153+
{
154+
return true;
155+
}
156+
157+
return false;
158+
}
159+
80160
/// <summary>
81161
/// Get authenticated user's e-mail adress.
82162
/// </summary>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma warning disable CS1591
2+
using System.ComponentModel;
3+
4+
namespace GoogleApis.Blazor.Extensions
5+
{
6+
public static class EnumExtensions
7+
{
8+
public static string ToDescriptionString(this Enum val)
9+
{
10+
var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
11+
12+
return attributes.Length > 0
13+
? attributes[0].Description
14+
: val.ToString();
15+
}
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Text.Json;
2+
#pragma warning disable CS1591
3+
namespace GoogleApis.Blazor.Extensions
4+
{
5+
public static class JsonElementExtenstion
6+
{
7+
public static JsonElement? GetPropertyExtension(this JsonElement jsonElement, string propertyName)
8+
{
9+
if (jsonElement.TryGetProperty(propertyName, out JsonElement returnElement))
10+
{
11+
return returnElement;
12+
}
13+
14+
return null;
15+
}
16+
}
17+
}

GoogleApis.Blazor/GoogleApis.Blazor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
<ItemGroup>
2929
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.4" />
30+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
3031
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
3132
<PackageReference Include="Microsoft.JSInterop" Version="6.0.4" />
3233
</ItemGroup>

0 commit comments

Comments
 (0)