Add google authentication
This commit is contained in:
		
							
								
								
									
										42
									
								
								Core.Thalos.BFF.Api/Services/IGoogleAuthorization.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								Core.Thalos.BFF.Api/Services/IGoogleAuthorization.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| using Google.Apis.Auth.OAuth2; | ||||
| using Google.Apis.Auth.OAuth2.Flows; | ||||
|  | ||||
| namespace Core.Thalos.BFF.Api.Services | ||||
| { | ||||
|     public interface IGoogleAuthorization | ||||
|     { | ||||
|         string GetAuthorizationUrl(); | ||||
|         Task<UserCredential> ExchangeCodeForToken(string code); | ||||
|     } | ||||
|  | ||||
|     public class GoogleAuthorizationService( | ||||
|         IGoogleAuthHelper googleHelper, IConfiguration config) : IGoogleAuthorization | ||||
|     { | ||||
|         private string RedirectUrl = config["Authentication:Google:RedirectUri"]!; | ||||
|  | ||||
|         public async Task<UserCredential> ExchangeCodeForToken(string code) | ||||
|         { | ||||
|             var flow = new GoogleAuthorizationCodeFlow( | ||||
|                 new GoogleAuthorizationCodeFlow.Initializer | ||||
|                 { | ||||
|                     ClientSecrets = googleHelper.GetClientSecrets(), | ||||
|                     Scopes = googleHelper.GetScopes() | ||||
|                 }); | ||||
|  | ||||
|             var token = await flow.ExchangeCodeForTokenAsync( | ||||
|                 "user", code, RedirectUrl, CancellationToken.None); | ||||
|  | ||||
|             return new UserCredential(flow, "user", token); | ||||
|         } | ||||
|  | ||||
|         public string GetAuthorizationUrl() => | ||||
|             new GoogleAuthorizationCodeFlow( | ||||
|                 new GoogleAuthorizationCodeFlow.Initializer | ||||
|                 { | ||||
|  | ||||
|                     ClientSecrets = googleHelper.GetClientSecrets(), | ||||
|                     Scopes = googleHelper.GetScopes(), | ||||
|                     Prompt = "consent" | ||||
|                 }).CreateAuthorizationCodeRequest(RedirectUrl).Build().ToString(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales