refactoring
This commit is contained in:
parent
6470cf9273
commit
fa85f1effc
7 changed files with 54 additions and 48 deletions
|
|
@ -2,25 +2,13 @@
|
||||||
|
|
||||||
namespace Yavsc
|
namespace Yavsc
|
||||||
{
|
{
|
||||||
public class GoogleAuthSettings
|
public class GoogleAuthSettings
|
||||||
{
|
{
|
||||||
public string ApiKey { get; set; }
|
public string ApiKey { get; set; }
|
||||||
public string BrowserApiKey { get; set; }
|
|
||||||
public class Account
|
|
||||||
{
|
|
||||||
public string type { get; set; }
|
|
||||||
public string project_id { get; set; }
|
|
||||||
public string private_key_id { get; set; }
|
|
||||||
public string private_key { get; set; }
|
|
||||||
public string client_email { get; set; }
|
|
||||||
public string client_id { get; set; }
|
|
||||||
public string client_secret { get; set; }
|
|
||||||
public string auth_uri { get; set; }
|
|
||||||
public string token_uri { get; set; }
|
|
||||||
public string auth_provider_x509_cert_url { get; set; }
|
|
||||||
public string client_x509_cert_url { get; set; }
|
|
||||||
|
|
||||||
}
|
public string BrowserApiKey { get; set; }
|
||||||
public Account ServiceAccount { get; set; }
|
public string GoogleServiceAccountJson { get; set; }
|
||||||
}
|
public string GoogleWebClientJson { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
Yavsc.Server/Settings/GoogleServiceAccount.cs
Normal file
19
Yavsc.Server/Settings/GoogleServiceAccount.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
namespace Yavsc
|
||||||
|
{
|
||||||
|
public class GoogleServiceAccount
|
||||||
|
{
|
||||||
|
public string type { get; set; }
|
||||||
|
public string project_id { get; set; }
|
||||||
|
public string private_key_id { get; set; }
|
||||||
|
public string private_key { get; set; }
|
||||||
|
public string client_email { get; set; }
|
||||||
|
public string client_id { get; set; }
|
||||||
|
public string auth_uri { get; set; }
|
||||||
|
public string token_uri { get; set; }
|
||||||
|
public string auth_provider_x509_cert_url { get; set; }
|
||||||
|
public string client_x509_cert_url { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -114,8 +114,8 @@ namespace Yavsc.Helpers
|
||||||
try {
|
try {
|
||||||
using (var m = new SimpleJsonPostMethod(ep)) {
|
using (var m = new SimpleJsonPostMethod(ep)) {
|
||||||
return await m.Invoke<TokenResponse>(
|
return await m.Invoke<TokenResponse>(
|
||||||
new { refresh_token= oldResponse.RefreshToken, client_id=settings.ServiceAccount.client_id,
|
new { refresh_token= oldResponse.RefreshToken, client_id=Startup.GoogleWebClientConfiguration["web:client_id"],
|
||||||
client_secret=settings.ServiceAccount.client_secret,
|
client_secret=Startup.GoogleWebClientConfiguration["web:client_secret"],
|
||||||
grant_type="refresh_token" }
|
grant_type="refresh_token" }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,26 +59,28 @@ namespace Yavsc.Services
|
||||||
|
|
||||||
IDataStore _dataStore;
|
IDataStore _dataStore;
|
||||||
ILogger _logger;
|
ILogger _logger;
|
||||||
|
string _client_id;
|
||||||
|
string _client_secret;
|
||||||
|
|
||||||
GoogleAuthSettings _googleSettings ;
|
public CalendarManager(
|
||||||
|
|
||||||
public CalendarManager(IOptions<GoogleAuthSettings> settings,
|
|
||||||
ApplicationDbContext dbContext,
|
ApplicationDbContext dbContext,
|
||||||
IDataStore dataStore,
|
IDataStore dataStore,
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
IOptions<GoogleAuthSettings> googleSettingsOptions)
|
IOptions<GoogleAuthSettings> settings)
|
||||||
{
|
{
|
||||||
|
_client_id = Startup.GoogleWebClientConfiguration["web:cient_id"];
|
||||||
|
_client_secret = Startup.GoogleWebClientConfiguration["web:cient_secret"];
|
||||||
|
|
||||||
_ApiKey = settings.Value.ApiKey;
|
_ApiKey = settings.Value.ApiKey;
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_logger = loggerFactory.CreateLogger<CalendarManager>();
|
_logger = loggerFactory.CreateLogger<CalendarManager>();
|
||||||
_dataStore = dataStore;
|
_dataStore = dataStore;
|
||||||
_googleSettings = googleSettingsOptions.Value;
|
|
||||||
_flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
_flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
||||||
{
|
{
|
||||||
ClientSecrets = new ClientSecrets
|
ClientSecrets = new ClientSecrets
|
||||||
{
|
{
|
||||||
ClientId = _googleSettings.ServiceAccount.client_id,
|
ClientId = _client_id,
|
||||||
ClientSecret = _googleSettings.ServiceAccount.client_secret
|
ClientSecret = _client_secret
|
||||||
},
|
},
|
||||||
Scopes = scopesCalendar ,
|
Scopes = scopesCalendar ,
|
||||||
DataStore = dataStore
|
DataStore = dataStore
|
||||||
|
|
@ -280,8 +282,8 @@ namespace Yavsc.Services
|
||||||
try {
|
try {
|
||||||
using (var m = new SimpleJsonPostMethod(ep)) {
|
using (var m = new SimpleJsonPostMethod(ep)) {
|
||||||
return await m.Invoke<TokenResponse>(
|
return await m.Invoke<TokenResponse>(
|
||||||
new { refresh_token= oldResponse.RefreshToken, client_id=_googleSettings.ServiceAccount.client_id,
|
new { refresh_token= oldResponse.RefreshToken, client_id=_client_id,
|
||||||
client_secret=_googleSettings.ServiceAccount.client_secret,
|
client_secret=_client_secret,
|
||||||
grant_type="refresh_token" }
|
grant_type="refresh_token" }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -129,11 +129,10 @@ namespace Yavsc
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var gvents = new OAuthEvents();
|
|
||||||
YavscGoogleAppOptions = new YavscGoogleOptions
|
YavscGoogleAppOptions = new YavscGoogleOptions
|
||||||
{
|
{
|
||||||
ClientId = Configuration["Authentication:Google:ServiceAccount:client_id"],
|
ClientId = GoogleWebClientConfiguration ["web:client_id"],
|
||||||
ClientSecret = Configuration["Authentication:Google:ServiceAccount:client_secret"],
|
ClientSecret = GoogleWebClientConfiguration ["web:client_secret"],
|
||||||
AccessType = "offline",
|
AccessType = "offline",
|
||||||
Scope = { "profile", "https://www.googleapis.com/auth/plus.login",
|
Scope = { "profile", "https://www.googleapis.com/auth/plus.login",
|
||||||
"https://www.googleapis.com/auth/admin.directory.resource.calendar",
|
"https://www.googleapis.com/auth/admin.directory.resource.calendar",
|
||||||
|
|
|
||||||
|
|
@ -48,19 +48,6 @@ namespace Yavsc
|
||||||
logger.LogWarning("It has been set to : "+Environment.GetEnvironmentVariable("APPDATA"));
|
logger.LogWarning("It has been set to : "+Environment.GetEnvironmentVariable("APPDATA"));
|
||||||
}
|
}
|
||||||
|
|
||||||
var creds = GoogleSettings?.ServiceAccount?.private_key;
|
|
||||||
if (creds==null)
|
|
||||||
throw new InvalidOperationException("No Google API credential");
|
|
||||||
foreach (var feature in app.ServerFeatures)
|
|
||||||
{
|
|
||||||
var val = JsonConvert.SerializeObject(feature.Value);
|
|
||||||
logger.LogInformation( $"#Feature {feature.Key}: {val}" );
|
|
||||||
}
|
|
||||||
foreach (var prop in app.Properties)
|
|
||||||
{
|
|
||||||
var val = JsonConvert.SerializeObject(prop.Value);
|
|
||||||
logger.LogInformation( $"#Property {prop.Key}: {val}" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.OptionsModel;
|
using Microsoft.Extensions.OptionsModel;
|
||||||
using Microsoft.Extensions.PlatformAbstractions;
|
using Microsoft.Extensions.PlatformAbstractions;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Yavsc
|
namespace Yavsc
|
||||||
{
|
{
|
||||||
|
|
@ -43,6 +44,7 @@ namespace Yavsc
|
||||||
public static string Authority { get; private set; }
|
public static string Authority { get; private set; }
|
||||||
public static string Temp { get; set; }
|
public static string Temp { get; set; }
|
||||||
public static SiteSettings SiteSetup { get; private set; }
|
public static SiteSettings SiteSetup { get; private set; }
|
||||||
|
public static GoogleServiceAccount GServiceAccount { get; private set; }
|
||||||
|
|
||||||
public static string HostingFullName { get; set; }
|
public static string HostingFullName { get; set; }
|
||||||
|
|
||||||
|
|
@ -76,11 +78,20 @@ namespace Yavsc
|
||||||
var auth = Configuration["Site:Authority"];
|
var auth = Configuration["Site:Authority"];
|
||||||
var cxstr = Configuration["Data:DefaultConnection:ConnectionString"];
|
var cxstr = Configuration["Data:DefaultConnection:ConnectionString"];
|
||||||
ConnectionString = cxstr;
|
ConnectionString = cxstr;
|
||||||
|
|
||||||
AppDomain.CurrentDomain.SetData(Constants.YavscConnectionStringEnvName, ConnectionString);
|
AppDomain.CurrentDomain.SetData(Constants.YavscConnectionStringEnvName, ConnectionString);
|
||||||
|
|
||||||
|
var googleClientFile = Configuration["Authentication:Google:GoogleWebClientJson"];
|
||||||
|
var googleServiceAccountJsonFile = Configuration["Authentication:Google:GoogleServiceAccountJson"];
|
||||||
|
GoogleWebClientConfiguration = new ConfigurationBuilder().AddJsonFile(googleClientFile).Build();
|
||||||
|
var safile = new FileInfo(googleServiceAccountJsonFile);
|
||||||
|
GServiceAccount = JsonConvert.DeserializeObject<GoogleServiceAccount>(safile.OpenText().ReadToEnd());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ConnectionString { get; set; }
|
public static string ConnectionString { get; set; }
|
||||||
public static GoogleAuthSettings GoogleSettings { get; set; }
|
public static GoogleAuthSettings GoogleSettings { get; set; }
|
||||||
public IConfigurationRoot Configuration { get; set; }
|
public IConfigurationRoot Configuration { get; set; }
|
||||||
|
public static IConfigurationRoot GoogleWebClientConfiguration { get; set; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue