From fabcd08b503c7c5379811886ea4a367fea3b02ea Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 24 Dec 2018 03:07:05 +0000 Subject: [PATCH] refactoring --- Yavsc.Server/Settings/GoogleAuthSettings.cs | 28 ++++++------------- Yavsc.Server/Settings/GoogleServiceAccount.cs | 19 +++++++++++++ Yavsc/Helpers/GoogleHelpers.cs | 4 +-- Yavsc/Services/GoogleApis/CalendarManager.cs | 20 +++++++------ Yavsc/Startup/Startup.OAuth.cs | 7 ++--- Yavsc/Startup/Startup.SanityChecks.cs | 13 --------- Yavsc/Startup/Startup.cs | 11 ++++++++ 7 files changed, 54 insertions(+), 48 deletions(-) create mode 100644 Yavsc.Server/Settings/GoogleServiceAccount.cs diff --git a/Yavsc.Server/Settings/GoogleAuthSettings.cs b/Yavsc.Server/Settings/GoogleAuthSettings.cs index f4630707..cbb5df54 100644 --- a/Yavsc.Server/Settings/GoogleAuthSettings.cs +++ b/Yavsc.Server/Settings/GoogleAuthSettings.cs @@ -2,25 +2,13 @@ namespace Yavsc { - public class GoogleAuthSettings - { - 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 class GoogleAuthSettings + { + public string ApiKey { get; set; } - } - public Account ServiceAccount { get; set; } - } + public string BrowserApiKey { get; set; } + public string GoogleServiceAccountJson { get; set; } + public string GoogleWebClientJson { get; set; } + + } } diff --git a/Yavsc.Server/Settings/GoogleServiceAccount.cs b/Yavsc.Server/Settings/GoogleServiceAccount.cs new file mode 100644 index 00000000..321e7e3d --- /dev/null +++ b/Yavsc.Server/Settings/GoogleServiceAccount.cs @@ -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; } + + } +} + diff --git a/Yavsc/Helpers/GoogleHelpers.cs b/Yavsc/Helpers/GoogleHelpers.cs index c4020dd2..703a06ed 100644 --- a/Yavsc/Helpers/GoogleHelpers.cs +++ b/Yavsc/Helpers/GoogleHelpers.cs @@ -114,8 +114,8 @@ namespace Yavsc.Helpers try { using (var m = new SimpleJsonPostMethod(ep)) { return await m.Invoke( - new { refresh_token= oldResponse.RefreshToken, client_id=settings.ServiceAccount.client_id, - client_secret=settings.ServiceAccount.client_secret, + new { refresh_token= oldResponse.RefreshToken, client_id=Startup.GoogleWebClientConfiguration["web:client_id"], + client_secret=Startup.GoogleWebClientConfiguration["web:client_secret"], grant_type="refresh_token" } ); } diff --git a/Yavsc/Services/GoogleApis/CalendarManager.cs b/Yavsc/Services/GoogleApis/CalendarManager.cs index 4da7c51b..e07df361 100644 --- a/Yavsc/Services/GoogleApis/CalendarManager.cs +++ b/Yavsc/Services/GoogleApis/CalendarManager.cs @@ -59,26 +59,28 @@ namespace Yavsc.Services IDataStore _dataStore; ILogger _logger; + string _client_id; + string _client_secret; - GoogleAuthSettings _googleSettings ; - - public CalendarManager(IOptions settings, + public CalendarManager( ApplicationDbContext dbContext, IDataStore dataStore, ILoggerFactory loggerFactory, - IOptions googleSettingsOptions) + IOptions settings) { + _client_id = Startup.GoogleWebClientConfiguration["web:cient_id"]; + _client_secret = Startup.GoogleWebClientConfiguration["web:cient_secret"]; + _ApiKey = settings.Value.ApiKey; _dbContext = dbContext; _logger = loggerFactory.CreateLogger(); _dataStore = dataStore; - _googleSettings = googleSettingsOptions.Value; _flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { - ClientId = _googleSettings.ServiceAccount.client_id, - ClientSecret = _googleSettings.ServiceAccount.client_secret + ClientId = _client_id, + ClientSecret = _client_secret }, Scopes = scopesCalendar , DataStore = dataStore @@ -280,8 +282,8 @@ namespace Yavsc.Services try { using (var m = new SimpleJsonPostMethod(ep)) { return await m.Invoke( - new { refresh_token= oldResponse.RefreshToken, client_id=_googleSettings.ServiceAccount.client_id, - client_secret=_googleSettings.ServiceAccount.client_secret, + new { refresh_token= oldResponse.RefreshToken, client_id=_client_id, + client_secret=_client_secret, grant_type="refresh_token" } ); } diff --git a/Yavsc/Startup/Startup.OAuth.cs b/Yavsc/Startup/Startup.OAuth.cs index b3e9a333..36b20677 100644 --- a/Yavsc/Startup/Startup.OAuth.cs +++ b/Yavsc/Startup/Startup.OAuth.cs @@ -127,13 +127,12 @@ namespace Yavsc options.AccessDeniedPath = new PathString(Constants.LoginPath.Substring(1)); }); - - var gvents = new OAuthEvents(); + YavscGoogleAppOptions = new YavscGoogleOptions { - ClientId = Configuration["Authentication:Google:ServiceAccount:client_id"], - ClientSecret = Configuration["Authentication:Google:ServiceAccount:client_secret"], + ClientId = GoogleWebClientConfiguration ["web:client_id"], + ClientSecret = GoogleWebClientConfiguration ["web:client_secret"], AccessType = "offline", Scope = { "profile", "https://www.googleapis.com/auth/plus.login", "https://www.googleapis.com/auth/admin.directory.resource.calendar", diff --git a/Yavsc/Startup/Startup.SanityChecks.cs b/Yavsc/Startup/Startup.SanityChecks.cs index ce47ed03..f356f270 100644 --- a/Yavsc/Startup/Startup.SanityChecks.cs +++ b/Yavsc/Startup/Startup.SanityChecks.cs @@ -48,19 +48,6 @@ namespace Yavsc 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}" ); - } } } } diff --git a/Yavsc/Startup/Startup.cs b/Yavsc/Startup/Startup.cs index 0299e9f5..6b4c1b7e 100755 --- a/Yavsc/Startup/Startup.cs +++ b/Yavsc/Startup/Startup.cs @@ -20,6 +20,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Net.Http.Headers; +using Newtonsoft.Json; namespace Yavsc { @@ -43,6 +44,7 @@ namespace Yavsc public static string Authority { get; private set; } public static string Temp { get; set; } public static SiteSettings SiteSetup { get; private set; } + public static GoogleServiceAccount GServiceAccount { get; private set; } public static string HostingFullName { get; set; } @@ -76,11 +78,20 @@ namespace Yavsc var auth = Configuration["Site:Authority"]; var cxstr = Configuration["Data:DefaultConnection:ConnectionString"]; ConnectionString = cxstr; + 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(safile.OpenText().ReadToEnd()); } + public static string ConnectionString { get; set; } public static GoogleAuthSettings GoogleSettings { 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. public void ConfigureServices(IServiceCollection services)