示例#1
0
        static RestSharpServiceFactory()
        {
            JamaOptions           jamaOptions       = JamaOptionsFactory.Create();
            JsonSerializerOptions serializerOptions = JsonSerializerOptionsFactory.Create();

            Client = RestClientFactory.Create(jamaOptions, serializerOptions);
        }
示例#2
0
        public static void SerializeItemTypeImage()
        {
            JsonSerializerOptions options = JsonSerializerOptionsFactory.Create();
            string json = JsonSerializer.Serialize(ItemTypeImage.Book2, options);

            Assert.AreEqual("\"BOOK2\"", json);
        }
        public static RootOptions UseApiKey(this RootOptions options, string algorithmName, string subject, JwtPayload customPayload, out string token, out SecurityKey privateKey)
        {
            if (null == options.Authentication)
            {
                options.Authentication = new AuthenticationOptions();
            }

            if (null == options.Authentication.MonitorApiKey)
            {
                options.Authentication.MonitorApiKey = new MonitorApiKeyOptions();
            }

            SigningCredentials signingCreds;
            JsonWebKey         exportableJwk;

            switch (algorithmName)
            {
            case SecurityAlgorithms.EcdsaSha256:
            case SecurityAlgorithms.EcdsaSha256Signature:
            case SecurityAlgorithms.EcdsaSha384:
            case SecurityAlgorithms.EcdsaSha384Signature:
            case SecurityAlgorithms.EcdsaSha512:
            case SecurityAlgorithms.EcdsaSha512Signature:
                ECDsa            ecDsa    = ECDsa.Create(GetEcCurveFromName(algorithmName));
                ECDsaSecurityKey ecSecKey = new ECDsaSecurityKey(ecDsa);
                signingCreds = new SigningCredentials(ecSecKey, algorithmName);
                ECDsa            pubEcDsa    = ECDsa.Create(ecDsa.ExportParameters(false));
                ECDsaSecurityKey pubEcSecKey = new ECDsaSecurityKey(pubEcDsa);
                exportableJwk = JsonWebKeyConverter.ConvertFromECDsaSecurityKey(pubEcSecKey);
                privateKey    = ecSecKey;
                break;

            case SecurityAlgorithms.RsaSha256:
            case SecurityAlgorithms.RsaSha256Signature:
            case SecurityAlgorithms.RsaSha384:
            case SecurityAlgorithms.RsaSha384Signature:
            case SecurityAlgorithms.RsaSha512:
            case SecurityAlgorithms.RsaSha512Signature:
                RSA            rsa       = RSA.Create(GetRsaKeyLengthFromName(algorithmName));
                RsaSecurityKey rsaSecKey = new RsaSecurityKey(rsa);
                signingCreds = new SigningCredentials(rsaSecKey, algorithmName);
                RSA            pubRsa       = RSA.Create(rsa.ExportParameters(false));
                RsaSecurityKey pubRsaSecKey = new RsaSecurityKey(pubRsa);
                exportableJwk = JsonWebKeyConverter.ConvertFromRSASecurityKey(pubRsaSecKey);
                privateKey    = rsaSecKey;
                break;

            case SecurityAlgorithms.HmacSha256:
            case SecurityAlgorithms.HmacSha384:
            case SecurityAlgorithms.HmacSha512:
                HMAC hmac = HMAC.Create(GetHmacAlgorithmFromName(algorithmName));
                SymmetricSecurityKey hmacSecKey = new SymmetricSecurityKey(hmac.Key);
                signingCreds  = new SigningCredentials(hmacSecKey, algorithmName);
                exportableJwk = JsonWebKeyConverter.ConvertFromSymmetricSecurityKey(hmacSecKey);
                privateKey    = hmacSecKey;
                break;

            default:
                throw new ArgumentException($"Algorithm name '{algorithmName}' not supported", nameof(algorithmName));
            }

            JwtHeader               newHeader    = new JwtHeader(signingCreds, null, JwtConstants.HeaderType);
            JwtSecurityToken        newToken     = new JwtSecurityToken(newHeader, customPayload);
            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
            string resultToken = tokenHandler.WriteToken(newToken);

            JsonSerializerOptions serializerOptions = JsonSerializerOptionsFactory.Create(JsonSerializerOptionsFactory.JsonIgnoreCondition.WhenWritingNull);
            string publicKeyJson = JsonSerializer.Serialize(exportableJwk, serializerOptions);

            string publicKeyEncoded = Base64UrlEncoder.Encode(publicKeyJson);

            options.Authentication.MonitorApiKey.Subject   = subject;
            options.Authentication.MonitorApiKey.PublicKey = publicKeyEncoded;

            token = resultToken;

            return(options);
        }