Skip to content

Commit

Permalink
Merge pull request #6 from markogrady/master
Browse files Browse the repository at this point in the history
Changes to the api to allow settings to be store in secrets and azure
  • Loading branch information
MJMortimer authored Oct 16, 2017
2 parents c12df1e + 0b53088 commit aa19e9b
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 3 deletions.
46 changes: 46 additions & 0 deletions Xero.Api.Example.MVC/Controllers/HomeSettingController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Xero.Api.Example.MVC.Authenticators;
using Xero.Api.Example.MVC.Helpers;
using Xero.Api.Infrastructure.OAuth;

namespace Xero.Api.Example.MVC.Controllers
{
public class HomeSettingController : Controller
{
private IMvcAuthenticator _authenticator;
private ApiUser _user;
private ApplicationSettings _applicationSettings;
public HomeSettingController(IOptions<ApplicationSettings> applicationSettings)
{
_user = XeroApiHelper.User();
_applicationSettings = applicationSettings.Value;
_authenticator = XeroApiHelper.MvcAuthenticator(_applicationSettings);
}

public IActionResult Index()
{
return View();
}

public ActionResult Connect()
{
var authorizeUrl = _authenticator.GetRequestTokenAuthorizeUrl(_user.Identifier);

return Redirect(authorizeUrl);
}

public ActionResult Authorize(string oauth_token, string oauth_verifier, string org)
{
var accessToken = _authenticator.RetrieveAndStoreAccessToken(_user.Identifier, oauth_token, oauth_verifier);
if (accessToken == null)
return View("NoAuthorized");

return View(accessToken);
}
}
}
26 changes: 25 additions & 1 deletion Xero.Api.Example.MVC/Helpers/XeroApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Xero.Api.Example.MVC.Helpers
{
public static class XeroApiHelper
{
private static readonly IMvcAuthenticator Authenticator;
private static IMvcAuthenticator Authenticator;

static XeroApiHelper()
{
Expand Down Expand Up @@ -50,9 +50,33 @@ public static IMvcAuthenticator MvcAuthenticator()
return Authenticator;
}

public static IMvcAuthenticator MvcAuthenticator(ApplicationSettings applicationSettings)
{

// Set up some token stores to hold request and access tokens
var accessTokenStore = new MemoryTokenStore();
var requestTokenStore = new MemoryTokenStore();

// Set the application settings with an authenticator relevant to your app type
if (applicationSettings.IsPartnerApp)
{
Authenticator = new PartnerMvcAuthenticator(requestTokenStore, accessTokenStore);
}
else
{
Authenticator = new PublicMvcAuthenticator(requestTokenStore, accessTokenStore);
}
return Authenticator;
}

public static IXeroCoreApi CoreApi()
{
return new XeroCoreApi(Authenticator as IAuthenticator, User());
}

public static IXeroCoreApi CoreApi(ApplicationSettings applicationSettings)
{
return new XeroCoreApi(Authenticator as IAuthenticator,applicationSettings, User());
}
}
}
19 changes: 18 additions & 1 deletion Xero.Api.Example.MVC/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,38 @@ namespace Xero.Api.Example.MVC
{
public class Startup
{
public Startup(IConfiguration configuration)
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

builder.AddEnvironmentVariables();

if (env.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
Configuration = builder.Build();


}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ApplicationSettings>(Configuration.GetSection("XeroApi"));
services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand All @@ -32,6 +48,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler("/Home/Error");
}


app.UseStaticFiles();

Expand Down
19 changes: 19 additions & 0 deletions Xero.Api.Example.MVC/Views/HomeSetting/Authorize.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@model Xero.Api.Infrastructure.Interfaces.IToken

@{
ViewBag.Title = "Authorize";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Authorized</h2>
<h3>Token Key: @Model.TokenKey</h3>
<h3>Token Secret: @Model.TokenSecret</h3>
<h3>Expires At: @Model.ExpiresAt</h3>

@if (!string.IsNullOrEmpty(@Model.Session))
{
<h3>Session: @Model.Session</h3>
}


@Html.ActionLink("Organisation Details", "Index", "Organisation")
6 changes: 6 additions & 0 deletions Xero.Api.Example.MVC/Views/HomeSetting/Connect.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@{
ViewBag.Title = "Connect";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Connect</h2>
9 changes: 9 additions & 0 deletions Xero.Api.Example.MVC/Views/HomeSetting/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}



<a id="btnConnect" href="@Url.Action("Connect", "Home")">Connect to Xero</a>

4 changes: 4 additions & 0 deletions Xero.Api.Example.MVC/Views/HomeSetting/NoAuthorized.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@{
ViewBag.Title = "Not Authorized";
Layout = "~/Views/Shared/_Layout.cshtml";
}
1 change: 1 addition & 0 deletions Xero.Api.Example.MVC/Xero.Api.Example.MVC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Company>Xero</Company>
<Description />
<Copyright>Copyright © Xero 2017</Copyright>
<UserSecretsId>aspnet-XeroExampleWeb-5552706e-c69d-4ec8-8105-6a953e7a0971</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion Xero.Api/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using System;
using Microsoft.Extensions.Configuration;

namespace Xero.Api
{
Expand Down Expand Up @@ -26,6 +27,7 @@ public ApplicationSettings()
public string SigningCertificatePath => ApiSettings["SigningCertPath"];

public string SigningCertificatePassword => ApiSettings["SigningCertPassword"];
public bool IsPartnerApp => Convert.ToBoolean(ApiSettings["IsPartnerApp"]);


}
Expand Down
6 changes: 6 additions & 0 deletions Xero.Api/Core/XeroCoreApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public XeroCoreApi(IAuthenticator auth, IUser user = null, IRateLimiter rateLimi
Connect();
}

public XeroCoreApi(IAuthenticator auth,ApplicationSettings applicationSettings, IUser user = null, IRateLimiter rateLimiter = null)
: base(applicationSettings.BaseUrl, auth, new Consumer(applicationSettings.Key, applicationSettings.Secret), user, rateLimiter)
{
Connect();
}

public IAccountsEndpoint Accounts { get; private set; }
public AllocationsEndpoint Allocations { get; private set; }
public AttachmentsEndpoint Attachments { get; private set; }
Expand Down

0 comments on commit aa19e9b

Please sign in to comment.