* Web.csproj:

* App.master:
* IModule.cs:
* IRenderer.cs:
* DataManager.cs:
* ITagHandler.cs:
* ViewRenderer.cs:
* ResultPages.cs:
* BBCodeHelper.cs:
* IViewRenderer.cs:
* Entity.cs:
* YavscModel.csproj:
* OAuth2.cs:
* Calendar.cs:
* Profile.aspx:
* ApiClient.cs:
* MapTracks.cs:
* SalesCatalog.csproj:
* EntityQuery.cs:
* AdminController.cs:
* BlogsController.cs:
* GoogleController.cs:
* SimpleJsonPostMethod.cs:
* JsonReaderError.cs:
* AccountController.cs:
* JsonReaderError.aspx:
* GoogleErrorMessage.cs:
* GoogleErrorMessage.aspx: 

* Catalog.cs: refactoring

* Makefile: preprod is from local domain, and prod is for
  lua.pschneider.fr
This commit is contained in:
Paul Schneider 2015-01-21 18:50:02 +01:00
commit 8301159645
29 changed files with 969 additions and 314 deletions

View file

@ -130,7 +130,7 @@ namespace Yavsc.Helpers
for (int i = 1; i<=m1; i++) {
string t1 = toc [i, 0, 0];
// assert t1 != null
// ASSERT t1 != null
ttb.AppendFormat ("<a href=\"#s{0}\">{0}) {1}</a><br/>\n", i, t1);
for (int j = 1; j <= m2; j++) {

View file

@ -0,0 +1,58 @@
//
// Manager.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// Copyright (c) 2015 Paul Schneider
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Yavsc.Helpers;
using System.Web.Profile;
using Yavsc.Model.Google;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace Yavsc.Helpers.Google
{
public class ApiClient
{
protected static string CLIENT_ID = "325408689282-6bekh7p3guj4k0f3301a6frf025cnrk1.apps.googleusercontent.com";
protected static string CLIENT_SECRET = "MaxYcvJJCs2gDGvaELZbzwfL";
protected static string API_KEY="AIzaSyBV_LQHb22nGgjNvFzZwnQHjao3Q7IewRw";
/* // to use in descendence
*
protected static string getPeopleUri = "https://www.googleapis.com/plus/v1/people";
private static string authUri = "https://accounts.google.com/o/oauth2/auth";
*/
protected static string scopeTracks = "https://www.googleapis.com/auth/tracks";
protected static string scopeCalendar = "https://www.googleapis.com/auth/calendar";
protected static string[] scopeOpenid = {
"openid",
"profile",
"email"
};
// private static string dateFormat = "yyyy-MM-ddTHH:mm:ss";
}
}

View file

@ -0,0 +1,103 @@
//
// Calendar.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// Copyright (c) 2015 Paul Schneider
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Yavsc.Helpers;
using System.Web.Profile;
using Yavsc.Model.Google;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using System.Web;
namespace Yavsc.Helpers.Google
{
public class Calendar: ApiClient
{
protected static string getCalListUri = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
protected static string getCalEntriesUri = "https://www.googleapis.com/calendar/v3/calendars/{0}/events";
private static string dateFormat = "yyyy-MM-ddTHH:mm:ss";
private string timeZone = "+01:00";
public CalendarList GetCalendars (string cred, out string json)
{
CalendarList res = null;
HttpWebRequest webreq = WebRequest.CreateHttp (getCalListUri);
webreq.Headers.Add (HttpRequestHeader.Authorization, cred);
webreq.Method = "GET";
webreq.ContentType = "application/http";
using (WebResponse resp = webreq.GetResponse ()) {
using (Stream respstream = resp.GetResponseStream ()) {
using (StreamReader readresp = new StreamReader (respstream, Encoding.UTF8)) {
json = readresp.ReadToEnd ();
res = JsonConvert.DeserializeObject<CalendarList> (json);
}
}
resp.Close ();
}
webreq.Abort ();
return res;
}
public CalendarEntryList GetCalendar (string calid, DateTime mindate, DateTime maxdate, ProfileBase upr, out string responseStr)
{
string uri = string.Format (
getCalEntriesUri, HttpUtility.UrlEncode (calid)) +
string.Format ("?orderBy=startTime&singleEvents=true&timeMin={0}&timeMax={1}&key=" + API_KEY,
HttpUtility.UrlEncode (mindate.ToString (dateFormat) + timeZone),
HttpUtility.UrlEncode (maxdate.ToString (dateFormat) + timeZone));
HttpWebRequest webreq = WebRequest.CreateHttp (uri);
string cred = OAuth2.GetFreshGoogleCredential (upr);
webreq.Headers.Add (HttpRequestHeader.Authorization, cred);
webreq.Method = "GET";
webreq.ContentType = "application/http";
CalendarEntryList res = null;
try {
using (WebResponse resp = webreq.GetResponse ()) {
using (Stream respstream = resp.GetResponseStream ()) {
using (StreamReader readresp = new StreamReader (respstream, Encoding.UTF8)) {
responseStr = readresp.ReadToEnd ();
try {
res = JsonConvert.DeserializeObject<CalendarEntryList> (responseStr);
} catch (JsonReaderException ex) {
respstream.Close ();
resp.Close ();
webreq.Abort ();
throw new GoogleErrorException(ex,responseStr);
}
}
respstream.Close ();
}
resp.Close ();
}
} catch (WebException ex) {
webreq.Abort ();
throw new GoogleErrorException (ex);
}
webreq.Abort ();
return res;
}
}
}

View file

@ -0,0 +1,48 @@
//
// Entity.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// Copyright (c) 2015 Paul Schneider
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Yavsc.Helpers;
using System.Web.Profile;
using Yavsc.Model.Google;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace Yavsc.Helpers.Google
{
public class Entity
{
public string ID;
public string Name;
/// <summary>
/// The type:
// AUTOMOBILE: A car or passenger vehicle.
// TRUCK: A truck or cargo vehicle.
// WATERCRAFT: A boat or other waterborne vehicle.
// PERSON: A person.
/// </summary>
public string Type;
}
}

View file

@ -0,0 +1,38 @@
//
// EntityQuery.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// Copyright (c) 2015 Paul Schneider
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Yavsc.Helpers;
using System.Web.Profile;
using Yavsc.Model.Google;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace Yavsc.Helpers.Google
{
public class EntityQuery {
public string [] EntityIds;
public string MinId;
}
}

View file

@ -0,0 +1,65 @@
//
// Google.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// Copyright (c) 2015 Paul Schneider
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Yavsc.Helpers;
using System.Web.Profile;
using Yavsc.Model.Google;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace Yavsc.Helpers.Google
{
public class MapTracks:ApiClient {
protected static string googleMapTracksPath = "https://www.googleapis.com/tracks/v1/";
// entities/[create|list|delete]
// collections/[list|create|[add|remove]entities|delete]
// crumbs/[record|getrecent|gethistory|report|summarize|getlocationinfo|delete
// entities/[create|list|delete]
// collections/[list|create|[add|remove]entities|delete]
// crumbs/[record|getrecent|gethistory|report|summarize|getlocationinfo|delete
static string [] CreateEntity( Entity[] entities ) {
string [] ans = null;
using (SimpleJsonPostMethod< Entity[] ,string []> wr =
new SimpleJsonPostMethod< Entity[] ,string[]> (googleMapTracksPath + "entities/create"))
{
ans = wr.Invoke (entities);
}
return ans;
}
static Entity[] ListEntities (EntityQuery eq)
{
Entity [] ans = null;
using (SimpleJsonPostMethod<EntityQuery,Entity[]> wr =
new SimpleJsonPostMethod<EntityQuery,Entity[]> (googleMapTracksPath + "entities/create"))
{
ans = wr.Invoke (eq);
}
return ans;
}
}
}

View file

@ -0,0 +1,217 @@
//
// OAuth2.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// Copyright (c) 2015 Paul Schneider
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Yavsc.Model.Google;
using System.Web.Profile;
using System.Web;
using Yavsc.Model;
namespace Yavsc.Helpers.Google
{
public class PeopleApi: ApiClient
{
private static string getPeopleUri = "https://www.googleapis.com/plus/v1/people";
public static People GetMe (AuthToken gat)
{
People me;
HttpWebRequest webreppro = WebRequest.CreateHttp (getPeopleUri + "/me");
webreppro.ContentType = "application/http";
webreppro.Headers.Add (HttpRequestHeader.Authorization, gat.token_type + " " + gat.access_token);
webreppro.Method = "GET";
using (WebResponse proresp = webreppro.GetResponse ()) {
using (Stream prresponseStream = proresp.GetResponseStream ()) {
using (StreamReader readproresp = new StreamReader (prresponseStream, Encoding.UTF8)) {
string prresponseStr = readproresp.ReadToEnd ();
me = JsonConvert.DeserializeObject<People> (prresponseStr);
}
prresponseStream.Close ();
}
proresp.Close ();
}
webreppro.Abort ();
return me;
}
}
public class OAuth2:ApiClient
{
protected static string tokenUri = "https://accounts.google.com/o/oauth2/token";
protected static string authUri = "https://accounts.google.com/o/oauth2/auth";
public string RedirectUri { get; set; }
public OAuth2 (string redirectUri)
{
RedirectUri = redirectUri;
}
/// <summary>
/// Login with Google
/// by redirecting the specified http web response bresp,
/// and using the specified session state.
/// </summary>
/// <param name="bresp">Bresp.</param>
/// <param name="state">State.</param>
public void Login (HttpResponseBase bresp, string state)
{
string scope = string.Join ("%20", scopeOpenid);
string prms = String.Format ("response_type=code&client_id={0}&redirect_uri={1}&scope={2}&state={3}&include_granted_scopes=false",
CLIENT_ID, RedirectUri, scope, state);
GetAuthResponse (bresp, prms);
}
public void GetCalAuth (HttpResponseBase bresp, string state)
{
string scope = string.Join ("%20", scopeOpenid);
scope = string.Join ("%20", scopeCalendar);
string prms = String.Format ("response_type=code&client_id={0}&redirect_uri={1}&scope={2}&state={3}&include_granted_scopes=false&access_type=offline",
CLIENT_ID, RedirectUri, scope, state);
GetAuthResponse (bresp, prms);
}
private void GetAuthResponse (HttpResponseBase bresp, string prms)
{
string cont = null;
WebRequest wr = WebRequest.Create (authUri + "?" + prms);
wr.Method = "GET";
using (
WebResponse response = wr.GetResponse ()) {
string resQuery = response.ResponseUri.Query;
cont = HttpUtility.ParseQueryString (resQuery) ["continue"];
response.Close ();
}
wr.Abort ();
bresp.Redirect (cont);
}
/// <summary>
/// Builds the post data, from code given
/// by Google in the request parameters,
/// and using the given <c>redirectUri</c>.
/// This request body is used to get a new
/// OAuth2 token from Google, it is Url encoded.
/// </summary>
/// <returns>The post data from code.</returns>
/// <param name="redirectUri">Redirect URI.</param>
/// <param name="code">Code.</param>
public static string TokenPostDataFromCode (string redirectUri, string code)
{
string postdata =
string.Format (
"redirect_uri={0}&client_id={1}&client_secret={2}&code={3}&grant_type=authorization_code",
HttpUtility.UrlEncode (redirectUri),
HttpUtility.UrlEncode (CLIENT_ID),
HttpUtility.UrlEncode (CLIENT_SECRET),
HttpUtility.UrlEncode (code));
return postdata;
}
public AuthToken GetToken (HttpRequestBase rq, string state, out string message)
{
string code = OAuth2.GetCodeFromRequest (rq, state, out message);
string postdata = OAuth2.TokenPostDataFromCode (RedirectUri, code);
return GetTokenPosting (postdata);
}
[Obsolete ("Use GetToken instead.")]
public static AuthToken GetTokenFromBody (string postdata)
{
throw new NotImplementedException ();
}
internal static AuthToken GetTokenPosting (string postdata)
{
HttpWebRequest webreq = WebRequest.CreateHttp (tokenUri);
webreq.Method = "POST";
webreq.Accept = "application/json";
webreq.ContentType = "application/x-www-form-urlencoded";
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes (postdata);
webreq.ContentLength = bytes.Length;
using (Stream dataStream = webreq.GetRequestStream ()) {
dataStream.Write (bytes, 0, bytes.Length);
dataStream.Close ();
}
AuthToken gat = null;
using (WebResponse response = webreq.GetResponse ()) {
using (Stream responseStream = response.GetResponseStream ()) {
using (StreamReader readStream = new StreamReader (responseStream, Encoding.UTF8)) {
string responseStr = readStream.ReadToEnd ();
gat = JsonConvert.DeserializeObject<AuthToken> (responseStr);
readStream.Close ();
}
responseStream.Close ();
}
response.Close ();
}
webreq.Abort ();
return gat;
}
public static string GetCodeFromRequest (HttpRequestBase rq, string state, out string message)
{
message = "";
string code = rq.Params ["code"];
string error = rq.Params ["error"];
if (error != null) {
message =
string.Format (LocalizedText.Google_error,
LocalizedText.ResourceManager.GetString (error));
return null;
}
string rqstate = rq.Params ["state"];
if (state != null && string.Compare (rqstate, state) != 0) {
message =
LocalizedText.ResourceManager.GetString ("invalid request state");
return null;
}
return code;
}
public static string GetFreshGoogleCredential (ProfileBase pr)
{
string token = (string)pr.GetPropertyValue ("gtoken");
string token_type = (string)pr.GetPropertyValue ("gtokentype");
DateTime token_exp = (DateTime)pr.GetPropertyValue ("gtokenexpir");
if (token_exp < DateTime.Now) {
string refresh_token = (string)pr.GetPropertyValue ("grefreshtoken");
AuthToken gat = OAuth2.GetTokenPosting (
string.Format ("grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}",
CLIENT_ID, CLIENT_SECRET, refresh_token));
token = gat.access_token;
pr.SetPropertyValue ("gtoken", token);
pr.Save ();
// ASSERT gat.token_type == pr.GetPropertyValue("token_type")
}
return token_type + " " + token;
}
}
}

View file

@ -0,0 +1,91 @@
//
// PostJson.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// Copyright (c) 2015 Paul Schneider
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Net;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Yavsc.Helpers
{
public class SimpleJsonPostMethod<TQuery,TAnswer>: IDisposable
{
internal HttpWebRequest request = null;
internal HttpWebRequest Request { get { return request; } }
string CharSet {
get { return Request.TransferEncoding; }
set { Request.TransferEncoding=value;}
}
string Method { get { return Request.Method; } }
public string Path {
get{ return Request.RequestUri.ToString(); }
}
public void SetCredential(string cred) {
Request.Headers.Add(HttpRequestHeader.Authorization,cred);
}
public SimpleJsonPostMethod (string pathToMethod)
{
// ASSERT Request == null
request = WebRequest.CreateHttp (pathToMethod);
Request.Method = "POST";
Request.Accept = "application/json";
Request.ContentType = "application/json";
Request.TransferEncoding = "UTF-8";
}
public TAnswer Invoke(TQuery query)
{
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes (JsonConvert.SerializeObject(query));
Request.ContentLength = bytes.Length;
using (Stream dataStream = Request.GetRequestStream ()) {
dataStream.Write (bytes, 0, bytes.Length);
dataStream.Close ();
}
TAnswer ans = default (TAnswer);
using (WebResponse response = Request.GetResponse ()) {
using (Stream responseStream = response.GetResponseStream ()) {
using (StreamReader readStream = new StreamReader (responseStream, Encoding.UTF8)) {
string responseStr = readStream.ReadToEnd ();
ans = JsonConvert.DeserializeObject<TAnswer> (responseStr);
readStream.Close ();
}
responseStream.Close ();
}
response.Close();
}
return ans;
}
#region IDisposable implementation
public void Dispose ()
{
if (Request != null) Request.Abort ();
}
#endregion
}
}