From 84a2a26e0c31d8ba390adf913ffbf35977cb563b Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 23 Dec 2018 01:57:58 +0000 Subject: [PATCH] cleanings --- .gitignore | 2 +- Yavsc.Server/Settings/GoogleAuthSettings.cs | 10 +++++----- .../Controllers/Accounting/ManageController.cs | 9 ++------- Yavsc/GoogleCloudMobileDeclaration | 0 Yavsc/Helpers/GoogleHelpers.cs | 6 +++--- Yavsc/Services/GoogleApis/CalendarManager.cs | 17 ++++++++++++----- Yavsc/Startup/Startup.OAuth.cs | 8 ++++---- Yavsc/Startup/Startup.SanityChecks.cs | 8 +------- Yavsc/appsettings.json | 4 ++-- dnx.mk | 2 +- global.json | 1 + omnisharp.json | 2 +- 12 files changed, 33 insertions(+), 36 deletions(-) delete mode 100644 Yavsc/GoogleCloudMobileDeclaration diff --git a/.gitignore b/.gitignore index 3b52d7ed..cd43e262 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.pfx *.userprefs *.Private.cs -*.secret.json +*-secret.json .gitignore .paket/ diff --git a/Yavsc.Server/Settings/GoogleAuthSettings.cs b/Yavsc.Server/Settings/GoogleAuthSettings.cs index 462fa8cb..f4630707 100644 --- a/Yavsc.Server/Settings/GoogleAuthSettings.cs +++ b/Yavsc.Server/Settings/GoogleAuthSettings.cs @@ -5,22 +5,22 @@ namespace Yavsc public class GoogleAuthSettings { public string ApiKey { get; set; } - public string ClientSecret { get; set; } - public string ClientId { get; set; } public string BrowserApiKey { get; set; } - public class ServiceAccount + 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 ServiceAccount Account { get; set; } + public Account ServiceAccount { get; set; } } -} \ No newline at end of file +} diff --git a/Yavsc/Controllers/Accounting/ManageController.cs b/Yavsc/Controllers/Accounting/ManageController.cs index e803ebaa..8cc17e6b 100644 --- a/Yavsc/Controllers/Accounting/ManageController.cs +++ b/Yavsc/Controllers/Accounting/ManageController.cs @@ -302,15 +302,10 @@ namespace Yavsc.Controllers public async Task SetGoogleCalendar(string returnUrl, string pageToken) { -#if Debug - if (_calendarManager==null) throw new Exception("No service!"); -#endif - var calendar = await _calendarManager.GetCalendarsAsync(User.GetUserId(), pageToken); - if (calendar == null) - return new ChallengeResult(); + var calendars = await _calendarManager.GetCalendarsAsync(User.GetUserId(), pageToken); return View(new SetGoogleCalendarViewModel { ReturnUrl = returnUrl, - Calendars = calendar + Calendars = calendars }); } diff --git a/Yavsc/GoogleCloudMobileDeclaration b/Yavsc/GoogleCloudMobileDeclaration deleted file mode 100644 index e69de29b..00000000 diff --git a/Yavsc/Helpers/GoogleHelpers.cs b/Yavsc/Helpers/GoogleHelpers.cs index 56b4dfa5..c4020dd2 100644 --- a/Yavsc/Helpers/GoogleHelpers.cs +++ b/Yavsc/Helpers/GoogleHelpers.cs @@ -114,14 +114,14 @@ namespace Yavsc.Helpers try { using (var m = new SimpleJsonPostMethod(ep)) { return await m.Invoke( - new { refresh_token= oldResponse.RefreshToken, client_id=settings.ClientId, - client_secret=settings.ClientSecret, + new { refresh_token= oldResponse.RefreshToken, client_id=settings.ServiceAccount.client_id, + client_secret=settings.ServiceAccount.client_secret, grant_type="refresh_token" } ); } } catch (Exception ex) { - throw new Exception ("Quelque chose s'est mal passé à l'envoi",ex); + throw new Exception ("No refresh token for Google service account",ex); } } } diff --git a/Yavsc/Services/GoogleApis/CalendarManager.cs b/Yavsc/Services/GoogleApis/CalendarManager.cs index 400f141a..0ce89aa4 100644 --- a/Yavsc/Services/GoogleApis/CalendarManager.cs +++ b/Yavsc/Services/GoogleApis/CalendarManager.cs @@ -74,8 +74,8 @@ namespace Yavsc.Services { ClientSecrets = new ClientSecrets { - ClientId = _googleSettings.ClientId, - ClientSecret = _googleSettings.ClientSecret + ClientId = _googleSettings.ServiceAccount.client_id, + ClientSecret = _googleSettings.ServiceAccount.client_secret }, Scopes = new[] { scopeCalendar }, DataStore = dataStore @@ -259,7 +259,13 @@ namespace Yavsc.Services _logger.LogInformation("Got a Google token"); var c = SystemClock.Default; if (token.IsExpired(c)) { - token = await RefreshToken(token); + if (string.IsNullOrWhiteSpace(token.RefreshToken)) + { + _logger.LogError("no refresh token to exploit and actual one expired : {}"); + } + else { + token = await RefreshToken(token); + } } UserCredential cred = new UserCredential(_flow,login.ProviderKey,token); _logger.LogInformation("Got creadential"); @@ -273,12 +279,13 @@ namespace Yavsc.Services public async Task RefreshToken(TokenResponse oldResponse) { string ep = " https://www.googleapis.com/oauth2/v4/token"; + _logger.LogInformation($"rt:{oldResponse.RefreshToken}"); // refresh_token client_id client_secret grant_type=refresh_token try { using (var m = new SimpleJsonPostMethod(ep)) { return await m.Invoke( - new { refresh_token= oldResponse.RefreshToken, client_id=_googleSettings.ClientId, - client_secret=_googleSettings.ClientSecret, + new { refresh_token= oldResponse.RefreshToken, client_id=_googleSettings.ServiceAccount.client_id, + client_secret=_googleSettings.ServiceAccount.client_secret, grant_type="refresh_token" } ); } diff --git a/Yavsc/Startup/Startup.OAuth.cs b/Yavsc/Startup/Startup.OAuth.cs index afe119a7..ed4a8687 100644 --- a/Yavsc/Startup/Startup.OAuth.cs +++ b/Yavsc/Startup/Startup.OAuth.cs @@ -132,8 +132,8 @@ namespace Yavsc var gvents = new OAuthEvents(); YavscGoogleAppOptions = new YavscGoogleOptions { - ClientId = Configuration["Authentication:Google:ClientId"], - ClientSecret = Configuration["Authentication:Google:ClientSecret"], + ClientId = Configuration["Authentication:Google:ServiceAccount:client_id"], + ClientSecret = Configuration["Authentication:Google:SerivceAccount:client_secret"], AccessType = "offline", Scope = { "profile", "https://www.googleapis.com/auth/plus.login", "https://www.googleapis.com/auth/admin.directory.resource.calendar", @@ -150,7 +150,7 @@ namespace Yavsc var gcontext = context as GoogleOAuthCreatingTicketContext; context.Identity.AddClaim(new Claim(YavscClaimTypes.GoogleUserId, gcontext.GoogleUserId)); var dbContext = serviceScope.ServiceProvider.GetService(); - + /* var store = serviceScope.ServiceProvider.GetService(); await store.StoreAsync(gcontext.GoogleUserId, new TokenResponse { AccessToken = gcontext.TokenResponse.AccessToken, @@ -158,7 +158,7 @@ namespace Yavsc TokenType = gcontext.TokenResponse.TokenType, ExpiresInSeconds = int.Parse(gcontext.TokenResponse.ExpiresIn), IssuedUtc = DateTime.Now - }); + }); */ await dbContext.StoreTokenAsync (gcontext.GoogleUserId, gcontext.TokenResponse.Response, gcontext.TokenResponse.AccessToken, diff --git a/Yavsc/Startup/Startup.SanityChecks.cs b/Yavsc/Startup/Startup.SanityChecks.cs index 52ef7a4f..ce47ed03 100644 --- a/Yavsc/Startup/Startup.SanityChecks.cs +++ b/Yavsc/Startup/Startup.SanityChecks.cs @@ -48,15 +48,9 @@ namespace Yavsc logger.LogWarning("It has been set to : "+Environment.GetEnvironmentVariable("APPDATA")); } - var creds = GoogleSettings?.Account?.private_key; + var creds = GoogleSettings?.ServiceAccount?.private_key; if (creds==null) throw new InvalidOperationException("No Google API credential"); - var initializer = new ServiceAccountCredential.Initializer(Startup.GoogleSettings.Account.client_email); - initializer = initializer.FromPrivateKey(Startup.GoogleSettings.Account.private_key); - if (initializer==null) - - throw new InvalidOperationException("Invalid Google API credential"); - foreach (var feature in app.ServerFeatures) { var val = JsonConvert.SerializeObject(feature.Value); diff --git a/Yavsc/appsettings.json b/Yavsc/appsettings.json index 2059e523..066056b9 100755 --- a/Yavsc/appsettings.json +++ b/Yavsc/appsettings.json @@ -53,7 +53,7 @@ "ApiKey": "[Your ApiKey]", "ClientId": "[Your ClientId]", "ClientSecret": "[Your ClientSecret]", - "Account": { + "ServiceAccount": { "project_id": "[Yours]", "private_key_id": "[Yours]", "private_key": "-----[Yours]---\n", @@ -74,4 +74,4 @@ "ClientSecret": "[Your ClientSecret]" } } -} \ No newline at end of file +} diff --git a/dnx.mk b/dnx.mk index 5707ca5f..a5a5f6b1 100644 --- a/dnx.mk +++ b/dnx.mk @@ -44,7 +44,7 @@ project.lock.json: project.json $(dnu) restore watch: project.lock.json - ASPNET_ENV=$(ASPNET_ENV) ASPNET_LOG_LEVEL=$(ASPNET_LOG_LEVEL) dnx-watch web --configuration=$(CONFIGURATION) + MONO_OPTIONS=--debug ASPNET_ENV=$(ASPNET_ENV) ASPNET_LOG_LEVEL=$(ASPNET_LOG_LEVEL) dnx-watch web --configuration=$(CONFIGURATION) clean: rm -rf bin obj diff --git a/global.json b/global.json index 3efc5304..254ef43a 100644 --- a/global.json +++ b/global.json @@ -4,6 +4,7 @@ "Yavsc.Abstract", "Yavsc.Server", "cli", + "test", "testOauthClient", "OAuth.AspNet.Token", "OAuth.AspNet.Server" diff --git a/omnisharp.json b/omnisharp.json index d0499bf4..d910b660 100644 --- a/omnisharp.json +++ b/omnisharp.json @@ -1,6 +1,6 @@ { "dotnet": { "projects": "*/project.json", - "enablePackageRestore": true + "enablePackageRestore": false } }