43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
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();
|
|
}
|
|
}
|