cleanings

vnext
Paul Schneider 6 years ago
parent 451632b51c
commit 84a2a26e0c
12 changed files with 33 additions and 36 deletions

2
.gitignore vendored

@ -1,7 +1,7 @@
*.pfx *.pfx
*.userprefs *.userprefs
*.Private.cs *.Private.cs
*.secret.json *-secret.json
.gitignore .gitignore
.paket/ .paket/

@ -5,22 +5,22 @@ namespace Yavsc
public class GoogleAuthSettings public class GoogleAuthSettings
{ {
public string ApiKey { get; set; } public string ApiKey { get; set; }
public string ClientSecret { get; set; }
public string ClientId { get; set; }
public string BrowserApiKey { 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 project_id { get; set; }
public string private_key_id { get; set; } public string private_key_id { get; set; }
public string private_key { get; set; } public string private_key { get; set; }
public string client_email { get; set; } public string client_email { get; set; }
public string client_id { get; set; } public string client_id { get; set; }
public string client_secret { get; set; }
public string auth_uri { get; set; } public string auth_uri { get; set; }
public string token_uri { get; set; } public string token_uri { get; set; }
public string auth_provider_x509_cert_url { get; set; } public string auth_provider_x509_cert_url { get; set; }
public string client_x509_cert_url { get; set; } public string client_x509_cert_url { get; set; }
} }
public ServiceAccount Account { get; set; } public Account ServiceAccount { get; set; }
} }
} }

@ -302,15 +302,10 @@ namespace Yavsc.Controllers
public async Task<IActionResult> SetGoogleCalendar(string returnUrl, string pageToken) public async Task<IActionResult> SetGoogleCalendar(string returnUrl, string pageToken)
{ {
#if Debug var calendars = await _calendarManager.GetCalendarsAsync(User.GetUserId(), pageToken);
if (_calendarManager==null) throw new Exception("No service!");
#endif
var calendar = await _calendarManager.GetCalendarsAsync(User.GetUserId(), pageToken);
if (calendar == null)
return new ChallengeResult();
return View(new SetGoogleCalendarViewModel { return View(new SetGoogleCalendarViewModel {
ReturnUrl = returnUrl, ReturnUrl = returnUrl,
Calendars = calendar Calendars = calendars
}); });
} }

@ -114,14 +114,14 @@ 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.ClientId, new { refresh_token= oldResponse.RefreshToken, client_id=settings.ServiceAccount.client_id,
client_secret=settings.ClientSecret, client_secret=settings.ServiceAccount.client_secret,
grant_type="refresh_token" } grant_type="refresh_token" }
); );
} }
} }
catch (Exception ex) { 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);
} }
} }
} }

@ -74,8 +74,8 @@ namespace Yavsc.Services
{ {
ClientSecrets = new ClientSecrets ClientSecrets = new ClientSecrets
{ {
ClientId = _googleSettings.ClientId, ClientId = _googleSettings.ServiceAccount.client_id,
ClientSecret = _googleSettings.ClientSecret ClientSecret = _googleSettings.ServiceAccount.client_secret
}, },
Scopes = new[] { scopeCalendar }, Scopes = new[] { scopeCalendar },
DataStore = dataStore DataStore = dataStore
@ -259,7 +259,13 @@ namespace Yavsc.Services
_logger.LogInformation("Got a Google token"); _logger.LogInformation("Got a Google token");
var c = SystemClock.Default; var c = SystemClock.Default;
if (token.IsExpired(c)) { 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); UserCredential cred = new UserCredential(_flow,login.ProviderKey,token);
_logger.LogInformation("Got creadential"); _logger.LogInformation("Got creadential");
@ -273,12 +279,13 @@ namespace Yavsc.Services
public async Task<TokenResponse> RefreshToken(TokenResponse oldResponse) public async Task<TokenResponse> RefreshToken(TokenResponse oldResponse)
{ {
string ep = " https://www.googleapis.com/oauth2/v4/token"; string ep = " https://www.googleapis.com/oauth2/v4/token";
_logger.LogInformation($"rt:{oldResponse.RefreshToken}");
// refresh_token client_id client_secret grant_type=refresh_token // refresh_token client_id client_secret grant_type=refresh_token
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.ClientId, new { refresh_token= oldResponse.RefreshToken, client_id=_googleSettings.ServiceAccount.client_id,
client_secret=_googleSettings.ClientSecret, client_secret=_googleSettings.ServiceAccount.client_secret,
grant_type="refresh_token" } grant_type="refresh_token" }
); );
} }

@ -132,8 +132,8 @@ namespace Yavsc
var gvents = new OAuthEvents(); var gvents = new OAuthEvents();
YavscGoogleAppOptions = new YavscGoogleOptions YavscGoogleAppOptions = new YavscGoogleOptions
{ {
ClientId = Configuration["Authentication:Google:ClientId"], ClientId = Configuration["Authentication:Google:ServiceAccount:client_id"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"], ClientSecret = Configuration["Authentication:Google:SerivceAccount: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",
@ -150,7 +150,7 @@ namespace Yavsc
var gcontext = context as GoogleOAuthCreatingTicketContext; var gcontext = context as GoogleOAuthCreatingTicketContext;
context.Identity.AddClaim(new Claim(YavscClaimTypes.GoogleUserId, gcontext.GoogleUserId)); context.Identity.AddClaim(new Claim(YavscClaimTypes.GoogleUserId, gcontext.GoogleUserId));
var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>(); var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
/*
var store = serviceScope.ServiceProvider.GetService<IDataStore>(); var store = serviceScope.ServiceProvider.GetService<IDataStore>();
await store.StoreAsync(gcontext.GoogleUserId, new TokenResponse { await store.StoreAsync(gcontext.GoogleUserId, new TokenResponse {
AccessToken = gcontext.TokenResponse.AccessToken, AccessToken = gcontext.TokenResponse.AccessToken,
@ -158,7 +158,7 @@ namespace Yavsc
TokenType = gcontext.TokenResponse.TokenType, TokenType = gcontext.TokenResponse.TokenType,
ExpiresInSeconds = int.Parse(gcontext.TokenResponse.ExpiresIn), ExpiresInSeconds = int.Parse(gcontext.TokenResponse.ExpiresIn),
IssuedUtc = DateTime.Now IssuedUtc = DateTime.Now
}); }); */
await dbContext.StoreTokenAsync (gcontext.GoogleUserId, await dbContext.StoreTokenAsync (gcontext.GoogleUserId,
gcontext.TokenResponse.Response, gcontext.TokenResponse.Response,
gcontext.TokenResponse.AccessToken, gcontext.TokenResponse.AccessToken,

@ -48,15 +48,9 @@ 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?.Account?.private_key; var creds = GoogleSettings?.ServiceAccount?.private_key;
if (creds==null) if (creds==null)
throw new InvalidOperationException("No Google API credential"); 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) foreach (var feature in app.ServerFeatures)
{ {
var val = JsonConvert.SerializeObject(feature.Value); var val = JsonConvert.SerializeObject(feature.Value);

@ -53,7 +53,7 @@
"ApiKey": "[Your ApiKey]", "ApiKey": "[Your ApiKey]",
"ClientId": "[Your ClientId]", "ClientId": "[Your ClientId]",
"ClientSecret": "[Your ClientSecret]", "ClientSecret": "[Your ClientSecret]",
"Account": { "ServiceAccount": {
"project_id": "[Yours]", "project_id": "[Yours]",
"private_key_id": "[Yours]", "private_key_id": "[Yours]",
"private_key": "-----[Yours]---\n", "private_key": "-----[Yours]---\n",
@ -74,4 +74,4 @@
"ClientSecret": "[Your ClientSecret]" "ClientSecret": "[Your ClientSecret]"
} }
} }
} }

@ -44,7 +44,7 @@ project.lock.json: project.json
$(dnu) restore $(dnu) restore
watch: project.lock.json 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: clean:
rm -rf bin obj rm -rf bin obj

@ -4,6 +4,7 @@
"Yavsc.Abstract", "Yavsc.Abstract",
"Yavsc.Server", "Yavsc.Server",
"cli", "cli",
"test",
"testOauthClient", "testOauthClient",
"OAuth.AspNet.Token", "OAuth.AspNet.Token",
"OAuth.AspNet.Server" "OAuth.AspNet.Server"

@ -1,6 +1,6 @@
{ {
"dotnet": { "dotnet": {
"projects": "*/project.json", "projects": "*/project.json",
"enablePackageRestore": true "enablePackageRestore": false
} }
} }

Loading…