Skip to content

Commit c894863

Browse files
author
Kalyan Krishna
committed
added proper claim check
1 parent deb6fc3 commit c894863

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

TodoListService-ManualJwt/Controllers/TodoListController.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace TodoListService_ManualJwt.Controllers
3636
{
3737
public class TodoListController : ApiController
3838
{
39+
private string ScopeClaimType = "http://schemas.microsoft.com/identity/claims/scope";
40+
private string ScopeClaimValue = "user_impersonation";
41+
3942
//
4043
// To Do items list for all users. Since the list is stored in memory, it will go away if the service is cycled.
4144
//
@@ -44,14 +47,7 @@ public class TodoListController : ApiController
4447
// GET api/todolist
4548
public IEnumerable<TodoItem> Get()
4649
{
47-
//
48-
// The Scope claim tells you what permissions the client application has in the service.
49-
// In this case we look for a scope value of user_impersonation, or full access to the service as the user.
50-
//
51-
if (ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/scope").Value != "user_impersonation")
52-
{
53-
throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized, ReasonPhrase = "The Scope claim does not contain 'user_impersonation' or scope claim not found" });
54-
}
50+
this.CheckExpectedClaim();
5551

5652
// A user's To Do list is keyed off of the NameIdentifier claim, which contains an immutable, unique identifier for the user.
5753
Claim subject = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier);
@@ -64,15 +60,24 @@ public IEnumerable<TodoItem> Get()
6460
// POST api/todolist
6561
public void Post(TodoItem todo)
6662
{
67-
if (ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/scope").Value != "user_impersonation")
68-
{
69-
throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized, ReasonPhrase = "The Scope claim does not contain 'user_impersonation' or scope claim not found" });
70-
}
63+
this.CheckExpectedClaim();
7164

7265
if (null != todo && !string.IsNullOrWhiteSpace(todo.Title))
7366
{
7467
todoBag.Add(new TodoItem { Title = todo.Title, Owner = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value });
7568
}
7669
}
70+
71+
private void CheckExpectedClaim()
72+
{
73+
//
74+
// The Scope claim tells you what permissions the client application has in the service.
75+
// In this case we look for a scope value of user_impersonation, or full access to the service as the user.
76+
//
77+
if (!ClaimsPrincipal.Current.HasClaim(ScopeClaimType, ScopeClaimValue))
78+
{
79+
throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized, ReasonPhrase = "The Scope claim does not contain 'user_impersonation' or scope claim not found" });
80+
}
81+
}
7782
}
7883
}

0 commit comments

Comments
 (0)