| | 1 | | using Microsoft.IdentityModel.Tokens; |
| | 2 | |
|
| | 3 | | namespace Kestrun.Jwt; |
| | 4 | | /// <summary> |
| | 5 | | /// Specifies supported JWT signing algorithms. |
| | 6 | | /// </summary> |
| | 7 | | public enum JwtAlgorithm |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Automatically selects the algorithm based on the key length. |
| | 11 | | /// </summary> |
| | 12 | | Auto, |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// HMAC using SHA-256. |
| | 16 | | /// </summary> |
| | 17 | | HS256, |
| | 18 | | /// <summary> |
| | 19 | | /// HMAC using SHA-384. |
| | 20 | | /// </summary> |
| | 21 | | HS384, |
| | 22 | | /// <summary> |
| | 23 | | /// HMAC using SHA-512. |
| | 24 | | /// </summary> |
| | 25 | | HS512, |
| | 26 | | /// <summary> |
| | 27 | | /// RSA using SHA-256. |
| | 28 | | /// </summary> |
| | 29 | | RS256, |
| | 30 | | /// <summary> |
| | 31 | | /// RSA using SHA-384. |
| | 32 | | /// </summary> |
| | 33 | | RS384, |
| | 34 | | /// <summary> |
| | 35 | | /// RSA using SHA-512. |
| | 36 | | /// </summary> |
| | 37 | | RS512, |
| | 38 | | /// <summary> |
| | 39 | | /// RSASSA-PSS using SHA-256. |
| | 40 | | /// </summary> |
| | 41 | | PS256, |
| | 42 | | /// <summary> |
| | 43 | | /// RSASSA-PSS using SHA-384. |
| | 44 | | /// </summary> |
| | 45 | | PS384, |
| | 46 | | /// <summary> |
| | 47 | | /// RSASSA-PSS using SHA-512. |
| | 48 | | /// </summary> |
| | 49 | | PS512, |
| | 50 | | /// <summary> |
| | 51 | | /// ECDSA using P-256 and SHA-256. |
| | 52 | | /// </summary> |
| | 53 | | ES256, |
| | 54 | | /// <summary> |
| | 55 | | /// ECDSA using P-384 and SHA-384. |
| | 56 | | /// </summary> |
| | 57 | | ES384, |
| | 58 | | /// <summary> |
| | 59 | | /// ECDSA using P-521 and SHA-512. |
| | 60 | | /// </summary> |
| | 61 | | ES512 |
| | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Provides extension methods for the JwtAlgorithm enum. |
| | 66 | | /// </summary> |
| | 67 | | public static class JwtAlgorithmExtensions |
| | 68 | | { |
| | 69 | | /// <summary> |
| | 70 | | /// Converts the specified <see cref="JwtAlgorithm"/> to its corresponding JWT algorithm string. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="alg">The JWT algorithm to convert.</param> |
| | 73 | | /// <param name="keyByteLength">The key length in bytes, used only when <see cref="JwtAlgorithm.Auto"/> is specified |
| | 74 | | /// <returns>The JWT algorithm string representation.</returns> |
| | 75 | | public static string ToJwtString(this JwtAlgorithm alg, int keyByteLength = 0) |
| | 76 | | { |
| | 77 | | // handle the “Auto” case only for HMAC |
| 38 | 78 | | return alg == JwtAlgorithm.Auto |
| 38 | 79 | | ? keyByteLength switch |
| 38 | 80 | | { |
| 5 | 81 | | >= 64 => SecurityAlgorithms.HmacSha512, |
| 4 | 82 | | >= 48 => SecurityAlgorithms.HmacSha384, |
| 16 | 83 | | _ => SecurityAlgorithms.HmacSha256 |
| 38 | 84 | | } |
| 38 | 85 | | : alg switch |
| 38 | 86 | | { |
| 1 | 87 | | JwtAlgorithm.HS256 => SecurityAlgorithms.HmacSha256, |
| 1 | 88 | | JwtAlgorithm.HS384 => SecurityAlgorithms.HmacSha384, |
| 1 | 89 | | JwtAlgorithm.HS512 => SecurityAlgorithms.HmacSha512, |
| 38 | 90 | |
|
| 1 | 91 | | JwtAlgorithm.RS256 => SecurityAlgorithms.RsaSha256, |
| 1 | 92 | | JwtAlgorithm.RS384 => SecurityAlgorithms.RsaSha384, |
| 1 | 93 | | JwtAlgorithm.RS512 => SecurityAlgorithms.RsaSha512, |
| 38 | 94 | |
|
| 1 | 95 | | JwtAlgorithm.PS256 => SecurityAlgorithms.RsaSsaPssSha256, |
| 1 | 96 | | JwtAlgorithm.PS384 => SecurityAlgorithms.RsaSsaPssSha384, |
| 1 | 97 | | JwtAlgorithm.PS512 => SecurityAlgorithms.RsaSsaPssSha512, |
| 38 | 98 | |
|
| 1 | 99 | | JwtAlgorithm.ES256 => SecurityAlgorithms.EcdsaSha256, |
| 1 | 100 | | JwtAlgorithm.ES384 => SecurityAlgorithms.EcdsaSha384, |
| 1 | 101 | | JwtAlgorithm.ES512 => SecurityAlgorithms.EcdsaSha512, |
| 38 | 102 | |
|
| 1 | 103 | | _ => throw new ArgumentOutOfRangeException(nameof(alg), alg, null) |
| 38 | 104 | | }; |
| | 105 | | } |
| | 106 | | } |