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); }
public HttpJsonContent(JsonKeyValuePairs json) : base() { var body = json.Stringify(); this.content = Encoding.UTF8.GetBytes(body); this.ContentType = "application/json"; }
public static string Stringify(this JsonKeyValuePairs kvp) { var builder = new JsonBuilder(); builder.Render(kvp); return(builder.ToString()); }
public Jwt(JsonKeyValuePairs payload, string secret) { var header = CreateJwtHeader(); var jwt = Base64UrlEncode(header.Stringify()) + "." + Base64UrlEncode(payload.Stringify()); jwt += "." + Sign(jwt, secret); Token = jwt; }
public void AddRange(JsonKeyValuePairs kvp) { foreach (string key in kvp.Keys) { if (!base.ContainsKey(key)) { base.Add(key, kvp[key]); } } }
private JsonKeyValuePairs CreateJwtHeader() { const string Algorithm = "HS256"; var header = new JsonKeyValuePairs { { "alg", Algorithm }, { "typ", "JWT" } }; return(header); }
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"; }
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"]); }
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); }
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); }
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; }
public FetchResponse <T> Post(string url, JsonKeyValuePairs json, string authorization) { return(Fetch(url, "POST", json.Stringify(), authorization, "application/json")); }
public static JsonKeyValuePairs FlattenJson(JsonKeyValuePairs json) { return(FlattenJson(json, "")); }