4
4
using Microsoft . AspNetCore . Authorization ;
5
5
using Microsoft . AspNetCore . Http ;
6
6
using Microsoft . AspNetCore . Mvc ;
7
+ using Microsoft . Identity . Web ;
7
8
using Microsoft . Identity . Web . Resource ;
8
9
using System ;
9
10
using System . Collections . Generic ;
@@ -30,8 +31,8 @@ public TodoListController(IHttpContextAccessor contextAccessor)
30
31
// Pre-populate with sample data
31
32
if ( TodoStore . Count == 0 )
32
33
{
33
- TodoStore . Add ( 1 , new Todo ( ) { Id = 1 , Owner = $ "{ _contextAccessor . HttpContext . User . Identity . Name } ", Title = "Pick up groceries" } ) ;
34
- TodoStore . Add ( 2 , new Todo ( ) { Id = 2 , Owner = $ "{ _contextAccessor . HttpContext . User . Identity . Name } ", Title = "Finish invoice report" } ) ;
34
+ TodoStore . Add ( 1 , new Todo ( ) { Id = 1 , Owner = $ "{ GetObjectIdClaim ( _contextAccessor . HttpContext . User ) } ", Title = "Pick up groceries" } ) ;
35
+ TodoStore . Add ( 2 , new Todo ( ) { Id = 2 , Owner = $ "{ GetObjectIdClaim ( _contextAccessor . HttpContext . User ) } ", Title = "Finish invoice report" } ) ;
35
36
TodoStore . Add ( 3 , new Todo ( ) { Id = 3 , Owner = "Other User" , Title = "Rent a car" } ) ;
36
37
TodoStore . Add ( 4 , new Todo ( ) { Id = 4 , Owner = "Other User" , Title = "Get vaccinated" } ) ;
37
38
}
@@ -45,11 +46,11 @@ public TodoListController(IHttpContextAccessor contextAccessor)
45
46
) ]
46
47
public IEnumerable < Todo > Get ( )
47
48
{
48
- if ( IsInScopes ( new string [ ] { "ToDoList.Read" , "ToDoList.ReadWrite" } ) )
49
+ if ( HasDelegatedPermissions ( new string [ ] { "ToDoList.Read" , "ToDoList.ReadWrite" } ) )
49
50
{
50
- return TodoStore . Values . Where ( x => x . Owner == User . Identity . Name ) ;
51
+ return TodoStore . Values . Where ( x => x . Owner == GetObjectIdClaim ( User ) ) ;
51
52
}
52
- else if ( IsInPermissions ( new string [ ] { "ToDoList.Read.All" , "ToDoList.ReadWrite.All" } ) )
53
+ else if ( HasApplicationPermissions ( new string [ ] { "ToDoList.Read.All" , "ToDoList.ReadWrite.All" } ) )
53
54
{
54
55
return TodoStore . Values ;
55
56
}
@@ -68,11 +69,11 @@ public Todo Get(int id)
68
69
//then it will be t.id==id && x.Owner == owner
69
70
//if it has app permissions the it will return t.id==id
70
71
71
- if ( IsInScopes ( new string [ ] { "ToDoList.Read" , "ToDoList.ReadWrite" } ) )
72
+ if ( HasDelegatedPermissions ( new string [ ] { "ToDoList.Read" , "ToDoList.ReadWrite" } ) )
72
73
{
73
- return TodoStore . Values . FirstOrDefault ( t => t . Id == id && t . Owner == User . Identity . Name ) ;
74
+ return TodoStore . Values . FirstOrDefault ( t => t . Id == id && t . Owner == GetObjectIdClaim ( User ) ) ;
74
75
}
75
- else if ( IsInPermissions ( new string [ ] { "ToDoList.Read.All" , "ToDoList.ReadWrite.All" } ) )
76
+ else if ( HasApplicationPermissions ( new string [ ] { "ToDoList.Read.All" , "ToDoList.ReadWrite.All" } ) )
76
77
{
77
78
return TodoStore . Values . FirstOrDefault ( t => t . Id == id ) ;
78
79
}
@@ -90,11 +91,11 @@ public void Delete(int id)
90
91
if (
91
92
(
92
93
93
- IsInScopes ( new string [ ] { "ToDoList.ReadWrite" } ) && TodoStore . Values . Any ( x => x . Id == id && x . Owner == User . Identity . Name ) )
94
+ HasDelegatedPermissions ( new string [ ] { "ToDoList.ReadWrite" } ) && TodoStore . Values . Any ( x => x . Id == id && x . Owner == GetObjectIdClaim ( User ) ) )
94
95
95
96
||
96
97
97
- IsInPermissions ( new string [ ] { "ToDoList.ReadWrite.All" } )
98
+ HasApplicationPermissions ( new string [ ] { "ToDoList.ReadWrite.All" } )
98
99
)
99
100
{
100
101
TodoStore . Remove ( id ) ;
@@ -113,9 +114,9 @@ public void Delete(int id)
113
114
AcceptedAppPermission = new string [ ] { "ToDoList.ReadWrite.All" } ) ]
114
115
public IActionResult Post ( [ FromBody ] Todo todo )
115
116
{
116
- var owner = HttpContext . User . Identity . Name ;
117
+ var owner = GetObjectIdClaim ( User ) ;
117
118
118
- if ( IsInPermissions ( new string [ ] { "ToDoList.ReadWrite.All" } ) )
119
+ if ( HasApplicationPermissions ( new string [ ] { "ToDoList.ReadWrite.All" } ) )
119
120
{
120
121
//with such a permission any owner name is accepted from UI
121
122
owner = todo . Owner ;
@@ -141,13 +142,13 @@ public IActionResult Patch(int id, [FromBody] Todo todo)
141
142
}
142
143
143
144
if (
144
- IsInScopes ( new string [ ] { "ToDoList.ReadWrite" } )
145
- && TodoStore . Values . Any ( x => x . Id == id && x . Owner == User . Identity . Name )
146
- && todo . Owner == User . Identity . Name
145
+ HasDelegatedPermissions ( new string [ ] { "ToDoList.ReadWrite" } )
146
+ && TodoStore . Values . Any ( x => x . Id == id && x . Owner == GetObjectIdClaim ( User ) )
147
+ && todo . Owner == GetObjectIdClaim ( User )
147
148
148
149
||
149
150
150
- IsInPermissions ( new string [ ] { "ToDoList.ReadWrite.All" } )
151
+ HasApplicationPermissions ( new string [ ] { "ToDoList.ReadWrite.All" } )
151
152
152
153
)
153
154
{
@@ -162,21 +163,29 @@ public IActionResult Patch(int id, [FromBody] Todo todo)
162
163
}
163
164
164
165
//check if the permission is inside claims
165
- private bool IsInPermissions ( string [ ] permissionsNames )
166
+ private bool HasApplicationPermissions ( string [ ] permissionsNames )
166
167
{
167
- var result = User . Claims . Where ( c => c . Type . Equals ( ClaimTypes . Role ) ) . FirstOrDefault ( ) ?
168
- . Value . Split ( ' ' ) . Any ( v => permissionsNames . Any ( p => p . Equals ( v ) ) ) ;
168
+ var rolesClaim = User . Claims . Where (
169
+ c => c . Type == ClaimConstants . Roles || c . Type == ClaimConstants . Role )
170
+ . SelectMany ( c => c . Value . Split ( ' ' ) ) ;
169
171
170
- return result ?? false ;
172
+ var result = rolesClaim . Any ( v => permissionsNames . Any ( p => p . Equals ( v ) ) ) ;
173
+
174
+ return result ;
171
175
}
172
176
173
177
//check if the scope is inside claims
174
- private bool IsInScopes ( string [ ] scopesNames )
178
+ private bool HasDelegatedPermissions ( string [ ] scopesNames )
175
179
{
176
- var result = User . Claims . Where ( c => c . Type . Equals ( Constants . ScopeClaimType ) ) . FirstOrDefault ( ) ?
180
+ var result = ( User . FindFirst ( ClaimConstants . Scp ) ?? User . FindFirst ( ClaimConstants . Scope ) ) ?
177
181
. Value . Split ( ' ' ) . Any ( v => scopesNames . Any ( s => s . Equals ( v ) ) ) ;
178
182
179
183
return result ?? false ;
180
184
}
185
+
186
+ private string GetObjectIdClaim ( ClaimsPrincipal user )
187
+ {
188
+ return ( user . FindFirst ( ClaimConstants . Oid ) ?? user . FindFirst ( ClaimConstants . ObjectId ) ) ? . Value ;
189
+ }
181
190
}
182
191
}
0 commit comments