-
-
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.
* correct mismatched enum * Add issuer validation tests Co-authored-by: 조태혁 <[email protected]>
- Loading branch information
Showing
2 changed files
with
85 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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, "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, "invalid-issuer"); | ||
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 issuer) | ||
{ | ||
var builder = new PasetoBuilder().Use(version, purpose); | ||
switch (purpose) | ||
{ | ||
case Purpose.Local: | ||
{ | ||
var key = builder.GenerateSymmetricKey(); | ||
var token = builder | ||
.WithKey(key) | ||
.Issuer(issuer) | ||
.Encode(); | ||
return (token, key); | ||
} | ||
case Purpose.Public: | ||
{ | ||
var keyPair = builder.GenerateAsymmetricKeyPair(Enumerable.Repeat((byte)0x00, 32).ToArray()); | ||
var token = builder | ||
.WithKey(keyPair.SecretKey) | ||
.Issuer(issuer) | ||
.Encode(); | ||
return (token, keyPair.PublicKey); | ||
} | ||
default: | ||
throw new InvalidEnumArgumentException(); | ||
} | ||
} | ||
} | ||
} |