1- using Microsoft . AspNetCore . Components ;
1+ using GoogleApis . Blazor . Extensions ;
2+ using Microsoft . AspNetCore . Components ;
3+ using Microsoft . Extensions . Configuration ;
24using Microsoft . JSInterop ;
35using System ;
46using 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>
0 commit comments