This repository has been archived by the owner on Jul 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarController.cs
111 lines (101 loc) · 4.02 KB
/
CalendarController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
using EventsCalendar.Repository;
using EventsCalendar.Web.Areas.Admin.Models.Calendar;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Util;
namespace EventsCalendar.Web.Areas.Admin.Controllers
{
public partial class CalendarController : Controller
{
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
if (_client == null)
_client = new WebServerClient(GoogleAuthenticationServer.Description)
{
ClientIdentifier = SettingsRepository.GoogleAPI.ClientID,
ClientSecret = SettingsRepository.GoogleAPI.ClientSecret,
};
if (_authenticator == null)
_authenticator = new OAuth2Authenticator<WebServerClient>(_client, GetAuthorization);
if (_service == null)
_service = new CalendarService(_authenticator);
}
private static CalendarService _service;
private static WebServerClient _client;
private static OAuth2Authenticator<WebServerClient> _authenticator;
private IAuthorizationState _state;
private UserRepository _userRepository = new UserRepository();
private IAuthorizationState GetAuthorization(WebServerClient client)
{
// If this user is already authenticated, then just return the auth state.
IAuthorizationState state = _state ?? _userRepository.GetCalendarAuthorization(SettingsRepository.Reddit.BotLogin);
if (state != null)
return state;
// Check if an authorization request already is in progress.
System.Threading.Thread.Sleep(2000);
state = client.ProcessUserAuthorization();
if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
{
// Store and return the credentials.
_userRepository.UpdateCalendarAuthorization(SettingsRepository.Reddit.BotLogin, _state = state);
return state;
}
// Otherwise do a new authorization request.
return null;
}
//
// GET: /Admin/Calendar
public virtual ActionResult Index()
{
var model = new IndexModel
{
TokenPresent = _userRepository.GetCalendarAuthorization(SettingsRepository.Reddit.BotLogin) != null,
};
if (model.TokenPresent)
{
try
{
model.Calendars = _service.CalendarList.List().Fetch();
}
catch (Google.GoogleApiRequestException)
{
model.TokenInvalid = true;
}
}
return View(model);
}
//
// GET: /Admin/Calendar/Authorize
public virtual ActionResult Authorize(String code)
{
if (!String.IsNullOrEmpty(code))
{
var oldState = _authenticator.State;
_authenticator.LoadAccessToken();
}
return RedirectToAction(Actions.Index());
}
//
// GET: /Admin/Calendar/Authorize
[HttpPost]
public virtual ActionResult Authorize()
{
_userRepository.ResetCalendarAuthorization(SettingsRepository.Reddit.BotLogin);
string scope = CalendarService.Scopes.Calendar.GetStringValue();
OutgoingWebResponse response = _client.PrepareRequestUserAuthorization(new[] { scope });
response.Send(); // Will throw a ThreadAbortException to prevent sending another response.
return null;
}
}
}