Skip to content

Commit 101138e

Browse files
author
Kolappan Nathan
committed
Fixes #20
1 parent 2c7db45 commit 101138e

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.1] - 2019-03-25
8+
### Added
9+
- Added documentaion for many functions and classes
10+
- New claims for JWT token
11+
12+
### Changed
13+
- Updated NuGet package
14+
- Replace induvidual claim adding code using a common function
15+
716
## [1.0.0] - 2019-01-11
817
### Added
918
- Added JWT authorization.

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.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1pbiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiJFZGRhcmQgU3RhcmsiLCJ1c2VyX2lkZW50aWZpZXIiOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJjb21wYW55X2lkZW50aWZpZXIiOiJHT1QiLCJqdGkiOiJjNGM3NzYwNy04ZTkzLTRlOWQtOTEyOC1hMmE3MThhMmY2N2QiLCJleHAiOjE1NTYwOTM1OTUsImlzcyI6IldlYkFwaUJvaWxlcnBsYXRlIiwiYXVkIjoiaHR0cHM6Ly93d3cuZXhhbXBsZS5jb20ifQ.tUfpZfEnpHslE1Sb4Xo6QcouRtWbd3MoIlFm9lhLYhY",
371+
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJXZWJBcGlCb2lsZXJwbGF0ZSIsImF1ZCI6WyJodHRwczovL3d3dy5leGFtcGxlLmNvbSIsImh0dHBzOi8vd3d3LmV4YW1wbGUuY29tIl0sImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6ImFkbWluIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IkVkZGFyZCBTdGFyayIsInVzZXJfaWRlbnRpZmllciI6IjAwMDAwMDAwLTAwMDAtMDAwMC0wMDAwLTAwMDAwMDAwMDAwMCIsImNvbXBhbnlfaWRlbnRpZmllciI6IkdPVCIsImp0aSI6ImJhMjkyNGE1LTBkMWQtNGZlNi05ZTYzLTMwNTJjNzMwZTQzZiIsImlhdCI6IjI1LTAzLTIwMTkgMTAuNDAuMDQgQU0iLCJleHAiOjE1NTYxMDI0MDR9.LVJ9XqqR7FqLC2l5wcAzJuZgkdusrd7U08YrJ0UW37Q",
372372
"type": "string"
373373
}
374374
]
@@ -397,7 +397,7 @@
397397
],
398398
"variable": [
399399
{
400-
"id": "0ec76a63-f9ff-4fd3-879b-04f9c35bf8b4",
400+
"id": "27307e0d-ed1c-4c21-abd9-3f812016da7c",
401401
"key": "api-url",
402402
"value": "https://localhost:44332/api/",
403403
"type": "string"

src/WebApiBolierplate/API/Helpers/JwtTokenBuilder.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public JwtSecurityToken Build()
2424
{
2525
EnsureArguments();
2626

27-
claims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
27+
AddClaim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString());
28+
AddClaim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString());
2829

2930
var token = new JwtSecurityToken(
3031
issuer: issuer,
@@ -69,7 +70,7 @@ public JwtTokenBuilder AddSecurityKey(SecurityKey securityKey)
6970
public JwtTokenBuilder AddSubject(string subject)
7071
{
7172
this.subject = subject;
72-
claims.Add(new Claim(JwtRegisteredClaimNames.Sub, this.subject));
73+
AddClaim(JwtRegisteredClaimNames.Sub, this.subject);
7374
return this;
7475
}
7576

@@ -79,6 +80,7 @@ public JwtTokenBuilder AddSubject(string subject)
7980
public JwtTokenBuilder AddIssuer(string issuer)
8081
{
8182
this.issuer = issuer;
83+
AddClaim(JwtRegisteredClaimNames.Iss, issuer);
8284
return this;
8385
}
8486

@@ -88,6 +90,7 @@ public JwtTokenBuilder AddIssuer(string issuer)
8890
public JwtTokenBuilder AddAudience(string audience)
8991
{
9092
this.audience = audience;
93+
AddClaim(JwtRegisteredClaimNames.Aud, audience);
9194
return this;
9295
}
9396

@@ -109,11 +112,7 @@ public JwtTokenBuilder AddExpiry(int expiryInDays)
109112
/// <returns></returns>
110113
public JwtTokenBuilder AddRole(string value)
111114
{
112-
if (value != null)
113-
{
114-
claims.Add(new Claim(ClaimTypes.Role, value));
115-
}
116-
return this;
115+
return AddClaim(ClaimTypes.Role, value);
117116
}
118117

119118
/// <summary>
@@ -123,31 +122,19 @@ public JwtTokenBuilder AddRole(string value)
123122
/// <returns></returns>
124123
public JwtTokenBuilder AddName(string value)
125124
{
126-
if (value != null)
127-
{
128-
claims.Add(new Claim(ClaimTypes.Name, value));
129-
}
130-
return this;
125+
return AddClaim(ClaimTypes.Name, value);
131126
}
132127

133128
#region [Custom Claims]
134129

135130
public JwtTokenBuilder AddUserId(string value)
136131
{
137-
if (value != null)
138-
{
139-
claims.Add(new Claim(CustomClaims.UserIdentifier, value));
140-
}
141-
return this;
132+
return AddClaim(CustomClaims.UserIdentifier, value);
142133
}
143134

144135
public JwtTokenBuilder AddCompanyId(string value)
145136
{
146-
if (value != null)
147-
{
148-
claims.Add(new Claim(CustomClaims.CompanyIdentifier, value));
149-
}
150-
return this;
137+
return AddClaim(CustomClaims.CompanyIdentifier, value);
151138
}
152139

153140
#endregion [Custom Claims]
@@ -162,7 +149,7 @@ public JwtTokenBuilder AddCompanyId(string value)
162149
/// <returns></returns>
163150
public JwtTokenBuilder AddClaim(string type, string value)
164151
{
165-
if (value != null)
152+
if (value != null && type != null)
166153
{
167154
claims.Add(new Claim(type, value));
168155
}

0 commit comments

Comments
 (0)