Ejemplo n.º 1
0
        public ITokenVerifier GetSignatureVerification(JsonWebTokenHeader header)
        {
            if (null == header)
            {
                return(new NullVerification(_consumerConfig));
            }
            switch (header.Algorithm)
            {
            case SigningAlgorithm.HS256:
            case SigningAlgorithm.HS384:
            case SigningAlgorithm.HS512:
                return(new HmacSigning(_keyProvider, header.Algorithm));

            case SigningAlgorithm.RS256:
            case SigningAlgorithm.RS384:
            case SigningAlgorithm.RS512:
                return(new RsaValidation(_rsaPublicKeyProvider));

            case SigningAlgorithm.ES256:
            case SigningAlgorithm.ES384:
            case SigningAlgorithm.ES512:
                return(new EccValidation(_eccPublicKeyProvider));
            }
            throw new NotImplementedException();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the JwtModel class.
 /// </summary>
 /// <param name="payload">The payload.</param>
 /// <param name="sign">The signature provider.</param>
 public JsonWebToken(T payload, ISignatureProvider sign)
 {
     Payload   = payload;
     signature = sign;
     header    = sign != null ? new JsonWebTokenHeader
     {
         AlgorithmName = sign.Name
     } : JsonWebTokenHeader.NoAlgorithm;
 }
Ejemplo n.º 3
0
        public ECDsaCng LoadRemoteKey(JsonWebTokenHeader header)
        {
            var dsa    = new ECDsaCng();
            var cached = _cache.GetPublicKeyBytes(header.KeyUri.ToString(), header.KeyId);

            if (null != cached)
            {
                dsa.FromXmlString(Encoding.UTF8.GetString(cached), ECKeyXmlFormat.Rfc4050);
                return(dsa);
            }

            string data;

            using (var wc = new WebClient())
            {
                try
                {
                    data = wc.DownloadString(header.KeyUri);
                }
                catch (WebException e)
                {
                    throw new RemoteKeyInaccessibleException("Unable to download the public key from URI " + header.KeyUri, e);
                }
            }

            switch (header.KeyFormat)
            {
            case KeyFormat.Rfc4050:

                dsa.FromXmlString(data, ECKeyXmlFormat.Rfc4050);
                _cache.Cache(Encoding.UTF8.GetBytes(data), header.KeyId, header.KeyUri.ToString());
                return(dsa);

            case KeyFormat.X509:
                var ms     = new MemoryStream(Encoding.ASCII.GetBytes(data));
                var reader = new CngBuilder(new PemReader(ms));
                dsa = new ECDsaCng(reader.Build());
                _cache.Cache(Encoding.UTF8.GetBytes(dsa.ToXmlString(ECKeyXmlFormat.Rfc4050)), header.KeyId, header.KeyUri.ToString());
                return(dsa);
            }
            throw new NotSupportedException("Can not open an ECC key with the keyformat " + header.KeyFormat);
        }
Ejemplo n.º 4
0
 public RSACng LoadRemoteKey(JsonWebTokenHeader header)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 5
0
 public RSACng LoadRemoteKey(JsonWebTokenHeader header)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 6
0
 public ECDsaCng LoadRemoteKey(JsonWebTokenHeader header)
 {
     return(_public);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Refreshs the cache.
 /// </summary>
 private void Refresh()
 {
     headerCache  = WebFormat.Base64UrlDecodeTo <JsonWebTokenHeader>(HeaderBase64Url) ?? JsonWebTokenHeader.NoAlgorithm;
     payloadCache = WebFormat.Base64UrlDecodeTo <T>(PayloadBase64Url);
 }