Merge pull request #18 from tparnell8/AuthenticationHelpers
add some auth helpers
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Authentication\TestAuthenticationHelper.cs" />
|
||||
<Compile Include="Class1.cs" />
|
||||
<Compile Include="Client\TestAuthenticatedUntappdCredentials.cs" />
|
||||
<Compile Include="Client\TestUnAuthenticatedUntappdCredentials.cs" />
|
||||
|
||||
65
src/Untappd.Net/Authentication/AuthenticationHelper.cs
Normal file
65
src/Untappd.Net/Authentication/AuthenticationHelper.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Redirect the user to this string.
|
||||
/// </summary>
|
||||
/// <param name="credentials"></param>
|
||||
/// <param name="redirectUrl">The URL to redirect back to your application. Should listen on code as a string param</param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Url to get the OAuth token.
|
||||
/// </summary>
|
||||
/// <param name="credentials"></param>
|
||||
/// <param name="redirectUrl"></param>
|
||||
/// <param name="code">Data returned by the initial request(see: RedirectUserTo)</param>
|
||||
/// <returns></returns>
|
||||
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,
|
||||
credentials.ClientSecret,
|
||||
redirectUrl,
|
||||
code);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,6 @@
|
||||
public struct Constants
|
||||
{
|
||||
public const string BaseRequestString = "https://api.untappd.com";
|
||||
public const string OAuthTokenEndPoint = "https://untappd.com/oauth/authorize";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Authentication\AuthenticationHelper.cs" />
|
||||
<Compile Include="Client\AuthenticatedUntappdCredentials.cs" />
|
||||
<Compile Include="Client\IAuthenticatedUntappdCredentials.cs" />
|
||||
<Compile Include="Client\IUnAuthenticadedUntappdCredentials.cs" />
|
||||
|
||||
Reference in New Issue
Block a user