Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 266e423

Browse files
WIP : Authorization applied
1 parent f104baf commit 266e423

File tree

6 files changed

+92
-33
lines changed

6 files changed

+92
-33
lines changed

‎server/AuthWebApplication/AuthWebApplication/Controllers/AuthorizeTokenController.cs

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,115 @@
1717

1818
namespace AuthWebApplication.Controllers
1919
{
20-
[Authorize]
20+
//[Authorize]
2121
[Route("api/[controller]")]
2222
[ApiController]
2323
public class AuthorizeTokenController : ControllerBase
2424
{
2525
private RedisService redisService;
2626
private readonly ILogger<AuthorizeTokenController> logger;
2727

28-
public AuthorizeTokenController(ILogger<AuthorizeTokenController> logger,RedisService redisService)
28+
public AuthorizeTokenController(ILogger<AuthorizeTokenController> logger,RedisService redisService)
2929
{
3030
this.logger = logger;
3131
this.redisService = redisService;
3232
}
3333

34-
public async Task<IActionResult> Get(string resource)
35-
{
36-
var userName = this.User.Identity.Name;
37-
var claimsIdentity = this.User.Identities.First() as ClaimsIdentity;
38-
var claim = claimsIdentity.Claims.First(x => x.Type == JwtRegisteredClaimNames.Jti);
39-
var jti = claim.Value;
34+
// public async Task<IActionResult> Get(string resource)
35+
// {
36+
// var userName = this.User.Identity.Name;
37+
// var claimsIdentity = this.User.Identities.First() as ClaimsIdentity;
38+
// var claim = claimsIdentity.Claims.First(x => x.Type == JwtRegisteredClaimNames.Jti);
39+
// var jti = claim.Value;
40+
41+
// var inValid = string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(jti) || string.IsNullOrWhiteSpace(resource);
42+
// if (inValid)
43+
// {
44+
// return Unauthorized("Invalid data");
45+
// }
46+
47+
// var redisValue = await redisService.Get(userName);
48+
// if (string.IsNullOrWhiteSpace(redisValue))
49+
// {
50+
// return Unauthorized(userName);
51+
// }
52+
53+
// var dbValue = (dynamic)JsonConvert.DeserializeObject(redisValue);
54+
// var jtiArray = ((dbValue as dynamic).jtis as dynamic) as JArray;
55+
// var list = jtiArray.ToObject<List<string>>();
56+
// var validJti = list.Exists(x => x == jti);
57+
58+
// if (!validJti)
59+
// {
60+
// return Unauthorized(jti);
61+
// }
62+
63+
// var permissionViewModels = JsonConvert.DeserializeObject<List<ApplicationPermissionViewModel>>(
64+
// ((dbValue as dynamic).resources as JValue).ToString());
65+
// var permitted = permissionViewModels.Exists(x => x.Name == resource && Convert.ToBoolean(x.IsAllowed));
4066

41-
var inValid = string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(jti) || string.IsNullOrWhiteSpace(resource);
67+
// if (!permitted)
68+
// {
69+
// return Forbid("Bearer");
70+
// }
71+
72+
// return Ok();
73+
// }
74+
75+
public async Task<IActionResult> Get(string user, string resource, string jti)
76+
{
77+
var inValid = string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(jti) || string.IsNullOrWhiteSpace(resource);
4278
if (inValid)
4379
{
44-
return Unauthorized("Invalid data");
80+
return Unauthorized("Invalid data. User or resource or jti cannot be empty");
4581
}
4682

47-
var redisValue = await redisService.Get(userName);
83+
var redisValue = await redisService.Get(user);
4884
if (string.IsNullOrWhiteSpace(redisValue))
4985
{
50-
return Unauthorized(userName);
86+
return Unauthorized(user);
5187
}
5288

53-
var dbValue = (dynamic)JsonConvert.DeserializeObject(redisValue);
54-
var jtiArray = ((dbValue as dynamic).jtis as dynamic) as JArray;
55-
var list = jtiArray.ToObject<List<string>>();
56-
var validJti = list.Exists(x => x == jti);
89+
var dbValue = JsonConvert.DeserializeObject<AuthorizationDataModel>(redisValue);
5790

58-
if (!validJti)
91+
if (dbValue==null)
5992
{
60-
return Unauthorized(jti);
93+
return Unauthorized("Invalid cache. Please logout and do a fresh login");
6194
}
6295

63-
var permissionViewModels = JsonConvert.DeserializeObject<List<ApplicationPermissionViewModel>>(
64-
((dbValue as dynamic).resources as JValue).ToString());
65-
var permitted = permissionViewModels.Exists(x => x.Name == resource && Convert.ToBoolean(x.IsAllowed));
96+
var validJti = dbValue.jtis.Exists(x => x == jti);
6697

67-
if (!permitted)
98+
if (!validJti)
6899
{
69-
return Forbid("Bearer");
100+
return Unauthorized(jti);
70101
}
71102

103+
// var permissionViewModels = JsonConvert.DeserializeObject<List<ApplicationPermissionViewModel>>(
104+
// ((dbValue as dynamic).resources as JValue).ToString());
105+
// var permitted = permissionViewModels.Exists(x => x.Name == resource && Convert.ToBoolean(x.IsAllowed));
106+
107+
// if (!permitted)
108+
// {
109+
// return Forbid("Bearer");
110+
// }
111+
72112
return Ok();
73113
}
114+
115+
private class AuthorizationDataModel
116+
{
117+
public List<string> jtis { get; set; }
118+
119+
public List<AuthorizationResourceModel> resources { get; set; }
120+
}
121+
122+
private class AuthorizationResourceModel
123+
{
124+
public string Name { get; set; }
125+
126+
public string IsAllowed { get; set; }
127+
128+
public string IsDisabled { get; set; }
129+
}
74130
}
75131
}

‎server/AuthWebApplication/AuthWebApplication/Controllers/TokenController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task<ActionResult> Post([FromBody] LoginViewModel loginViewModel)
108108

109109
await securityDb.UserTokens.AddAsync(token);
110110
await securityDb.SaveChangesAsync();
111-
await redisService.Set(token.Name, jwtOptions.ValidFor, token.Jti, JsonConvert.SerializeObject(resources));
111+
await redisService.Set(token.Name, jwtOptions.ValidFor, token.Jti, resources);
112112
return Ok(jwt);
113113
}
114114

‎server/RedisLibrary/Services/RedisService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void Connect()
4343
logger.LogDebug("Connected to Redis");
4444
}
4545

46-
public async Task<bool> Set(string key, TimeSpan expiry, string tokenJti, string resources)
46+
public async Task<bool> Set(string key, TimeSpan expiry, string tokenJti, List<dynamic> resources)
4747
{
4848
var db = _redis.GetDatabase();
4949
var redisValue = await db.StringGetAsync(key);

‎server/RedisLibrary/obj/AuthLibrary.csproj.nuget.dgspec.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"privateAssets": "all"
8080
}
8181
},
82-
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.100\\RuntimeIdentifierGraph.json"
82+
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.101\\RuntimeIdentifierGraph.json"
8383
}
8484
}
8585
}

‎server/RedisLibrary/obj/project.assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@
12771277
"privateAssets": "all"
12781278
}
12791279
},
1280-
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.100\\RuntimeIdentifierGraph.json"
1280+
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.101\\RuntimeIdentifierGraph.json"
12811281
}
12821282
}
12831283
}

‎server/WebApplication2/WebApplication2/Attributes/TokenAuthorizeAttribute.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void OnAuthorization(AuthorizationFilterContext context)
2424
var token = context.HttpContext.Request.Headers["Authorization"].ToString();
2525
try
2626
{
27-
HttpAuthorization(resource,token);
27+
HttpAuthorization(context,resource);
2828

2929
// GrpcAuthorization(token, resource);
3030

@@ -58,18 +58,21 @@ private void GoAuthorization(AuthorizationFilterContext context, string resource
5858
httpResponseMessage.EnsureSuccessStatusCode();
5959
}
6060

61-
private staticstring HttpAuthorization(stringresource, string token)
61+
private string HttpAuthorization(AuthorizationFilterContextcontext, string resource)
6262
{
6363
var client = Factory.HttpClient;
64-
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(token);
65-
var url = $"{Constants.AuthServer}/api/AuthorizeToken?resource={resource}";
64+
var user = context.HttpContext.User.Identity.Name;
65+
var claimsIdentity = context.HttpContext.User.Identities.First() as ClaimsIdentity;
66+
var claim = claimsIdentity.Claims.First(x => x.Type == JwtRegisteredClaimNames.Jti);
67+
var jti = claim.Value;
68+
var url = $"{Constants.AuthServer}/api/AuthorizeToken?user={user}&resource={resource}&jti={jti}";
6669
var httpResponseMessage = client.GetAsync(url).GetAwaiter().GetResult();
6770
httpResponseMessage.EnsureSuccessStatusCode();
68-
return token;
71+
return jti;
6972
}
7073

7174

72-
private staticvoid GrpcAuthorization(string token, string resource)
75+
private void GrpcAuthorization(string token, string resource)
7376
{
7477
Greeter.GreeterClient gClient = Factory.GreeterClient;
7578

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /