Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle RS512 situations when the modulus isn't base64 encoded. #29

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/Authress.SDK/Client/TokenVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ public async Task<VerifiedUserIdentity> VerifyToken(string jwtToken) {
return verifiedUserIdentity;
}

private byte[] ConvertFromBase64Url(string base64String) {
var result = base64String.Replace('_', '/').Replace('-', '+');
switch(result.Length % 4) {
case 2: result += "=="; break;
case 3: result += "="; break;
}

return Convert.FromBase64String(result);
}

private VerifiedUserIdentity VerifySignature(string jwtToken, Jwk key) {

var unverifiedJwtPayload = JsonConvert.DeserializeObject<Client.JWT.JwtPayload>(Base64UrlEncoder.Decode(jwtToken.Split('.')[1]));
Expand All @@ -184,20 +194,8 @@ private VerifiedUserIdentity VerifySignature(string jwtToken, Jwk key) {

var data = Encoding.UTF8.GetBytes($"{jwtToken.Split('.')[0]}.{jwtToken.Split('.')[1]}");

var keyAsString = key.x.Replace('_', '/').Replace('-', '+');
switch(keyAsString.Length % 4) {
case 2: keyAsString += "=="; break;
case 3: keyAsString += "="; break;
}

var jwtTokenSignature = jwtToken.Split('.')[2].Replace('_', '/').Replace('-', '+');
switch(jwtTokenSignature.Length % 4) {
case 2: jwtTokenSignature += "=="; break;
case 3: jwtTokenSignature += "="; break;
}

var edDsaPublicKey = NSec.Cryptography.PublicKey.Import(ed25519alg, Convert.FromBase64String("MCowBQYDK2VwAyEA" + keyAsString), KeyBlobFormat.PkixPublicKey);
var signatureData = Convert.FromBase64String(jwtTokenSignature);
var edDsaPublicKey = NSec.Cryptography.PublicKey.Import(ed25519alg, ConvertFromBase64Url("MCowBQYDK2VwAyEA" + key.x), KeyBlobFormat.PkixPublicKey);
var signatureData = ConvertFromBase64Url(jwtToken.Split('.')[2]);
if (!SignatureAlgorithm.Ed25519.Verify(edDsaPublicKey, data, signatureData)) {
throw new TokenVerificationException($"Unauthorized: Token Signature is not valid.");
}
Expand All @@ -211,8 +209,8 @@ private VerifiedUserIdentity VerifySignature(string jwtToken, Jwk key) {
var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(new RSAParameters()
{
Modulus = Convert.FromBase64String(key.n),
Exponent = Convert.FromBase64String(key.e)
Modulus = ConvertFromBase64Url(key.n),
Exponent = ConvertFromBase64Url(key.e)
});

var tokenHandler = new JwtSecurityTokenHandler();
Expand Down
Loading