1212
1313namespace GoogleApis . Blazor . Auth
1414{
15+ /// <summary>
16+ /// A class about Google OAuth2 and some user process.
17+ /// </summary>
1518 public class AuthService
1619 {
17- [ Inject ] IJSRuntime JSRuntime { get ; set ; }
1820 [ Inject ] IHttpClientFactory HttpClientFactory { get ; set ; }
19-
21+ [ Inject ] IJSRuntime JSRuntime { get ; set ; }
22+
23+ /// <summary>
24+ /// Constructs the class.
25+ /// </summary>
26+ /// <param name="jsRuntime"></param>
27+ /// <param name="httpClientFactory"></param>
2028 public AuthService ( IJSRuntime jsRuntime , IHttpClientFactory httpClientFactory )
2129 {
2230 JSRuntime = jsRuntime ;
@@ -27,14 +35,24 @@ public AuthService(IJSRuntime jsRuntime, IHttpClientFactory httpClientFactory)
2735 /// oAuth2 Step 1. Opens "Select Google Account" page in a new tab and return the value into redirectUrl.
2836 /// </summary>
2937 /// <param name="clientId"></param>
30- /// <param name="scope "></param>
38+ /// <param name="scopes "></param>
3139 /// <param name="redirectUrl"></param>
3240 /// <returns></returns>
33- public async Task RequestAuthorizationCode ( string clientId , Scope scope , string redirectUrl )
41+ public async Task RequestAuthorizationCode ( string clientId , List < Scope > scopes , string redirectUrl )
3442 {
3543 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" ) ;
44+ if ( scopes == null || scopes . Count == 0 )
45+ {
46+ return ;
47+ }
48+ List < string > scopeStringList = new List < string > ( ) ;
49+ foreach ( var scope in scopes )
50+ {
51+ scopeStringList . Add ( scope . ToDescriptionString ( ) ) ;
52+ }
53+ string scopeString = string . Join ( "+" , scopeStringList ) ;
3754
55+ await JSRuntime . InvokeAsync < object > ( "open" , $ "https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?response_type=code&client_id={ clientId } &scope={ scopeString } &redirect_uri={ encodedRedirectUrl } &flowName=GeneralOAuthFlow&access_type=offline&prompt=consent", "_blank" ) ;
3856 }
3957
4058 /// <summary>
@@ -59,6 +77,35 @@ public string AuthorizeCredential(string authorizationCode, string clientId, str
5977 return request . Content . ReadAsStringAsync ( ) . Result ;
6078 }
6179
80+ /// <summary>
81+ /// Get authenticated user's e-mail adress.
82+ /// </summary>
83+ /// <param name="accessToken"></param>
84+ /// <returns></returns>
85+ public string GetUserMail ( string accessToken )
86+ {
87+ var client = HttpClientFactory . CreateClient ( ) ;
88+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
89+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
90+ var result = client . GetAsync ( "https://www.googleapis.com/userinfo/v2/me" ) . Result ;
91+
92+ if ( ! result . IsSuccessStatusCode )
93+ {
94+ return "error" ;
95+ }
96+
97+ var jsonResult = JsonDocument . Parse ( result . Content . ReadAsStringAsync ( ) . Result ) ;
98+ string email = jsonResult . RootElement . GetProperty ( "email" ) . ToString ( ) ;
99+
100+ return email ;
101+ }
102+
103+ /// <summary>
104+ /// Brings a value into a given credential string. The string can be obtain with result of AuthorizeCredential method.
105+ /// </summary>
106+ /// <param name="credential"></param>
107+ /// <param name="credentialValueType"></param>
108+ /// <returns></returns>
62109 public string GetValueFromCredential ( string credential , CredentialValueType credentialValueType )
63110 {
64111 string accessToken = string . Empty ;
@@ -69,5 +116,23 @@ public string GetValueFromCredential(string credential, CredentialValueType cred
69116 return accessToken ;
70117 }
71118
119+ /// <summary>
120+ /// Get information with given accesstoken like scope, expires in etc. Returns a json format.
121+ /// </summary>
122+ /// <param name="accessToken"></param>
123+ /// <returns></returns>
124+ public string GetAccessTokenDetails ( string accessToken )
125+ {
126+ var client = HttpClientFactory . CreateClient ( ) ;
127+ var result = client . GetAsync ( $ "https://oauth2.googleapis.com/tokeninfo?access_token={ accessToken } ") . Result ;
128+
129+ if ( ! result . IsSuccessStatusCode )
130+ {
131+ return "error" ;
132+ }
133+
134+ return result . Content . ReadAsStringAsync ( ) . Result ;
135+ }
136+
72137 }
73138}
0 commit comments