From 44d2c4db2cc8b6547d920dbd77f4b016e3a93e0b Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Tue, 21 Apr 2015 20:17:51 -0400 Subject: [PATCH 1/2] add some auth helpers --- .../Authentication/AuthenticationHelper.cs | 42 +++++++++++++++++++ src/Untappd.Net/Constants.cs | 1 + src/Untappd.Net/Untappd.Net.csproj | 1 + 3 files changed, 44 insertions(+) create mode 100644 src/Untappd.Net/Authentication/AuthenticationHelper.cs diff --git a/src/Untappd.Net/Authentication/AuthenticationHelper.cs b/src/Untappd.Net/Authentication/AuthenticationHelper.cs new file mode 100644 index 0000000..7ce21e8 --- /dev/null +++ b/src/Untappd.Net/Authentication/AuthenticationHelper.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Untappd.Net.Client; +using Untappd.Net.Request; + +namespace Untappd.Net.Authentication +{ + public static class AuthenticationHelper + { + /// + /// Redirect the user to this string. + /// + /// + /// The URL to redirect back to your application. Should listen on code as a string param + /// + public static string RedirectUserTo(IUnAuthenticadedUntappdCredentials credentials, string redirectUrl) + { + return string.Format("{0}/?client_id={1}&response_type=code&redirect_url={2}", Constants.BaseRequestString, + credentials.ClientId, redirectUrl); + } + + /// + /// Url to get the OAuth token. + /// + /// + /// + /// Data returned by the initial request(see: RedirectUserTo) + /// + public static string TokenUrl(IUnAuthenticadedUntappdCredentials credentials, string redirectUrl, string code) + { + return string.Format("{0}/?client_id={1}&client_secret={2}&response_type=code&redirect_url={3}&code={4}", + Constants.OAuthTokenEndPoint, + credentials.ClientId, + credentials.ClientSecret, + redirectUrl, + code); + } + } +} diff --git a/src/Untappd.Net/Constants.cs b/src/Untappd.Net/Constants.cs index 058817f..1c72c24 100644 --- a/src/Untappd.Net/Constants.cs +++ b/src/Untappd.Net/Constants.cs @@ -3,5 +3,6 @@ public struct Constants { public const string BaseRequestString = "https://api.untappd.com"; + public const string OAuthTokenEndPoint = "https://untappd.com/oauth/authorize"; } } diff --git a/src/Untappd.Net/Untappd.Net.csproj b/src/Untappd.Net/Untappd.Net.csproj index bf3d012..e805d63 100644 --- a/src/Untappd.Net/Untappd.Net.csproj +++ b/src/Untappd.Net/Untappd.Net.csproj @@ -46,6 +46,7 @@ + From f63579884e5ea5ea8baf7bc6bfed456addcb3fe8 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Tue, 21 Apr 2015 20:29:33 -0400 Subject: [PATCH 2/2] add some argumentnull and exception handling --- .../TestAuthenticationHelper.cs | 51 +++++++++++++++++++ .../Untappd.Net.UnitTests.csproj | 1 + .../Authentication/AuthenticationHelper.cs | 23 +++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/Untappd.Net.UnitTests/Authentication/TestAuthenticationHelper.cs diff --git a/src/Untappd.Net.UnitTests/Authentication/TestAuthenticationHelper.cs b/src/Untappd.Net.UnitTests/Authentication/TestAuthenticationHelper.cs new file mode 100644 index 0000000..4d2d1b5 --- /dev/null +++ b/src/Untappd.Net.UnitTests/Authentication/TestAuthenticationHelper.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using Untappd.Net.Authentication; +using Untappd.Net.Client; + +namespace Untappd.Net.UnitTests.Authentication +{ + [TestFixture] + public class TestAuthenticationHelper + { + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void TestRedirectUserToException1() + { + + AuthenticationHelper.RedirectUserTo(null, "url"); + } + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void TestRedirectUserToException2() + { + + AuthenticationHelper.RedirectUserTo(new UnAuthenticatedUntappdCredentials("d", "d"), string.Empty); + } + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void TestTokenUrlException1() + { + + AuthenticationHelper.TokenUrl(null, "some", "code"); + } + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void TestTokenUrlException2() + { + + AuthenticationHelper.TokenUrl(new UnAuthenticatedUntappdCredentials("d", "d"), string.Empty, "code"); + } + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void TestTokenUrlException3() + { + + AuthenticationHelper.TokenUrl(new UnAuthenticatedUntappdCredentials("d", "d"), "ds", string.Empty); + } + } +} diff --git a/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj b/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj index 0986b69..9530b32 100644 --- a/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj +++ b/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj @@ -69,6 +69,7 @@ + diff --git a/src/Untappd.Net/Authentication/AuthenticationHelper.cs b/src/Untappd.Net/Authentication/AuthenticationHelper.cs index 7ce21e8..f7b59f3 100644 --- a/src/Untappd.Net/Authentication/AuthenticationHelper.cs +++ b/src/Untappd.Net/Authentication/AuthenticationHelper.cs @@ -18,6 +18,16 @@ namespace Untappd.Net.Authentication /// public static string RedirectUserTo(IUnAuthenticadedUntappdCredentials credentials, string redirectUrl) { + if (credentials == null) + { + throw new ArgumentNullException("credentials"); + } + + if (string.IsNullOrWhiteSpace(redirectUrl)) + { + throw new ArgumentNullException("redirectUrl"); + } + return string.Format("{0}/?client_id={1}&response_type=code&redirect_url={2}", Constants.BaseRequestString, credentials.ClientId, redirectUrl); } @@ -31,6 +41,19 @@ namespace Untappd.Net.Authentication /// public static string TokenUrl(IUnAuthenticadedUntappdCredentials credentials, string redirectUrl, string code) { + if (credentials == null) + { + throw new ArgumentNullException("credentials"); + } + + if (string.IsNullOrWhiteSpace(redirectUrl)) + { + throw new ArgumentNullException("redirectUrl"); + } + if (string.IsNullOrWhiteSpace(code)) + { + throw new ArgumentNullException("code"); + } return string.Format("{0}/?client_id={1}&client_secret={2}&response_type=code&redirect_url={3}&code={4}", Constants.OAuthTokenEndPoint, credentials.ClientId,