-
Notifications
You must be signed in to change notification settings - Fork 3
/
HomeController.cs
122 lines (101 loc) · 3.82 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Xero.NetStandard.OAuth2.Client;
using WorkflowMaxOAuth2Sample.Example;
using WorkflowMaxOAuth2Sample.Extensions;
using WorkflowMaxOAuth2Sample.Models;
using WorkflowMaxOAuth2Sample.Models.ClientApi;
namespace WorkflowMaxOAuth2Sample.Controllers
{
public class HomeController : Controller
{
private readonly MemoryTokenStore _tokenStore;
private readonly IXeroClient _xeroClient;
private readonly IHttpClientFactory _httpClientFactory;
public HomeController(MemoryTokenStore tokenStore, IXeroClient xeroClient, IHttpClientFactory httpClientFactory)
{
_tokenStore = tokenStore;
_xeroClient = xeroClient;
_httpClientFactory = httpClientFactory;
}
[HttpGet]
public IActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction(nameof(TenantClientLists));
}
return View();
}
[HttpGet]
[Authorize]
public async Task<IActionResult> TenantClientLists()
{
var token = await _tokenStore.GetAccessTokenAsync(User.XeroUserId());
var connections = await _xeroClient.GetConnectionsAsync(token);
connections = connections.Where(c => c.TenantType == "WORKFLOWMAX").ToList();
if (!connections.Any())
{
return RedirectToAction(nameof(NoTenants));
}
var data = new List<(Guid tenantId, ClientListResponse clients)>();
var client = _httpClientFactory.CreateClient("WorkflowMax");
client.SetBearerToken(token.AccessToken);
//Retrieve the WorkflowMax clients for each connection we have access to
foreach (var connection in connections)
{
ClientListResponse clients = await GetClients(client, connection.TenantId);
data.Add((connection.TenantId, clients));
}
var model = new TenantClientListsModel
{
LoggedInUser = $"{User.FindFirstValue(ClaimTypes.GivenName)} {User.FindFirstValue(ClaimTypes.Surname)}",
TenantClients = data
};
return View(model);
}
private static async Task<ClientListResponse> GetClients(HttpClient client, Guid tenantId)
{
var request = new HttpRequestMessage
{
RequestUri = new Uri("client.api/list", UriKind.Relative),
Headers = { { "Xero-Tenant-Id", tenantId.ToString() } }
};
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var clients = await response.Content.ReadAsAsync<ClientListResponse>(new[]
{
new XmlMediaTypeFormatter
{
UseXmlSerializer = true
}
});
return clients;
}
[HttpGet]
[Authorize]
public IActionResult NoTenants()
{
return View();
}
[HttpGet]
[Authorize(AuthenticationSchemes = "XeroSignIn")]
public IActionResult SignIn()
{
return RedirectToAction(nameof(TenantClientLists));
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}