fixes the calendar choice

vnext
Paul Schneider 7 years ago
parent 0952e80d4a
commit 13297a04ed
12 changed files with 225 additions and 234 deletions

@ -10,7 +10,6 @@ using Microsoft.Extensions.OptionsModel;
using Microsoft.Data.Entity;
using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.Extensions.Localization;
using Yavsc.Models.Workflow;
using Yavsc.Models.Identity;
@ -272,20 +271,12 @@ namespace Yavsc.Controllers
}
[HttpGet]
public async Task<IActionResult> SetGoogleCalendar(string returnUrl)
public async Task<IActionResult> SetGoogleCalendar(string returnUrl, string pageToken)
{
try
{
ViewBag.Calendars = await _calendarManager.GetCalendarsAsync(User.GetUserId());
}
catch (WebException ex)
{
// a bug
_logger.LogError("Google Api error");
_logger.LogError("Code: "+ex.HResult+"\n"+ ex.Message);
return RedirectToAction("LinkLogin", new { provider = "Google" });
}
return View(new SetGoogleCalendarViewModel { ReturnUrl = returnUrl });
return View(new SetGoogleCalendarViewModel {
ReturnUrl = returnUrl,
Calendars = await _calendarManager.GetCalendarsAsync(User.GetUserId(), pageToken)
});
}
[HttpPost, ValidateAntiForgeryToken]

@ -33,10 +33,9 @@ namespace Yavsc.Helpers
using Yavsc.Models.Calendar;
using Google.Apis.Auth.OAuth2;
using Microsoft.Data.Entity;
using Google.Apis.Auth.OAuth2.Flows;
using Microsoft.AspNet.Identity.EntityFramework;
using Yavsc.Services;
/// <summary>
/// Google helpers.
/// </summary>
@ -86,25 +85,16 @@ namespace Yavsc.Helpers
}
public static async Task<IdentityUserLogin<string>> GetGoogleUserLoginAsync(
this UserManager<ApplicationUser> userManager,
ApplicationDbContext context,
this ApplicationDbContext context,
string yavscUserId)
{
var user = await userManager.FindByIdAsync(yavscUserId);
var user = context.Users.FirstOrDefaultAsync(u=>u.Id==yavscUserId);
if (user==null) return null;
var googleLogin = await context.UserLogins.FirstOrDefaultAsync(
x => x.UserId == yavscUserId && x.LoginProvider == "Google"
);
return googleLogin;
}
public static UserCredential GetGoogleCredential(string googleUserLoginKey)
{
if (string.IsNullOrEmpty(googleUserLoginKey))
throw new InvalidOperationException("No Google login");
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer());
throw new NotImplementedException();
// TokenResponse resp = flow. ;
// return new UserCredential(flow, googleUserLoginKey, resp);
}
public static async Task<Period[]> GetFreeTime (this ICalendarManager manager, string calId, DateTime startDate, DateTime endDate)
{

@ -125,28 +125,7 @@ namespace Yavsc.Models
public DbSet<Service> Services { get; set; }
public DbSet<Product> Products { get; set; }
public Task ClearTokensAsync()
{
Tokens.RemoveRange(this.Tokens);
SaveChanges(null);
return Task.FromResult(0);
}
public Task DeleteTokensAsync(string email)
{
if (string.IsNullOrEmpty(email))
{
throw new ArgumentException("email MUST have a value");
}
var item = this.Tokens.FirstOrDefault(x => x.UserId == email);
if (item != null)
{
Tokens.Remove(item);
SaveChanges(email);
}
return Task.FromResult(0);
}
public Task<OAuth2Tokens> GetTokensAsync(string googleUserId)
{

@ -1,39 +0,0 @@
using Google.Apis.Requests;
using Google.Apis.Services;
using Yavsc.Models.Google;
namespace Yavsc.Services.GoogleApis
{
public class CalendarClient : ClientServiceRequest<Resource>
{
public CalendarClient(IClientService service) : base (service) {
}
public override string HttpMethod
{
get
{
return "POST";
}
}
public string calendarId
{
get; set;
}
public override string MethodName
{
get
{
return "calendar.events.insert";
}
}
public override string RestPath
{
get
{
return "calendars/{calendarId}/events";
}
}
}
}

@ -28,12 +28,18 @@ using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using System.Collections.Generic;
using System.Linq;
using Google.Apis.Http;
using Google.Apis.Services;
using Microsoft.AspNet.Authentication.OAuth;
using Microsoft.Data.Entity;
namespace Yavsc.Services
{
using System.Collections.Generic;
using System.Linq;
using Google.Apis.Services;
using System.Threading;
using Google.Apis.Auth.OAuth2.Flows;
using Newtonsoft.Json;
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.Models.Calendar;
@ -43,164 +49,204 @@ namespace Yavsc.Services
/// Google Calendar API client.
/// </summary>
public class CalendarManager : ICalendarManager
{
public class ExpiredTokenException : Exception {}
{
public class ExpiredTokenException : Exception { }
protected static string scopeCalendar = "https://www.googleapis.com/auth/calendar";
private string _ApiKey;
private IAuthorizationCodeFlow _flow;
private readonly UserManager<ApplicationUser> _userManager;
private readonly UserManager<ApplicationUser> _userManager;
ApplicationDbContext _dbContext;
ApplicationDbContext _dbContext;
ILogger _logger;
IDataStore _dataStore;
ILogger _logger;
public CalendarManager(IOptions<GoogleAuthSettings> settings,
UserManager<ApplicationUser> userManager,
ApplicationDbContext dbContext,
ILoggerFactory loggerFactory)
{
public CalendarManager(IOptions<GoogleAuthSettings> settings,
UserManager<ApplicationUser> userManager,
ApplicationDbContext dbContext,
IDataStore dataStore,
ILoggerFactory loggerFactory)
{
_ApiKey = settings.Value.ApiKey;
_userManager = userManager;
_dbContext = dbContext;
_logger = loggerFactory.CreateLogger<CalendarManager>();
}
/// <summary>
/// The get cal list URI.
/// </summary>
protected static string getCalListUri = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
/// <summary>
/// The get cal entries URI.
/// </summary>
protected static string getCalEntriesUri = "https://www.googleapis.com/calendar/v3/calendars/{0}/events";
/// <summary>
/// The date format.
/// </summary>
private static string dateFormat = "yyyy-MM-ddTHH:mm:ss";
/// <summary>
/// The time zone. TODO Fixme with machine time zone
/// </summary>
private string timeZone = "+01:00";
private readonly IDataStore dataStore = new FileDataStore(GoogleWebAuthorizationBroker.Folder);
/// <summary>
/// Gets the calendar list.
/// </summary>
/// <returns>The calendars.</returns>
/// <param name="userId">Yavsc user id</param>
public async Task<CalendarList> GetCalendarsAsync (string userId)
{
var service = new CalendarService();
var listRequest = service.CalendarList.List();
_userManager = userManager;
_dbContext = dbContext;
_logger = loggerFactory.CreateLogger<CalendarManager>();
_dataStore = dataStore;
_flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = Startup.GoogleSettings.ClientId,
ClientSecret = Startup.GoogleSettings.ClientSecret
},
Scopes = new[] { scopeCalendar },
DataStore = dataStore
});
_logger.LogWarning($"Using Google data store from "+JsonConvert.SerializeObject(_dataStore));
}
/// <summary>
/// The get cal list URI.
/// </summary>
protected static string getCalListUri = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
/// <summary>
/// The get cal entries URI.
/// </summary>
protected static string getCalEntriesUri = "https://www.googleapis.com/calendar/v3/calendars/{0}/events";
/// <summary>
/// The date format.
/// </summary>
private static string dateFormat = "yyyy-MM-ddTHH:mm:ss";
/// <summary>
/// The time zone. TODO Fixme with machine time zone
/// </summary>
private string timeZone = "+01:00";
/// <summary>
/// Gets the calendar list.
/// </summary>
/// <returns>The calendars.</returns>
/// <param name="userId">Yavsc user id</param>
public async Task<CalendarList> GetCalendarsAsync(string userId, string pageToken)
{
if (string.IsNullOrWhiteSpace(userId))
throw new Exception("the user id is not specified");
var service = await CreateUserCalendarServiceAsync(userId);
CalendarListResource.ListRequest calListReq = service.CalendarList.List ();
calListReq.PageToken = pageToken;
return calListReq.Execute ();
}
private ServiceAccountCredential GetServiceAccountCredential()
{
var creds = GoogleHelpers.GetCredentialForApi(new string[] { scopeCalendar });
if (creds == null)
throw new InvalidOperationException("No credential");
return creds;
}
/// <summary>
/// Gets a calendar event list, between the given dates.
/// </summary>
/// <returns>The calendar.</returns>
/// <param name="calid">Calendar identifier.</param>
/// <param name="mindate">Mindate.</param>
/// <param name="maxdate">Maxdate.</param>
/// <param name="cred">credential string.</param>
public async Task<Events> GetCalendarAsync(string calid, DateTime mindate, DateTime maxdate)
{
var service = await GetServiceAsync();
var listRequest = service.Events.List(calid);
return await listRequest.ExecuteAsync();
}
private ServiceAccountCredential GetServiceAccountCredential()
{
var creds = GoogleHelpers.GetCredentialForApi(new string[]{scopeCalendar});
if (creds==null)
throw new InvalidOperationException("No credential");
}
public async Task<DateTimeChooserViewModel> CreateViewModelAsync(
string inputId,
string calid, DateTime mindate, DateTime maxdate)
{
if (calid == null)
return new DateTimeChooserViewModel
{
InputId = inputId,
MinDate = mindate,
MaxDate = maxdate
};
return creds;
}
/// <summary>
/// Gets a calendar event list, between the given dates.
/// </summary>
/// <returns>The calendar.</returns>
/// <param name="calid">Calendar identifier.</param>
/// <param name="mindate">Mindate.</param>
/// <param name="maxdate">Maxdate.</param>
/// <param name="cred">credential string.</param>
public async Task<Events> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate)
{
var service = new CalendarService();
var eventList = await GetCalendarAsync(calid, mindate, maxdate);
List<Period> free = new List<Period>();
List<Period> busy = new List<Period>();
var listRequest = service.Events.List(calid);
return await listRequest.ExecuteAsync();
}
public async Task<DateTimeChooserViewModel> CreateViewModelAsync(
string inputId,
string calid, DateTime mindate, DateTime maxdate)
{
if (calid ==null) return new DateTimeChooserViewModel {
InputId = inputId,
MinDate = mindate,
MaxDate = maxdate
};
var eventList = await GetCalendarAsync(calid, mindate, maxdate);
List<Period> free = new List<Period> ();
List<Period> busy = new List<Period> ();
foreach (var ev in eventList.Items)
{
DateTime start = ev.Start.DateTime.Value;
DateTime end = ev.End.DateTime.Value;
if (ev.Transparency == "transparent" )
if (ev.Transparency == "transparent")
{
free.Add(new Period { Start = start, End = end });
free.Add(new Period { Start = start, End = end });
}
else busy.Add(new Period { Start = start, End = end });
}
return new DateTimeChooserViewModel {
InputId = inputId,
MinDate = mindate,
MaxDate = maxdate,
Free = free.ToArray(),
Busy = busy.ToArray(),
FreeDates = free.SelectMany( p => new string [] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray(),
BusyDates = busy.SelectMany( p => new string [] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray()
};
}
/// <summary>
/// Creates a event in a calendar
/// <c>calendar.events.insert</c>
/// </summary>
/// <param name="calid"></param>
/// <param name="startDate"></param>
/// <param name="lengthInSeconds"></param>
/// <param name="summary"></param>
/// <param name="description"></param>
/// <param name="location"></param>
/// <param name="available"></param>
/// <returns></returns>
return new DateTimeChooserViewModel
{
InputId = inputId,
MinDate = mindate,
MaxDate = maxdate,
Free = free.ToArray(),
Busy = busy.ToArray(),
FreeDates = free.SelectMany(p => new string[] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray(),
BusyDates = busy.SelectMany(p => new string[] { p.Start.ToString("DD/mm/yyyy"), p.End.ToString("DD/mm/yyyy") }).Distinct().ToArray()
};
}
/// <summary>
/// Creates a event in a calendar
/// <c>calendar.events.insert</c>
/// </summary>
/// <param name="calid"></param>
/// <param name="startDate"></param>
/// <param name="lengthInSeconds"></param>
/// <param name="summary"></param>
/// <param name="description"></param>
/// <param name="location"></param>
/// <param name="available"></param>
/// <returns></returns>
public async Task<Event> CreateEventAsync(string calid, DateTime startDate, int lengthInSeconds, string summary, string description, string location, bool available)
{
if (string.IsNullOrWhiteSpace (calid))
throw new Exception ("the calendar identifier is not specified");
var creds = GetServiceAccountCredential();
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
var computeService = new BaseClientService.Initializer()
{
HttpClientInitializer = credential
};
computeService.ApiKey = Startup.GoogleSettings.ApiKey;
computeService.ApplicationName = "Yavsc";
computeService.Validate();
{
if (string.IsNullOrWhiteSpace(calid))
throw new Exception("the calendar identifier is not specified");
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
var computeService = new BaseClientService.Initializer()
{
HttpClientInitializer = credential
};
computeService.ApiKey = Startup.GoogleSettings.ApiKey;
computeService.ApplicationName = "Yavsc";
computeService.Validate();
var service = new CalendarService();
Event ev = new Event {
Event ev = new Event
{
Start = new EventDateTime { DateTime = startDate },
End = new EventDateTime { DateTime = startDate.AddSeconds(lengthInSeconds) },
Summary = summary,
Description = description
};
var insert = service.Events.Insert(ev,calid);
var insert = service.Events.Insert(ev, calid);
var inserted = await insert.ExecuteAsync();
return inserted;
}
public async Task<CalendarService> GetServiceAsync()
{
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Yavsc"
});
}
/// <summary>
/// Creates Google User Credential
/// </summary>
/// <param name="userId">Yavsc use id</param>
/// <returns></returns>
public async Task<CalendarService> CreateUserCalendarServiceAsync(string userId)
{
var login = await _dbContext.GetGoogleUserLoginAsync(userId);
var token = await _flow.LoadTokenAsync(login.ProviderKey, CancellationToken.None);
UserCredential cred = new UserCredential(_flow,login.ProviderKey,token);
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = cred,
ApplicationName = "Yavsc"
});
}
}
}

@ -31,7 +31,7 @@ namespace Yavsc.Services
/// I calendar manager.
/// </summary>
public interface ICalendarManager {
Task<CalendarList> GetCalendarsAsync (string userId);
Task<CalendarList> GetCalendarsAsync (string userId, string pageToken);
Task<Events> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate);
Task<DateTimeChooserViewModel> CreateViewModelAsync(
string inputId,

@ -15,9 +15,13 @@ using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.WebEncoders;
using OAuth.AspNet.AuthServer;
using OAuth.AspNet.Tokens;
using Google.Apis.Util.Store;
using Yavsc.Auth;
using Yavsc.Extensions;
using Yavsc.Models;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
using Google.Apis.Auth.OAuth2.Responses;
namespace Yavsc
{
@ -87,7 +91,7 @@ namespace Yavsc
//
}
private void ConfigureOAuthApp(IApplicationBuilder app,
SiteSettings settingsOptions)
SiteSettings settingsOptions, ILogger logger)
{
app.UseIdentity();
@ -144,9 +148,18 @@ namespace Yavsc
{
var gcontext = context as GoogleOAuthCreatingTicketContext;
context.Identity.AddClaim(new Claim(YavscClaimTypes.GoogleUserId, gcontext.GoogleUserId));
var service =
serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
await service.StoreTokenAsync(gcontext.GoogleUserId, context.TokenResponse);
var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
await dbContext.StoreTokenAsync(gcontext.GoogleUserId, context.TokenResponse);
var store = serviceScope.ServiceProvider.GetService<IDataStore>();
await store.StoreAsync(gcontext.GoogleUserId, new TokenResponse {
AccessToken = gcontext.TokenResponse.AccessToken,
RefreshToken = gcontext.TokenResponse.RefreshToken,
TokenType = gcontext.TokenResponse.TokenType,
ExpiresInSeconds = int.Parse(gcontext.TokenResponse.ExpiresIn),
IssuedUtc = DateTime.Now
});
}
}
}

@ -27,6 +27,7 @@ namespace Yavsc
{
using System.Net;
using Formatters;
using Google.Apis.Util.Store;
using Microsoft.Extensions.Localization;
using Models;
using PayPal.Manager;
@ -230,6 +231,7 @@ namespace Yavsc
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<IGoogleCloudMessageSender, AuthMessageSender>();
services.AddTransient<IBillingService, BillingService>();
services.AddTransient<IDataStore, FileDataStore>( (sp) => new FileDataStore("googledatastore",false) );
services.AddTransient<ICalendarManager, CalendarManager>();
// TODO for SMS: services.AddTransient<ISmsSender, AuthMessageSender>();
@ -347,7 +349,7 @@ namespace Yavsc
options.AutomaticAuthentication = false;
});
ConfigureOAuthApp(app, SiteSetup);
ConfigureOAuthApp(app, SiteSetup, logger);
ConfigureFileServerApp(app, SiteSetup, env, authorizationService);
ConfigureWebSocketsApp(app, SiteSetup, env);
ConfigureWorkflow(app, SiteSetup, logger);

@ -1,4 +1,6 @@
using Google.Apis.Calendar.v3.Data;
namespace Yavsc.ViewModels.Calendar
{
public class SetGoogleCalendarViewModel
@ -6,6 +8,8 @@ namespace Yavsc.ViewModels.Calendar
public string GoogleCalendarId { get; set; }
public string ReturnUrl { get; set; }
public CalendarList Calendars { get; set; }
}
}

@ -5,16 +5,16 @@
}
<form asp-action="SetGoogleCalendar">
@{ var entryNum=0; }
@foreach (var calendar in ViewBag.Calendars?.items)
@foreach (var calendar in Model.Calendars.Items)
{
entryNum++;
@if (calendar.accessRole=="owner") {
@if (calendar.AccessRole=="owner") {
<label >
<input type="radio" name="GoogleCalendarId" value="@calendar.id" id="cal@entryNum">
<input type="radio" name="GoogleCalendarId" value="@calendar.Id" id="cal@entryNum">
<span style="background-color: @calendar.backgroundColor; foreground-color: @calendar.foregroundColor;">
@calendar.summary </span>
<i>"@calendar.description"</i>
<span style="background-color: @calendar.BackgroundColor; foreground-color: @calendar.ForegroundColor;">
@calendar.Summary </span>
<i>"@calendar.Description"</i>
</label>
}

@ -49,4 +49,3 @@
@inject IOptions<GoogleAuthSettings> GoogleSettings
@inject IOptions<SiteSettings> SiteSettings
@inject IHostingEnvironment HostingEnvironment
@inject ICalendarManager CalendarManager;

@ -1241,7 +1241,6 @@
<Content Include="GoogleApiSupport\Google.Apis\Requests\HttpRequestMessageExtenstions.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Core\Requests\RequestBuilder.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Core\Testing\VisibleForTestOnly.cs" />
<Content Include="Services\GoogleApis\CalendarClient.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Core\Json\NewtonsoftJsonSerializer.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Core\Json\IJsonSerializer.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Core\Discovery\Features.cs" />
@ -1249,6 +1248,13 @@
<Content Include="GoogleApiSupport\Google.Apis.Core\Http\ConfigurableHttpClient.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Core\Http\IHttpClientFactory.cs" />
<Content Include="Services\GoogleApis\Google.Apis.Calendar.v3.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Requests\RefreshTokenRequest.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\AuthorizationCodeInstalledApp.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Responses\TokenResponse.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\UserCredential.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Web\AuthWebUtility.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Web\AuthorizationCodeWebApp.cs" />
<Content Include="GoogleApiSupport\Google.Apis.Auth\OAuth2\Flows\IAuthorizationCodeFlow.cs" />
</ItemGroup>
<Import Project="../Yavsc.Abstract/Yavsc.Abstract.csproj" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

Loading…