Пример #1
0
        public void JsonDataRoundTrip()
        {
            var date = DateTime.UtcNow;

            var claims = new PasetoInstance
            {
                Issuer           = "http://auth.example.com",
                Subject          = "2986689",
                Audience         = "audience",
                Expiration       = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind).AddMinutes(10),
                NotBefore        = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind).AddMinutes(-10),
                IssuedAt         = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind),
                AdditionalClaims = new Dictionary <string, object>
                {
                    ["roles"] = new[] { "Admin", "User" }
                },
                Footer = new Dictionary <string, object>
                {
                    ["kid"] = "dpm0"
                },
            };

            string token       = PasetoUtility.Sign(_publicKey, _privateKey, claims);
            var    parsedToken = PasetoUtility.Parse(_publicKey, token);

            Assert.Equal(claims.Issuer, parsedToken.Issuer);
            Assert.Equal(claims.Subject, parsedToken.Subject);
            Assert.Equal(claims.Audience, parsedToken.Audience);
            Assert.Equal(claims.Expiration, parsedToken.Expiration);
            Assert.Equal(claims.NotBefore, parsedToken.NotBefore);
            Assert.Equal(claims.IssuedAt, parsedToken.IssuedAt);
            Assert.Equal(claims.AdditionalClaims, parsedToken.AdditionalClaims);
            Assert.Equal(claims.Footer, parsedToken.Footer);
        }
Пример #2
0
        public void FutureTokenDoesNotParse()
        {
            var testClaims = new PasetoInstance
            {
                NotBefore = DateTime.UtcNow.AddMinutes(1),
                Subject   = "2986689",
            };

            Assert.NotNull(PasetoUtility.Parse(_publicKey, PasetoUtility.Sign(_publicKey, _privateKey, claims: testClaims), validateTimes: false));
            Assert.Null(PasetoUtility.Parse(_publicKey, PasetoUtility.Sign(_publicKey, _privateKey, claims: testClaims)));
            testClaims.NotBefore = DateTime.UtcNow.AddMinutes(-1);
            Assert.NotNull(PasetoUtility.Parse(_publicKey, PasetoUtility.Sign(_publicKey, _privateKey, claims: testClaims)));
        }
Пример #3
0
        public void FooterCanBeParsed()
        {
            string footerText = "Hello friend";

            Assert.Equal(footerText, PasetoUtility.ParseFooter(PasetoUtility.SignBytes(_publicKey, _privateKey, new byte[0], footerText)));

            var footerJson = new Dictionary <string, object> {
                ["hello"] = "friend"
            };

            Assert.Equal(footerJson, PasetoUtility.ParseFooterJson(PasetoUtility.Sign(_publicKey, _privateKey, new PasetoInstance {
                Footer = footerJson
            })));
        }
Пример #4
0
    protected string criarToken(string chavePublica, string chavePrivada)
    {
        byte[] _publicKey  = HexToBytes(chavePublica);
        byte[] _privateKey = HexToBytes(chavePrivada);

        var date = DateTime.UtcNow;

        var claims = new PasetoInstance {
            Issuer           = "http://auth.example.com",
            Subject          = "2986689",
            Audience         = "audience",
            Expiration       = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind).AddMinutes(10),
            NotBefore        = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind).AddMinutes(-10),
            IssuedAt         = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind),
            AdditionalClaims = new Dictionary <string, object> {
                ["roles"] = new[] { "Admin", "User" }
            },
            Footer = new Dictionary <string, object> {
                ["kid"] = "dpm0"
            },
        };

        return(PasetoUtility.Sign(_publicKey, _privateKey, claims));
    }
Пример #5
0
 public void EmptyTokenRoundTrip()
 {
     PasetoUtility.Parse(_publicKey, PasetoUtility.SignBytes(_publicKey, _privateKey, Encoding.UTF8.GetBytes("{}")));
     Assert.Equal("{}", Encoding.UTF8.GetString(PasetoUtility.ParseBytes(_publicKey, PasetoUtility.Sign(_publicKey, _privateKey, new PasetoInstance())).Payload));
 }