-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57a4456
commit a1a97a6
Showing
6 changed files
with
200 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,138 +1,197 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Paseto.Builder; | ||
using Paseto.Cryptography.Key; | ||
using Xunit; | ||
|
||
namespace Paseto.Tests | ||
{ | ||
public sealed class PasetoValidationTest | ||
{ | ||
[Theory(DisplayName = "Should succeed on token with valid issuer")] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithValidIssuerValidationSucceeds(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateIssuer = true, | ||
ValidIssuer = "valid-issuer", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Issuer, "valid-issuer"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeTrue(); | ||
} | ||
|
||
[Theory(DisplayName = "Should fail on token with invalid issuer")] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithInValidIssuerValidationFails(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateIssuer = true, | ||
ValidIssuer = "valid-issuer", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Issuer, "invalid-issuer"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeFalse(); | ||
} | ||
|
||
[Theory(DisplayName = "Should succeed on token with valid subject")] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithValidSubjectValidationSucceeds(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateSubject = true, | ||
ValidSubject = "valid-subject", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Subject, "valid-subject"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeTrue(); | ||
} | ||
|
||
[Theory(DisplayName = "Should fail on token with invalid subject")] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithInValidSubjectValidationFails(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateSubject = true, | ||
ValidSubject = "valid-subject", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Subject, "invalid-subject"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeFalse(); | ||
} | ||
|
||
private static (string token, PasetoKey decodeKey) GenerateToken(ProtocolVersion version, Purpose purpose, string claimName, string claimValue) | ||
{ | ||
var builder = new PasetoBuilder().Use(version, purpose); | ||
switch (claimName) | ||
{ | ||
case PasetoRegisteredClaimNames.Issuer: | ||
builder.Issuer(claimValue); | ||
break; | ||
case PasetoRegisteredClaimNames.Subject: | ||
builder.Subject(claimValue); | ||
break; | ||
default: | ||
throw new NotImplementedException(); | ||
} | ||
switch (purpose) | ||
{ | ||
case Purpose.Local: | ||
{ | ||
var key = builder.GenerateSymmetricKey(); | ||
var token = builder | ||
.WithKey(key) | ||
.Encode(); | ||
return (token, key); | ||
} | ||
case Purpose.Public: | ||
{ | ||
var keyPair = builder.GenerateAsymmetricKeyPair(Enumerable.Repeat((byte)0x00, 32).ToArray()); | ||
var token = builder | ||
.WithKey(keyPair.SecretKey) | ||
.Encode(); | ||
return (token, keyPair.PublicKey); | ||
} | ||
default: | ||
throw new InvalidEnumArgumentException(); | ||
} | ||
} | ||
} | ||
namespace Paseto.Tests; | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Paseto.Builder; | ||
using Paseto.Cryptography.Key; | ||
using Xunit; | ||
|
||
public sealed class PasetoValidationTest | ||
{ | ||
[Theory(DisplayName = "Should succeed on token with valid audience")] | ||
[InlineData(ProtocolVersion.V1, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V1, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithValidAudienceValidationSucceeds(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateAudience = true, | ||
ValidAudience = "valid-audience", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Audience, "valid-audience"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeTrue(); | ||
} | ||
|
||
[Theory(DisplayName = "Should fail on token with invalid audience")] | ||
[InlineData(ProtocolVersion.V1, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V1, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithInValidAudienceValidationFails(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateAudience = true, | ||
ValidAudience = "valid-audience", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Audience, "invalid-audience"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeFalse(); | ||
} | ||
|
||
[Theory(DisplayName = "Should succeed on token with valid issuer")] | ||
[InlineData(ProtocolVersion.V1, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V1, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithValidIssuerValidationSucceeds(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateIssuer = true, | ||
ValidIssuer = "valid-issuer", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Issuer, "valid-issuer"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeTrue(); | ||
} | ||
|
||
[Theory(DisplayName = "Should fail on token with invalid issuer")] | ||
[InlineData(ProtocolVersion.V1, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V1, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithInValidIssuerValidationFails(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateIssuer = true, | ||
ValidIssuer = "valid-issuer", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Issuer, "invalid-issuer"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeFalse(); | ||
} | ||
|
||
[Theory(DisplayName = "Should succeed on token with valid subject")] | ||
[InlineData(ProtocolVersion.V1, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V1, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithValidSubjectValidationSucceeds(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateSubject = true, | ||
ValidSubject = "valid-subject", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Subject, "valid-subject"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeTrue(); | ||
} | ||
|
||
[Theory(DisplayName = "Should fail on token with invalid subject")] | ||
[InlineData(ProtocolVersion.V1, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V1, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V2, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V3, Purpose.Public)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Local)] | ||
[InlineData(ProtocolVersion.V4, Purpose.Public)] | ||
public void TokenWithInValidSubjectValidationFails(ProtocolVersion version, Purpose purpose) | ||
{ | ||
var validationParameters = new PasetoTokenValidationParameters() | ||
{ | ||
ValidateSubject = true, | ||
ValidSubject = "valid-subject", | ||
}; | ||
|
||
var (token, decodeKey) = GenerateToken(version, purpose, PasetoRegisteredClaimNames.Subject, "invalid-subject"); | ||
var decoded = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.WithKey(decodeKey) | ||
.Decode(token, validationParameters); | ||
|
||
decoded.IsValid.Should().BeFalse(); | ||
} | ||
|
||
private static (string token, PasetoKey decodeKey) GenerateToken(ProtocolVersion version, Purpose purpose, string claimName, string claimValue) | ||
{ | ||
var builder = new PasetoBuilder() | ||
.Use(version, purpose) | ||
.AddClaim(claimName, claimValue); | ||
|
||
switch (purpose) | ||
{ | ||
case Purpose.Local: | ||
{ | ||
var key = builder.GenerateSymmetricKey(); | ||
var token = builder | ||
.WithKey(key) | ||
.Encode(); | ||
return (token, key); | ||
} | ||
case Purpose.Public: | ||
{ | ||
var keyPair = builder.GenerateAsymmetricKeyPair(Enumerable.Repeat((byte)0x00, 32).ToArray()); | ||
var token = builder | ||
.WithKey(keyPair.SecretKey) | ||
.Encode(); | ||
return (token, keyPair.PublicKey); | ||
} | ||
default: | ||
throw new InvalidEnumArgumentException(); | ||
} | ||
} | ||
} |