refactoring

This commit is contained in:
2018-12-24 03:07:05 +00:00
parent ac69d524bd
commit fabcd08b50
7 changed files with 54 additions and 48 deletions

View File

@ -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; }
}
}

View 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; }
}
}

View File

@ -114,8 +114,8 @@ namespace Yavsc.Helpers
try {
using (var m = new SimpleJsonPostMethod(ep)) {
return await m.Invoke<TokenResponse>(
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" }
);
}

View File

@ -59,26 +59,28 @@ namespace Yavsc.Services
IDataStore _dataStore;
ILogger _logger;
string _client_id;
string _client_secret;
GoogleAuthSettings _googleSettings ;
public CalendarManager(IOptions<GoogleAuthSettings> settings,
public CalendarManager(
ApplicationDbContext dbContext,
IDataStore dataStore,
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;
_dbContext = dbContext;
_logger = loggerFactory.CreateLogger<CalendarManager>();
_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<TokenResponse>(
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" }
);
}

View File

@ -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",

View File

@ -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}" );
}
}
}
}

View File

@ -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<GoogleServiceAccount>(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)