Skip to content

Commit 2c7db45

Browse files
author
Kolappan Nathan
committed
Completed #14
1 parent 533cbb6 commit 2c7db45

File tree

12 files changed

+96
-29
lines changed

12 files changed

+96
-29
lines changed

postman/Web API Boilerplate.postman_collection.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@
368368
"bearer": [
369369
{
370370
"key": "token",
371-
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1pbiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiJFZGRhcmQgU3RhcmsiLCJ1c2VyX2lkZW50aWZpZXIiOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJjb21wYW55X2lkZW50aWZpZXIiOiJHT1QiLCJqdGkiOiI0ODZkMTdkMS0xOWNjLTQwNWMtOTUzYS1kNGFkYWFmZWQzMmUiLCJleHAiOjE1NDk4MDE0NzEsImlzcyI6IldlYkFwaUJvaWxlcnBsYXRlIiwiYXVkIjoiaHR0cHM6Ly93d3cuZXhhbXBsZS5jb20ifQ.G_I9CBLQnLFjO2LzmnMN74_sUIXYCQBhNt2Ll-VBCts",
371+
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1pbiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiJFZGRhcmQgU3RhcmsiLCJ1c2VyX2lkZW50aWZpZXIiOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJjb21wYW55X2lkZW50aWZpZXIiOiJHT1QiLCJqdGkiOiJjNGM3NzYwNy04ZTkzLTRlOWQtOTEyOC1hMmE3MThhMmY2N2QiLCJleHAiOjE1NTYwOTM1OTUsImlzcyI6IldlYkFwaUJvaWxlcnBsYXRlIiwiYXVkIjoiaHR0cHM6Ly93d3cuZXhhbXBsZS5jb20ifQ.tUfpZfEnpHslE1Sb4Xo6QcouRtWbd3MoIlFm9lhLYhY",
372372
"type": "string"
373373
}
374374
]
@@ -397,7 +397,7 @@
397397
],
398398
"variable": [
399399
{
400-
"id": "083f2568-ab9f-4d1c-ba04-1b375e74b6e8",
400+
"id": "0ec76a63-f9ff-4fd3-879b-04f9c35bf8b4",
401401
"key": "api-url",
402402
"value": "https://localhost:44332/api/",
403403
"type": "string"

src/WebApiBolierplate/API/API.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.AspNetCore.App" />
1313
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
14-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
14+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.9" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

src/WebApiBolierplate/API/Controllers/CustomBaseController.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using API.Helpers;
1+
using API.Helpers;
62
using Core.Constants;
7-
using Microsoft.AspNetCore.Http;
83
using Microsoft.AspNetCore.Mvc;
94

105
namespace API.Controllers

src/WebApiBolierplate/API/Helpers/JwtTokenBuilder.cs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ namespace API.Helpers
99
{
1010
public class JwtTokenBuilder
1111
{
12+
#region [Declarations]
13+
1214
private SecurityKey securityKey = null;
1315
private string subject = "";
1416
private string issuer = "";
1517
private string audience = "";
1618
private int expiryInDays = 30;
1719
private List<Claim> claims = new List<Claim>();
1820

21+
#endregion [Declarations]
22+
1923
public JwtSecurityToken Build()
2024
{
2125
EnsureArguments();
@@ -98,6 +102,11 @@ public JwtTokenBuilder AddExpiry(int expiryInDays)
98102

99103
#region [Adding Claims]
100104

105+
/// <summary>
106+
/// Adds user role as claim to the token
107+
/// </summary>
108+
/// <param name="value"></param>
109+
/// <returns></returns>
101110
public JwtTokenBuilder AddRole(string value)
102111
{
103112
if (value != null)
@@ -107,6 +116,11 @@ public JwtTokenBuilder AddRole(string value)
107116
return this;
108117
}
109118

119+
/// <summary>
120+
/// Adds user name as claim to the token
121+
/// </summary>
122+
/// <param name="value"></param>
123+
/// <returns></returns>
110124
public JwtTokenBuilder AddName(string value)
111125
{
112126
if (value != null)
@@ -116,48 +130,63 @@ public JwtTokenBuilder AddName(string value)
116130
return this;
117131
}
118132

119-
public JwtTokenBuilder AddClaim(string type, string value)
133+
#region [Custom Claims]
134+
135+
public JwtTokenBuilder AddUserId(string value)
120136
{
121137
if (value != null)
122138
{
123-
claims.Add(new Claim(type, value));
139+
claims.Add(new Claim(CustomClaims.UserIdentifier, value));
124140
}
125141
return this;
126142
}
127143

128-
public JwtTokenBuilder AddClaims(Dictionary<string, string> claimList)
144+
public JwtTokenBuilder AddCompanyId(string value)
129145
{
130-
foreach (var claim in claimList)
146+
if (value != null)
131147
{
132-
if (claim.Value != null && claim.Key != null)
133-
{
134-
claims.Add(new Claim(claim.Key, claim.Value));
135-
}
148+
claims.Add(new Claim(CustomClaims.CompanyIdentifier, value));
136149
}
137150
return this;
138151
}
139152

140-
#region [Custom Claims]
153+
#endregion [Custom Claims]
141154

142-
public JwtTokenBuilder AddUserId(string value)
155+
#region [Generic Claims]
156+
157+
/// <summary>
158+
/// Adds a given value to the given claim
159+
/// </summary>
160+
/// <param name="type">claim type</param>
161+
/// <param name="value">value for the claim</param>
162+
/// <returns></returns>
163+
public JwtTokenBuilder AddClaim(string type, string value)
143164
{
144165
if (value != null)
145166
{
146-
claims.Add(new Claim(CustomClaims.UserIdentifier, value));
167+
claims.Add(new Claim(type, value));
147168
}
148169
return this;
149170
}
150171

151-
public JwtTokenBuilder AddCompanyId(string value)
172+
/// <summary>
173+
/// Bulk add claims to the JWT tokens
174+
/// </summary>
175+
/// <param name="claimList">list of claim types and its corresponding values</param>
176+
/// <returns></returns>
177+
public JwtTokenBuilder AddClaims(Dictionary<string, string> claimList)
152178
{
153-
if (value != null)
179+
foreach (var claim in claimList)
154180
{
155-
claims.Add(new Claim(CustomClaims.CompanyIdentifier, value));
181+
if (claim.Value != null && claim.Key != null)
182+
{
183+
claims.Add(new Claim(claim.Key, claim.Value));
184+
}
156185
}
157186
return this;
158187
}
159188

160-
#endregion [Custom Claims]
189+
#endregion [Generic Claims]
161190

162191
#endregion [Adding Claims]
163192

src/WebApiBolierplate/API/Helpers/ValidationHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public ValidationHelper()
1212
webAPIHelper = new WebAPIHelper();
1313
}
1414

15+
/// <summary>
16+
/// Obtains all model validation errors and combaines them into a single CSV error text
17+
/// </summary>
18+
/// <param name="model"></param>
19+
/// <returns></returns>
1520
public BadRequestObjectResult GetDataValidationError(ModelStateDictionary model)
1621
{
1722
var errorText = string.Empty;

src/WebApiBolierplate/Core.Constants/Config.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
namespace Core.Constants
22
{
3+
/// <summary>
4+
/// A class to store config values. Values assigned at startup Avoids reading from config file everytime.
5+
/// </summary>
36
public static class Config
47
{
5-
8+
/// <summary>
9+
/// Configuration for CSV logger
10+
/// </summary>
611
public static class Logger
712
{
813
public static string DateFormat { get; set; }
914
public static string FileName { get; set; }
1015
}
1116

17+
/// <summary>
18+
/// Config for database, connection strings, etc...
19+
/// </summary>
1220
public static class DataBase
1321
{
1422
public static string ConnectionString { get; set; }
1523
}
1624

25+
/// <summary>
26+
/// Config needed for generating and verifying JWT auth token
27+
/// </summary>
1728
public static class JWT
1829
{
1930
public static string Key;

src/WebApiBolierplate/Core.Constants/Roles.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
namespace Core.Constants
22
{
3+
/// <summary>
4+
/// Contains all the roles of a user
5+
/// </summary>
36
public static class Roles
47
{
58
public const string Admin = "admin";
69
public const string User = "user";
710
}
811

12+
/// <summary>
13+
/// Contains roles strings used for auth purporses including combination of roles
14+
/// </summary>
915
public static class AuthRoles
1016
{
1117
// adding combained roles for auth

src/WebApiBolierplate/Core.Lib/Attributes/MinValueAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Core.Lib.Attributes
55
{
6+
/// <summary>
7+
/// Used to set minimum value validation for int, long and double values
8+
/// </summary>
69
public class MinValueAttribute : ValidationAttribute
710
{
811
private long Minimum { get; set; }

src/WebApiBolierplate/Core.Lib/Attributes/OnlyFutureAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Core.Lib.Attributes
55
{
6+
/// <summary>
7+
/// Sets validation to ensure that date value is greater than today
8+
/// </summary>
69
public class OnlyFutureAttribute : ValidationAttribute
710
{
811
public OnlyFutureAttribute()

src/WebApiBolierplate/Core.Lib/Attributes/PresentAndFutureAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Core.Lib.Attributes
55
{
6+
/// <summary>
7+
/// Similar to <see cref="OnlyFutureAttribute"/> but allows today's date value
8+
/// </summary>
69
public class PresentAndFutureAttribute : ValidationAttribute
710
{
811
public PresentAndFutureAttribute()

0 commit comments

Comments
 (0)