Skip to content

Commit 0a517b5

Browse files
authored
Merge pull request #40 from weznagwama/claims-troubleshoot-diagnosis
Issue #39 - add error handling and verbosity around claims matching
2 parents 5eccccb + c1d77ab commit 0a517b5

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

TaskService/Controllers/TasksController.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Configuration;
45
using System.Linq;
@@ -17,7 +18,7 @@ public class TasksController : ApiController
1718
private static List<Models.Task> db = new List<Models.Task>();
1819
private static int taskId;
1920

20-
// OWIN auth middleware constants
21+
// OWIN auth middleware constants -> These claims must match what's in your JWT, like for like. Click the 'claims' tab to check.
2122
public const string scopeElement = "http://schemas.microsoft.com/identity/claims/scope";
2223
public const string objectIdElement = "http://schemas.microsoft.com/identity/claims/objectidentifier";
2324

@@ -31,7 +32,9 @@ public class TasksController : ApiController
3132
public IEnumerable<Models.Task> Get()
3233
{
3334
HasRequiredScopes(ReadPermission);
34-
string owner = ClaimsPrincipal.Current.FindFirst(objectIdElement).Value;
35+
36+
var owner = CheckClaimMatch(objectIdElement);
37+
3538
IEnumerable<Models.Task> userTasks = db.Where(t => t.Owner == owner);
3639
return userTasks;
3740
}
@@ -46,7 +49,8 @@ public void Post(Models.Task task)
4649
if (String.IsNullOrEmpty(task.Text))
4750
throw new WebException("Please provide a task description");
4851

49-
string owner = ClaimsPrincipal.Current.FindFirst(objectIdElement).Value;
52+
var owner = CheckClaimMatch(objectIdElement);
53+
5054
task.Id = taskId++;
5155
task.Owner = owner;
5256
task.Completed = false;
@@ -61,11 +65,31 @@ public void Delete(int id)
6165
{
6266
HasRequiredScopes(WritePermission);
6367

64-
string owner = ClaimsPrincipal.Current.FindFirst(objectIdElement).Value;
68+
var owner = CheckClaimMatch(objectIdElement);
69+
6570
Models.Task task = db.Where(t => t.Owner.Equals(owner) && t.Id.Equals(id)).FirstOrDefault();
6671
db.Remove(task);
6772
}
6873

74+
/*
75+
* Check user claims match task details
76+
*/
77+
private string CheckClaimMatch(string claim)
78+
{
79+
try
80+
{
81+
return ClaimsPrincipal.Current.FindFirst(claim).Value;
82+
}
83+
catch (Exception e)
84+
{
85+
throw new HttpResponseException(new HttpResponseMessage
86+
{
87+
StatusCode = HttpStatusCode.BadRequest,
88+
ReasonPhrase = $"Unable to match claim '{claim}' against user claims; click the 'claims' tab to double-check."
89+
});
90+
}
91+
}
92+
6993
// Validate to ensure the necessary scopes are present.
7094
private void HasRequiredScopes(String permission)
7195
{

TaskWebApp/Controllers/TasksController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task<ActionResult> Index()
5656
case HttpStatusCode.Unauthorized:
5757
return ErrorAction("Please sign in again. " + response.ReasonPhrase);
5858
default:
59-
return ErrorAction("Error. Status code = " + response.StatusCode);
59+
return ErrorAction("Error. Status code = " + response.StatusCode + ": " + response.ReasonPhrase);
6060
}
6161
}
6262
catch (Exception ex)

0 commit comments

Comments
 (0)