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
*.userprefs
*.Private.cs
*.secret.json
*-secret.json
.gitignore
.paket/

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

@ -302,15 +302,10 @@ namespace Yavsc.Controllers
public async Task<IActionResult> 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
});
}

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

@ -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,8 +259,14 @@ namespace Yavsc.Services
_logger.LogInformation("Got a Google token");
var c = SystemClock.Default;
if (token.IsExpired(c)) {
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");
return new CalendarService(new BaseClientService.Initializer()
@ -273,12 +279,13 @@ namespace Yavsc.Services
public async Task<TokenResponse> 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<TokenResponse>(
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" }
);
}

@ -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<ApplicationDbContext>();
/*
var store = serviceScope.ServiceProvider.GetService<IDataStore>();
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,

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

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

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

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

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

Loading…