Esempio n. 1
0
        public static JsonKeyValuePairs MergeKeyValuePairs(JsonKeyValuePairs set1, JsonKeyValuePairs set2)
        {
            var result = new JsonKeyValuePairs();

            foreach (var key in set1.Keys)
            {
                result.Add(key, set1[key]);
            }

            foreach (var key in set2.Keys)
            {
                if (set1.ContainsKey(key))
                {
                    if (set1[key].IsA <JsonKeyValuePairs>() && set2[key].IsA <JsonKeyValuePairs>())
                    {
                        var subset1 = (JsonKeyValuePairs)set1[key];
                        var subset2 = (JsonKeyValuePairs)set2[key];
                        result[key] = MergeKeyValuePairs(subset1, subset2);
                    }
                    else
                    {
                        // duplicate
                    }
                }
                else
                {
                    result.Add(key, set2[key]);
                }
            }

            return(result);
        }
Esempio n. 2
0
        public HttpJsonContent(JsonKeyValuePairs json) : base()
        {
            var body = json.Stringify();

            this.content     = Encoding.UTF8.GetBytes(body);
            this.ContentType = "application/json";
        }
Esempio n. 3
0
        public static string Stringify(this JsonKeyValuePairs kvp)
        {
            var builder = new JsonBuilder();

            builder.Render(kvp);
            return(builder.ToString());
        }
Esempio n. 4
0
        public Jwt(JsonKeyValuePairs payload, string secret)
        {
            var header = CreateJwtHeader();

            var jwt = Base64UrlEncode(header.Stringify()) + "." + Base64UrlEncode(payload.Stringify());

            jwt += "." + Sign(jwt, secret);

            Token = jwt;
        }
Esempio n. 5
0
 public void AddRange(JsonKeyValuePairs kvp)
 {
     foreach (string key in kvp.Keys)
     {
         if (!base.ContainsKey(key))
         {
             base.Add(key, kvp[key]);
         }
     }
 }
Esempio n. 6
0
        private JsonKeyValuePairs CreateJwtHeader()
        {
            const string Algorithm = "HS256";

            var header = new JsonKeyValuePairs
            {
                { "alg", Algorithm },
                { "typ", "JWT" }
            };

            return(header);
        }
Esempio n. 7
0
        public HttpExceptionContent(Exception ex) : base()
        {
            var inner = ExceptionHelper.GetInner(ex);
            var json  = new JsonKeyValuePairs
            {
                { "message", inner.Message },
                { "stackTrace", ExceptionHelper.GetStackTrace(inner) }
            };

            var body = json.Stringify();

            this.content     = Encoding.UTF8.GetBytes(body);
            this.ContentType = "application/json";
        }
Esempio n. 8
0
        public static string GetOAuthToken(string clientID, string clientSecret, string oauthURI)
        {
            var identity       = clientID + ":" + clientSecret;
            var uri            = oauthURI + "/access_token?grant_type=client_credentials&scope=read+write";
            var client         = new WebClientEx();
            var textAsBytes    = Encoding.UTF8.GetBytes(identity);
            var identityBase64 = Convert.ToBase64String(textAsBytes);

            client.Headers["Authorization"] = "Basic " + identityBase64;
            client.Headers["Content-Type"]  = "application/x-www-form-urlencoded";

            var response     = client.Get(uri);
            var jsonResponse = new JsonKeyValuePairs(response);

            return((string)jsonResponse["access_token"]);
        }
Esempio n. 9
0
        private static JsonKeyValuePairs FlattenJson(JsonKeyValuePairs json, string rootKey)
        {
            var flat = new JsonKeyValuePairs();

            foreach (var key in json.Keys)
            {
                var value  = json[key];
                var newKey = rootKey + (string.IsNullOrEmpty(rootKey) ? "" : ".") + key;
                if (value.IsA <JsonKeyValuePairs>())
                {
                    var flattened = FlattenJson((JsonKeyValuePairs)value, newKey);
                    flat = JsonHelper.MergeKeyValuePairs(flat, flattened);
                }
                else
                {
                    flat.Add(newKey, value);
                }
            }

            return(flat);
        }
Esempio n. 10
0
        public JsonBuilder Append(JsonKeyValuePairs kvp)
        {
            builder.Append("{");

            var i = 0;

            foreach (var item in kvp)
            {
                if (i++ > 0)
                {
                    builder.Append(",");
                }

                this.Append(item.Key);
                builder.Append(":");
                this.Render(item.Value);
            }

            builder.Append("}");
            return(this);
        }
Esempio n. 11
0
        public Jwt(string token, string secret)
        {
            Token = token;
            var segments = token.Split('.');

            if (segments.Length != 3)
            {
                throw new Exception("Token structure is incorrect!");
            }

            var header  = new JsonKeyValuePairs(Encoding.UTF8.GetString(Base64UrlDecode(segments[0])));
            var payload = new JsonKeyValuePairs(Encoding.UTF8.GetString(Base64UrlDecode(segments[1])));

            var rawSignature = segments[0] + '.' + segments[1];

            if (!Verify(rawSignature, secret, segments[2]))
            {
                throw new JwtValidationFailedException();
            }

            Payload = payload;
        }
Esempio n. 12
0
 public FetchResponse <T> Post(string url, JsonKeyValuePairs json, string authorization)
 {
     return(Fetch(url, "POST", json.Stringify(), authorization, "application/json"));
 }
Esempio n. 13
0
 public static JsonKeyValuePairs FlattenJson(JsonKeyValuePairs json)
 {
     return(FlattenJson(json, ""));
 }