using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2.Flows; namespace Core.Thalos.BFF.Api.Services { public interface IGoogleAuthorization { string GetAuthorizationUrl(); Task ExchangeCodeForToken(string code); } public class GoogleAuthorizationService( IGoogleAuthHelper googleHelper, IConfiguration config) : IGoogleAuthorization { private string RedirectUrl = config["Authentication:Google:RedirectUri"]!; public async Task 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(); } }