From edd160ae75ab59d1d2b8cfb8c2e9f80c9404125f Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Wed, 10 Dec 2014 03:34:09 +0100 Subject: [PATCH] A successful OAuth2 token * TestCatalogInit.cs and TestBrands.cs: do build unit test only when TEST is defined. * Web.csproj: useless change to .Net 4.5.1 framework * GoogleController.cs: A successful OAuth2 token from Google! thanks to curl. --- SalesCatalog/Tests/TestBrands.cs | 3 ++ SalesCatalog/Tests/TestCatalogInit.cs | 3 ++ web/Controllers/GoogleController.cs | 74 +++++++++++++++------------ web/Web.csproj | 12 ++--- 4 files changed, 52 insertions(+), 40 deletions(-) diff --git a/SalesCatalog/Tests/TestBrands.cs b/SalesCatalog/Tests/TestBrands.cs index ca6b6cc6..52fd1c39 100644 --- a/SalesCatalog/Tests/TestBrands.cs +++ b/SalesCatalog/Tests/TestBrands.cs @@ -1,3 +1,5 @@ + + #if TEST using NUnit.Framework; using System; using SalesCatalog.Model; @@ -29,3 +31,4 @@ namespace SalesCatalog.Tests } } +#endif diff --git a/SalesCatalog/Tests/TestCatalogInit.cs b/SalesCatalog/Tests/TestCatalogInit.cs index 1dbeda61..697d6a98 100644 --- a/SalesCatalog/Tests/TestCatalogInit.cs +++ b/SalesCatalog/Tests/TestCatalogInit.cs @@ -1,3 +1,5 @@ +#if TEST + using System; using NUnit.Framework; using SalesCatalog.XmlImplementation; @@ -104,3 +106,4 @@ namespace SalesCatalog.Tests } } +#endif diff --git a/web/Controllers/GoogleController.cs b/web/Controllers/GoogleController.cs index 90e22472..95bc49e7 100644 --- a/web/Controllers/GoogleController.cs +++ b/web/Controllers/GoogleController.cs @@ -11,26 +11,34 @@ using Mono.Security.Protocol.Tls; using System.Net; using System.IO; using Yavsc.Model; +using Newtonsoft.Json; namespace Yavsc.Controllers { + public class TokenResult { + public string access_token { get; set; } + public string id_token { get; set; } + public int expires_in { get; set; } + public string token_type { get; set ; } + public string refresh_token { get; set; } + } + public class GoogleController : Controller { - string API_KEY="AIzaSyBV_LQHb22nGgjNvFzZwnQHjao3Q7IewRw"; + private string API_KEY="AIzaSyBV_LQHb22nGgjNvFzZwnQHjao3Q7IewRw"; - string CLIENT_ID="325408689282-6bekh7p3guj4k0f3301a6frf025cnrk1.apps.googleusercontent.com"; + private string CLIENT_ID="325408689282-6bekh7p3guj4k0f3301a6frf025cnrk1.apps.googleusercontent.com"; - string CLIENT_SECRET="MaxYcvJJCs2gDGvaELZbzwfL"; + private string CLIENT_SECRET="MaxYcvJJCs2gDGvaELZbzwfL"; string [] SCOPES = { "profile", "email" } ; - string getTokenUrl = "https://accounts.google.com/o/oauth2/token"; - // "https://www.googleapis.com/oauth2/v3/token";https://accounts.google.com/o/oauth2/token - string getCodeUrl = "https://accounts.google.com/o/oauth2/auth"; + string tokenUri = "https://accounts.google.com/o/oauth2/token"; + string authUri = "https://accounts.google.com/o/oauth2/auth"; public void Login() { @@ -53,7 +61,7 @@ namespace Yavsc.Controllers state ); - WebRequest wr = WebRequest.Create(getCodeUrl+"?"+prms); + WebRequest wr = WebRequest.Create(authUri+"?"+prms); wr.Method = "GET"; // Get the response. @@ -69,7 +77,7 @@ namespace Yavsc.Controllers } public void Auth() { - string redirectUri = Request.Url.Scheme + "://" + Request.Url.Authority + "/Google/Code"; + string redirectUri = Request.Url.Scheme + "://" + Request.Url.Authority + "/Google/Auth"; string code = Request.Params ["code"]; string error = Request.Params ["error"]; if (error != null) { @@ -84,37 +92,37 @@ namespace Yavsc.Controllers LocalizedText.ResourceManager.GetString("invalid request state"); return; } - HttpWebRequest webreq = WebRequest.CreateHttp(getTokenUrl); + + 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)); + + Byte[] bytes = System.Text.Encoding.UTF8.GetBytes (postdata); + HttpWebRequest webreq = WebRequest.CreateHttp (tokenUri); webreq.Method = "POST"; + webreq.Accept = "application/json"; webreq.ContentType = "application/x-www-form-urlencoded"; - webreq.SendChunked = true; - string postData = String.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code", - code, - CLIENT_ID, - CLIENT_SECRET, - redirectUri); - Encoding encr = new UTF8Encoding(); - Byte[] bytes = encr.GetBytes(postData); webreq.ContentLength = bytes.Length; - using (Stream dataStream = webreq.GetRequestStream()) { - dataStream.Write(bytes,0,bytes.Length); - dataStream.Close(); - } - try { - WebResponse response = webreq.GetResponse(); - string resQuery = response.ResponseUri.Query; - string cont = HttpUtility.ParseQueryString(resQuery)["continue"]; - Response.Redirect (cont); - } - catch (WebException wex) { - Response.Redirect(wex.Response.ResponseUri.AbsoluteUri); - } - } + using (Stream dataStream = webreq.GetRequestStream ()) { + dataStream.Write (bytes, 0, bytes.Length); + }; - public void Code() - { + using (WebResponse response = webreq.GetResponse ()) { + + using (Stream responseStream = response.GetResponseStream ()) { + using (StreamReader readStream = new StreamReader (responseStream, Encoding.ASCII)) { + string responseStr = readStream.ReadToEnd (); + TokenResult res = JsonConvert.DeserializeObject(responseStr); + } + } + } } + } } diff --git a/web/Web.csproj b/web/Web.csproj index 2eaa7eeb..fb6a1ce1 100644 --- a/web/Web.csproj +++ b/web/Web.csproj @@ -9,7 +9,7 @@ {349C5851-65DF-11DA-9384-00065B846F21};{603C0E0B-DB56-11DC-BE95-000D561079B0};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} Library Yavsc - v4.5 + v4.5.1 true @@ -70,25 +70,19 @@ lib\CodeKicker.BBCode.dll - - - - ..\..\..\..\..\usr\lib\mono\4.5\System.Web.Http.WebHost.dll - False ..\..\..\..\..\usr\lib\mono\4.5\System.Net.Http.Formatting.dll - False @@ -96,6 +90,10 @@ + + + +