Skip to content

Commit adf214a

Browse files
bable5Sean Mooney
authored andcommitted
Follow the 'connections' uri when present
* Full access to the API requires both user consent and designating which organizations the user wants the application to be able to interact with. Follow the 'connections' uri if present on any org to allow the user finish the setup process. * The connections app knows how to issue a redirct when the select-organization route is opened with a `redirect_uri` query parameter. This can send the user back to the original app once they have completed the organization selection process and makes exercising the api through the demo app smoother.
1 parent 1da808a commit adf214a

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

CSharpApp/Controllers/HomeController.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ public IActionResult Index()
2727
_settings.ClientId = _configuration["JDeere:ClientId"];
2828
_settings.ClientSecret = _configuration["JDeere:ClientSecret"];
2929
_settings.WellKnown = _configuration["JDeere:WellKnown"];
30-
_settings.CallbackUrl = _configuration["JDeere:CallbackUrl"];
30+
_settings.ServerUrl = _configuration["JDeere:ServerUrl"];
31+
_settings.CallbackUrl = _settings.ServerUrl + _configuration["JDeere:Callback"];
3132
_settings.Scopes = _configuration["JDeere:Scopes"];
3233
_settings.State = _configuration["JDeere:State"];
3334
_settings.APIURL = _configuration["JDeere:ApiUrl"];
34-
35+
3536
ViewBag.Settings = _settings;
3637

3738
return View();
@@ -87,6 +88,12 @@ public async Task<IActionResult> Callback(string code, string state)
8788
var responseContent = await response.Content.ReadAsStringAsync();
8889
_settings.AccessToken = JsonConvert.DeserializeObject<Token>(responseContent);
8990

91+
string organizationAccessUrl = await NeedsOrganizationAccess();
92+
if (organizationAccessUrl != null)
93+
{
94+
return Redirect(organizationAccessUrl);
95+
}
96+
9097
ViewBag.Settings = _settings;
9198

9299
return View("Index");
@@ -165,6 +172,36 @@ private async Task<HttpResponseMessage> SecuredApiGetRequest(string url)
165172

166173
return await client.GetAsync(url);
167174
}
175+
176+
/// <summary>Check to see if the 'connections' rel is present for any organization.
177+
/// If the rel is present it means the oauth application has not completed it's
178+
/// access to an organization and must redirect the user to the uri provided
179+
/// in the link.</summary>
180+
/// <returns>A redirect uri if the <code>connections</code>
181+
/// connections rel is present or <null> if no redirect is
182+
/// required to finish the setup.</returns>
183+
private async Task<string> NeedsOrganizationAccess()
184+
{
185+
var response = await SecuredApiGetRequest(_settings.APIURL + "/organizations");
186+
187+
response.EnsureSuccessStatusCode();
188+
var responseContent = await response.Content.ReadAsStringAsync();
189+
var dynorg = JsonConvert.DeserializeObject<dynamic>(responseContent);
190+
191+
foreach (var organization in dynorg.values)
192+
{
193+
foreach (var link in organization.links)
194+
{
195+
string rel = link.rel;
196+
if (rel == "connections")
197+
{
198+
string connectionsLink = link.uri;
199+
return QueryHelpers.AddQueryString(connectionsLink, "redirect_uri", _settings.ServerUrl);
200+
}
201+
}
202+
}
203+
return null;
204+
}
168205
}
169206
}
170207

CSharpApp/Models/JDeere.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
namespace CSharpApp.Models
1+
namespace CSharpApp.Models
22
{
33
public class JDeere
44
{
55
public string ClientId { get; set; }
66
public string ClientSecret { get; set; }
77
public string WellKnown { get; set; }
8+
public string ServerUrl { get; set; }
89
public string CallbackUrl { get; set; }
910
public Token AccessToken { get; set; }
1011
public string Scopes { get; set; }

CSharpApp/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"ApiUrl": "https://sandboxapi.deere.com/platform",
1414
"Scopes": "openid profile offline_access ag1 eq1",
1515
"State": "test state",
16-
"CallbackUrl": "http://localhost:9090/callback"
16+
"ServerUrl": "http://localhost:9090",
17+
"Callback": "/callback"
1718
},
1819
"AllowedHosts": "*"
1920
}

0 commit comments

Comments
 (0)