Skip to content

Commit 661e7a6

Browse files
committed
Enhance JwtHeader class
1 parent 3565699 commit 661e7a6

File tree

7 files changed

+142
-21
lines changed

7 files changed

+142
-21
lines changed

JwtManager/Helpers/Algorithm.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace JwtManager.Helpers
77
public enum Algorithm
88
{
99
HMAC = 1,
10-
RSA = 2
10+
RSA = 2,
11+
ECDSA = 3,
12+
RSASSA = 4
1113
}
1214
}

JwtManager/Helpers/KeySize.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace JwtManager.Helpers
6+
{
7+
public enum KeySize
8+
{
9+
S256 = 256,
10+
S384 = 384,
11+
S512 = 512
12+
}
13+
}

JwtManager/Jwt.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ namespace JwtManager
66
{
77
public abstract class Jwt
88
{
9-
public string Algorithm { get; set; }
10-
public string Digest { get; set; }
11-
129
/// <summary>
1310
/// Creates and signs a Jwt token for the given payload with the given private key
1411
/// </summary>
@@ -22,10 +19,5 @@ public abstract class Jwt
2219
/// <param name="token">A token to validate</param>
2320
/// <returns>The serialized payload if the token can be validated</returns>
2421
public abstract string Validate(string token);
25-
26-
public static Jwt Create(string algorithm, int digest)
27-
{
28-
return new RsJwt();
29-
}
3022
}
3123
}

JwtManager/JwtHeader.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,42 @@
44

55
namespace JwtManager
66
{
7-
class JwtHeader
7+
public class JwtHeader
88
{
99
public string alg { get; set; }
1010
public string typ { get; set; }
11+
12+
public void Set(Helpers.Algorithm algorithm, Helpers.KeySize size)
13+
{
14+
if(!Enum.IsDefined(typeof(Helpers.Algorithm), algorithm) || !Enum.IsDefined(typeof(Helpers.KeySize), size))
15+
{
16+
throw new Exception("Invalid values for algorithm or size.");
17+
}
18+
19+
string algstr = "";
20+
21+
switch(algorithm)
22+
{
23+
case Helpers.Algorithm.RSA:
24+
algstr = "RS";
25+
break;
26+
case Helpers.Algorithm.HMAC:
27+
algstr = "HS";
28+
break;
29+
case Helpers.Algorithm.ECDSA:
30+
algstr = "ES";
31+
break;
32+
case Helpers.Algorithm.RSASSA:
33+
if(size == Helpers.KeySize.S512)
34+
{
35+
throw new Exception("Invalid size for algorithm.");
36+
}
37+
algstr = "PS";
38+
break;
39+
}
40+
41+
alg = algstr + ((int)size).ToString();
42+
typ = "JWT";
43+
}
1144
}
1245
}

JwtManager/RsJwt.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class RsJwt : Jwt
1515
{
1616
public string PrivateKey { get; set; }
1717
public string PublicKey { get; set; }
18-
public int KeySize { get; set; }
18+
public Helpers.KeySize KeySize { get; set; }
1919

2020
public override string Sign(string payload)
2121
{
@@ -92,7 +92,9 @@ private JwtHeader Header
9292
{
9393
get
9494
{
95-
return new JwtHeader { alg = "RS" + KeySize.ToString(), typ = "JWT" };
95+
JwtHeader header = new JwtHeader();
96+
header.Set(Helpers.Algorithm.RSA, KeySize);
97+
return Header;
9698
}
9799
}
98100

JwtManagerTests/JwtHeaderTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace JwtManagerTests
7+
{
8+
[TestClass]
9+
public class JwtHeaderTests
10+
{
11+
class TestResult
12+
{
13+
public JwtManager.Helpers.Algorithm algorithm { get; set; }
14+
public JwtManager.Helpers.KeySize size { get; set; }
15+
public string alg { get; set; }
16+
public string typ { get; set; }
17+
}
18+
19+
[TestMethod]
20+
public void ValidCases()
21+
{
22+
JwtManager.JwtHeader header = new JwtManager.JwtHeader();
23+
24+
TestResult[] tests = {
25+
new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S256, alg = "RS256", typ = "JWT" },
26+
new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S384, alg = "RS384", typ = "JWT" },
27+
new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S512, alg = "RS512", typ = "JWT" },
28+
new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S256, alg = "HS256", typ = "JWT" },
29+
new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S384, alg = "HS384", typ = "JWT" },
30+
new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S512, alg = "HS512", typ = "JWT" }
31+
};
32+
33+
foreach(TestResult test in tests)
34+
{
35+
header.Set(test.algorithm, test.size);
36+
Assert.AreEqual(header.alg, test.alg);
37+
Assert.AreEqual(header.typ, test.typ);
38+
}
39+
}
40+
41+
[TestMethod]
42+
public void CheckEP512Exception()
43+
{
44+
JwtManager.JwtHeader header = new JwtManager.JwtHeader();
45+
Exception e = null;
46+
47+
try
48+
{
49+
header.Set(JwtManager.Helpers.Algorithm.RSASSA, JwtManager.Helpers.KeySize.S512);
50+
}
51+
catch(Exception ex)
52+
{
53+
e = ex;
54+
}
55+
56+
Assert.IsNotNull(e);
57+
Assert.AreEqual(e.Message, "Invalid size for algorithm.");
58+
}
59+
60+
[TestMethod]
61+
public void CheckNonValidOptions()
62+
{
63+
JwtManager.JwtHeader header = new JwtManager.JwtHeader();
64+
Exception e = null;
65+
66+
try
67+
{
68+
header.Set(JwtManager.Helpers.Algorithm.RSASSA, (JwtManager.Helpers.KeySize)555);
69+
}
70+
catch (Exception ex)
71+
{
72+
e = ex;
73+
}
74+
75+
Assert.IsNotNull(e);
76+
Assert.AreEqual(e.Message, "Invalid values for algorithm or size.");
77+
}
78+
}
79+
}

JwtManagerTests/RsJwtTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void SignDataWithInvalidHashAlgorithm()
244244
Exception e = null;
245245
JwtManager.RsJwt jwt = new JwtManager.RsJwt
246246
{
247-
KeySize = 555,
247+
KeySize = (JwtManager.Helpers.KeySize)555,
248248
PrivateKey = PrivateKey
249249
};
250250

@@ -274,7 +274,7 @@ public void ValidateWithInvalidHashAlgorithm()
274274
};
275275
JwtManager.RsJwt vJwt = new JwtManager.RsJwt
276276
{
277-
KeySize = 555,
277+
KeySize = (JwtManager.Helpers.KeySize)555,
278278
PublicKey = PublicKey
279279
};
280280

@@ -294,7 +294,7 @@ public void ValidateWithInvalidHashAlgorithm()
294294
Assert.AreEqual(e.Message, "Given key size is not valid.");
295295
}
296296

297-
protected abstract int HashKeySize();
297+
protected abstract JwtManager.Helpers.KeySize HashKeySize();
298298
}
299299

300300
[TestClass]
@@ -326,9 +326,9 @@ public static void ClassCleanup()
326326
}
327327
#endregion
328328

329-
protected override int HashKeySize()
329+
protected override JwtManager.Helpers.KeySize HashKeySize()
330330
{
331-
return 256;
331+
return JwtManager.Helpers.KeySize.S256;
332332
}
333333
}
334334

@@ -361,9 +361,9 @@ public static void ClassCleanup()
361361
}
362362
#endregion
363363

364-
protected override int HashKeySize()
364+
protected override JwtManager.Helpers.KeySize HashKeySize()
365365
{
366-
return 384;
366+
return JwtManager.Helpers.KeySize.S384;
367367
}
368368
}
369369

@@ -396,9 +396,9 @@ public static void ClassCleanup()
396396
}
397397
#endregion
398398

399-
protected override int HashKeySize()
399+
protected override JwtManager.Helpers.KeySize HashKeySize()
400400
{
401-
return 512;
401+
return JwtManager.Helpers.KeySize.S512;
402402
}
403403
}
404404
}

0 commit comments

Comments
 (0)