From e307811d89acd6ab3f1d90168d0d2b97759c88ac Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 20 Jun 2017 02:47:36 +0200 Subject: [PATCH] Google Api --- Yavsc/Helpers/GoogleHelpers.cs | 96 ++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 38 deletions(-) diff --git a/Yavsc/Helpers/GoogleHelpers.cs b/Yavsc/Helpers/GoogleHelpers.cs index f541de6e..ba911642 100644 --- a/Yavsc/Helpers/GoogleHelpers.cs +++ b/Yavsc/Helpers/GoogleHelpers.cs @@ -18,6 +18,13 @@ // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . +using Newtonsoft.Json; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Net; +using System.Web; +using System.IO; using System; using System.Collections.Generic; using System.Linq; @@ -39,41 +46,7 @@ namespace Yavsc.Helpers /// public static class GoogleHelpers { -/* WAZA - /// - /// Notifies the event. - /// - /// The event. - /// Evpub. - public static async Task NotifyEvent - (this HttpClient channel, GoogleAuthSettings googleSettings, CircleEvent evpub) - { - // ASSERT ModelState.IsValid implies evpub.Circles != null - //send a MessageWithPayload to circle members - // receive MessageWithPayloadResponse - // "https://gcm-http.googleapis.com/gcm/send" - - var regids = new List(); - foreach (var c in evpub.Circles) - foreach (var u in c.Members) - { - regids.AddRange(u.Member.Devices.Select(d => d.GCMRegistrationId)); - } - if (regids.Count > 0) return null; - var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl); - request.Headers.Authorization = new AuthenticationHeaderValue("key", googleSettings.ApiKey); - var msg = new MessageWithPayload() - { - notification = new Notification() { title = evpub.Title, body = evpub.Description, icon = "icon" }, - data = evpub, - registration_ids = regids.ToArray() - }; - var response = await channel.SendAsync(request); - var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); - return payload.Value(); - } -*/ - public static MessageWithPayloadResponse NotifyEvent + public static async Task NotifyEvent (this GoogleAuthSettings googleSettings, IEnumerable regids, Event ev) where Event : IEvent { @@ -99,9 +72,9 @@ namespace Yavsc.Helpers registration_ids = regids.ToArray() }; try { - using (var m = new SimpleJsonPostMethod("https://gcm-http.googleapis.com/gcm/send",$"key={googleSettings.ApiKey}")) { - return m.Invoke(msg); - } + using (var m = new SimpleJsonPostMethod("https://gcm-http.googleapis.com/gcm/send",$"key={googleSettings.ApiKey}")) { + return await m.Invoke(msg); + } } catch (Exception ex) { throw new Exception ("Quelque chose s'est mal passé à l'envoi",ex); @@ -136,6 +109,53 @@ namespace Yavsc.Helpers return result.ToArray(); } + const string jwtHeader="{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; + const string tokenEndPoint = "https://www.googleapis.com/oauth2/v4/token"; + + static long GetTimeSpan(long seconds) { + var zero = new DateTime(1970,1,1); + return zero.AddSeconds(seconds).ToFileTimeUtc(); + } + + static object CreateGoogleServiceClaimSet(string scope, int expiresInSeconds) { + return new { + iss = Startup.GoogleSettings.Account.client_email, + scope = scope, + aud = "https://www.googleapis.com/oauth2/v4/token", + exp = GetTimeSpan(expiresInSeconds), + iat = DateTime.Now.ToFileTimeUtc() + }; + } + + public static async Task GetJsonTokenAsync(string scope) + { + var claimSet = CreateGoogleServiceClaimSet(scope, 3600); + string jsonClaims = JsonConvert.SerializeObject(claimSet); + string encClaims = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonClaims)); + string tokenHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(jwtHeader))+"."+encClaims; + + X509Certificate2 cert = new X509Certificate2(); + cert.Import(Convert.FromBase64String(Startup.GoogleSettings.Account.private_key)); + RSACryptoServiceProvider key = new RSACryptoServiceProvider(); + key.FromXmlString(cert.PrivateKey.ToXmlString(true)); + byte[] sig = key.SignData(Encoding.UTF8.GetBytes(tokenHeader), CryptoConfig.MapNameToOID("SHA256")); + string assertion = tokenHeader+"."+Convert.ToBase64String(sig); + HttpWebRequest webreq = WebRequest.CreateHttp ("https://www.googleapis.com/oauth2/v4/token"); + webreq.ContentType = "application/x-www-form-urlencoded"; + using (var inputstream = await webreq.GetRequestStreamAsync()) { + var content = Encoding.UTF8.GetBytes( "grant_type="+ HttpUtility.UrlEncode(" urn:ietf:params:oauth:grant-type:jwt-bearer")+ + "&assertion="+HttpUtility.UrlEncode(assertion)); + await inputstream.WriteAsync(content,0,content.Length); + } + using (WebResponse resp = await webreq.GetResponseAsync ()) { + using (Stream respstream = resp.GetResponseStream ()) { + using (var rdr = new StreamReader(respstream)) { + return await rdr.ReadToEndAsync(); + } + } + } + } + } }