示例#1
0
        protected override IJwtAlgorithm Create(JwtAlgorithmName algorithm)
        {
#if NETSTANDARD2_0 || NET5_0
            switch (algorithm)
            {
            case JwtAlgorithmName.ES256:
                return(CreateES256Algorithm());

            case JwtAlgorithmName.ES384:
                return(CreateES384Algorithm());

            case JwtAlgorithmName.ES512:
                return(CreateES512Algorithm());

            case JwtAlgorithmName.HS256:
            case JwtAlgorithmName.HS384:
            case JwtAlgorithmName.HS512:
                throw new NotSupportedException($"For algorithm {algorithm} please use an instance of {nameof(HMACSHAAlgorithmFactory)}");

            case JwtAlgorithmName.RS256:
            case JwtAlgorithmName.RS384:
            case JwtAlgorithmName.RS512:
                throw new NotSupportedException($"For algorithm {algorithm} please use an instance of {nameof(RSAlgorithmFactory)}");

            default:
                throw new NotSupportedException($"For algorithm {Enum.GetName(typeof(JwtAlgorithmName), algorithm)} please use the appropriate factory by implementing {nameof(IAlgorithmFactory)}");
            }
#else
            throw new NotImplementedException("ECDSA algorithms are only supported when targeting .NET Standard 2.0");
#endif
        }
示例#2
0
        protected virtual IJwtAlgorithm Create(JwtAlgorithmName algorithm)
        {
            switch (algorithm)
            {
            case JwtAlgorithmName.HS256:
                return(new HMACSHA256Algorithm());

            case JwtAlgorithmName.HS384:
                return(new HMACSHA384Algorithm());

            case JwtAlgorithmName.HS512:
                return(new HMACSHA512Algorithm());

            case JwtAlgorithmName.RS256:
            case JwtAlgorithmName.RS384:
            case JwtAlgorithmName.RS512:
                throw new NotSupportedException($"For algorithm {algorithm} please use an instance of {nameof(RSAlgorithmFactory)}");

            case JwtAlgorithmName.ES256:
            case JwtAlgorithmName.ES384:
            case JwtAlgorithmName.ES512:
                throw new NotSupportedException($"For algorithm {algorithm} please use an instance of {nameof(ECDSAAlgorithmFactory)}");

            default:
                throw new NotSupportedException($"Algorithm {algorithm} is not supported.");
            }
        }
示例#3
0
        protected override IJwtAlgorithm Create(JwtAlgorithmName algorithm)
        {
            switch (algorithm)
            {
            case JwtAlgorithmName.RS256:
                return(CreateRS256Algorithm());

            case JwtAlgorithmName.RS384:
                return(CreateRS384Algorithm());

            case JwtAlgorithmName.RS512:
                return(CreateRS512Algorithm());

            case JwtAlgorithmName.HS256:
            case JwtAlgorithmName.HS384:
            case JwtAlgorithmName.HS512:
                throw new NotSupportedException($"For algorithm {algorithm} please use an instance of {nameof(HMACSHAAlgorithmFactory)}");

            case JwtAlgorithmName.ES256:
            case JwtAlgorithmName.ES384:
            case JwtAlgorithmName.ES512:
                throw new NotSupportedException($"For algorithm {algorithm} please use an instance of {nameof(ECDSAAlgorithmFactory)}");

            default:
                throw new NotSupportedException($"For algorithm {Enum.GetName(typeof(JwtAlgorithmName), algorithm)} please use the appropriate factory by implementing {nameof(IAlgorithmFactory)}");
            }
        }
示例#4
0
        public static string JwtEncode(JwtAlgorithmName algorithmName, string secret, IDictionary <string, object> extraHeaders, IDictionary <string, object> payload)
        {
            IJwtAlgorithm algorithm = algorithmName switch
            {
                JwtAlgorithmName.HS256 => new HMACSHA256Algorithm(),
                JwtAlgorithmName.HS384 => new HMACSHA384Algorithm(),
                JwtAlgorithmName.HS512 => new HMACSHA512Algorithm(),
                _ => throw new NotSupportedException("This algorith is not supported at the moment")
            };

            var jsonSerializer = new JsonNetSerializer();
            var urlEncoder     = new JwtBase64UrlEncoder();
            var jwtEncoder     = new JwtEncoder(algorithm, jsonSerializer, urlEncoder);

            return(jwtEncoder.Encode(extraHeaders, payload, secret));
        }

        #endregion
    }
示例#5
0
        protected virtual IJwtAlgorithm Create(JwtAlgorithmName algorithm)
        {
            switch (algorithm)
            {
            case JwtAlgorithmName.HS256:
                return(new HMACSHA256Algorithm());

            case JwtAlgorithmName.HS384:
                return(new HMACSHA384Algorithm());

            case JwtAlgorithmName.HS512:
                return(new HMACSHA512Algorithm());

            case JwtAlgorithmName.RS256:
                throw new NotSupportedException($"For algorithm {nameof(JwtAlgorithmName.RS256)} please create custom factory by implementing {nameof(IAlgorithmFactory)}");

            default:
                throw new NotSupportedException($"Algorithm {algorithm} is not supported.");
            }
        }
示例#6
0
 protected abstract IJwtAlgorithm Create(JwtAlgorithmName algorithm);