Skip to content

Commit

Permalink
Refactored related to GetTokens and version
Browse files Browse the repository at this point in the history
  • Loading branch information
budgetpreneur committed Dec 18, 2019
1 parent ce67489 commit c616ba7
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 176 deletions.
10 changes: 10 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
exclude_paths:
- '**/Examples/**'
- '**/UnitTests/**'
- '**/Editor/**'
- '**/PlayModeTests/**'
- '**/netstandard2.0/**'
- '**/Plugins/**'
- '**/Scripts/PubnubExample.*'

15 changes: 11 additions & 4 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
name: c-sharp
version: "4.3.0.0"
version: "4.4.0.0"
schema: 1
scm: github.com/pubnub/c-sharp
changelog:
- version: 4.4.0.0
date: December 19, 2019
changes:
- type: Improvement
text: Refactored code related to TMS GetToken and GetTokens
- type: Improvement
text: Refactored code to retrieve assembly version
- version: 4.3.0.0
date: December 12, 2019
changes:
Expand Down Expand Up @@ -458,7 +465,7 @@ features:
- QUERY-PARAM
supported-platforms:
-
version: Pubnub 'C#' 4.3.0.0
version: Pubnub 'C#' 4.4.0.0
platforms:
- Windows 10 and up
- Windows Server 2008 and up
Expand All @@ -468,7 +475,7 @@ supported-platforms:
- .Net Framework 4.5
- .Net Framework 4.6.1+
-
version: PubnubPCL 'C#' 4.3.0.0
version: PubnubPCL 'C#' 4.4.0.0
platforms:
- Xamarin.Android
- Xamarin.iOS
Expand All @@ -486,7 +493,7 @@ supported-platforms:
- .Net Standard 2.0
- .Net Core
-
version: PubnubUWP 'C#' 4.3.0.0
version: PubnubUWP 'C#' 4.4.0.0
platforms:
- Windows Phone 10
- Universal Windows Apps
Expand Down
13 changes: 0 additions & 13 deletions src/Api/PubnubApi/Builder/UriUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public static string EncodeUriComponent(string s, PNOperationType type, bool ign

return encodedUri;
}

private static bool IsOperationTypeForPercent2fEncode(PNOperationType type)
{
bool ret;
Expand Down Expand Up @@ -138,16 +137,6 @@ private static bool IsUnsafeToEncode(char ch, bool ignoreComma, bool ignoreColon
return " ~`!@#$%^&*()+=[]\\{}|;':\",/<>?".IndexOf(ch) >= 0;
}
}

private static bool IsUnsafeToEscapeForPamSign(char ch)
{
#if NET35 || NET40
return "*()':!~".ToLowerInvariant().IndexOf(ch) >= 0;
#else
return "*()':!".ToLowerInvariant().IndexOf(ch) >= 0;
#endif
}

private static char ToHex(int ch)
{
return (char)(ch < 10 ? '0' + ch : 'A' + ch - 10);
Expand Down Expand Up @@ -207,7 +196,5 @@ private static int ConvertToUtf32(String s, int index)
// Not a high-surrogate or low-surrogate. Genereate the UTF32 value for the BMP characters.
return (int)s[index];
}


}
}
98 changes: 44 additions & 54 deletions src/Api/PubnubApi/EndPoint/TokenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
using System.Text;
using System.Text.RegularExpressions;
using PubnubApi.CBOR;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Collections;
using System.Reflection;
using Newtonsoft.Json.Linq;

#if DEBUG && NET461
#endif
using Newtonsoft.Json;

namespace PubnubApi.EndPoint
{
Expand All @@ -20,13 +18,12 @@ public class TokenManager : IDisposable
private readonly IJsonPluggableLibrary jsonLib;
private readonly IPubnubLog pubnubLog;
private readonly string pubnubInstanceId;
private static ConcurrentDictionary<string, ConcurrentDictionary<TokenKey, string>> dicToken
private static ConcurrentDictionary<string, ConcurrentDictionary<PNTokenKey, string>> dicToken
{
get;
set;
} = new ConcurrentDictionary<string, ConcurrentDictionary<TokenKey, string>>();
} = new ConcurrentDictionary<string, ConcurrentDictionary<PNTokenKey, string>>();

#if DEBUG && NET461
internal class TokenManagerConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
Expand All @@ -37,8 +34,13 @@ public override bool CanConvert(Type objectType)

private static bool TypeImplementsGenericInterface(Type concreteType, Type interfaceType)
{
#if NET35 || NET40
return concreteType.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType);
#else
return concreteType.GetInterfaces()
.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == interfaceType);
#endif
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand Down Expand Up @@ -66,31 +68,6 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
throw new NotImplementedException();
}
}
#endif

internal class TokenKey
{
[JsonProperty("ResourceType ")]
public string ResourceType { get; set; }

[JsonProperty("ResourceId ")]
public string ResourceId { get; set; }

[JsonProperty("PatternFlag ")]
public int PatternFlag { get; set; }

public override bool Equals(object obj)
{
TokenKey currentKey = obj as TokenKey;
if (currentKey == null)
{
return false;
}
return currentKey.ResourceType == this.ResourceType && currentKey.ResourceId == this.ResourceId && currentKey.PatternFlag == this.PatternFlag;
}

public override int GetHashCode() => ResourceType.GetHashCode() ^ PatternFlag.GetHashCode() ^ ResourceId.GetHashCode();
}

public TokenManager(PNConfiguration config, IJsonPluggableLibrary jsonPluggableLibrary, IPubnubLog log, string instanceId)
{
Expand All @@ -100,7 +77,7 @@ public TokenManager(PNConfiguration config, IJsonPluggableLibrary jsonPluggableL
this.pubnubInstanceId = instanceId;
if (!dicToken.ContainsKey(instanceId))
{
dicToken.GetOrAdd(instanceId, new ConcurrentDictionary<TokenKey, string>());
dicToken.GetOrAdd(instanceId, new ConcurrentDictionary<PNTokenKey, string>());
}

}
Expand Down Expand Up @@ -331,76 +308,76 @@ public void SetToken(string token)
PNGrantToken tokenObj = ParseToken(token);
if (tokenObj != null)
{
#region "Non-Pattern Resources"
#region "Non-Pattern Resources"
if (tokenObj.Channels != null && tokenObj.Channels.Count > 0)
{
foreach(KeyValuePair<string, PNResourcePermission> kvp in tokenObj.Channels)
{
TokenKey key = new TokenKey { ResourceType = "channel", ResourceId = kvp.Key, PatternFlag = 0 };
PNTokenKey key = new PNTokenKey { ResourceType = "channel", ResourceId = kvp.Key, PatternFlag = 0 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
if (tokenObj.ChannelGroups != null && tokenObj.ChannelGroups.Count > 0)
{
foreach (KeyValuePair<string, PNResourcePermission> kvp in tokenObj.ChannelGroups)
{
TokenKey key = new TokenKey { ResourceType = "group", ResourceId = kvp.Key, PatternFlag = 0 };
PNTokenKey key = new PNTokenKey { ResourceType = "group", ResourceId = kvp.Key, PatternFlag = 0 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
if (tokenObj.Users != null && tokenObj.Users.Count > 0)
{
foreach (KeyValuePair<string, PNResourcePermission> kvp in tokenObj.Users)
{
TokenKey key = new TokenKey { ResourceType = "user", ResourceId = kvp.Key, PatternFlag = 0 };
PNTokenKey key = new PNTokenKey { ResourceType = "user", ResourceId = kvp.Key, PatternFlag = 0 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
if (tokenObj.Spaces != null && tokenObj.Spaces.Count > 0)
{
foreach (KeyValuePair<string, PNResourcePermission> kvp in tokenObj.Spaces)
{
TokenKey key = new TokenKey { ResourceType = "space", ResourceId = kvp.Key, PatternFlag = 0 };
PNTokenKey key = new PNTokenKey { ResourceType = "space", ResourceId = kvp.Key, PatternFlag = 0 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
#endregion
#region "Pattern Resources"
#endregion
#region "Pattern Resources"
if (tokenObj.ChannelPatterns != null && tokenObj.ChannelPatterns.Count > 0)
{
foreach (KeyValuePair<string, PNResourcePermission> kvp in tokenObj.ChannelPatterns)
{
TokenKey key = new TokenKey { ResourceType = "channel", ResourceId = kvp.Key, PatternFlag = 1 };
PNTokenKey key = new PNTokenKey { ResourceType = "channel", ResourceId = kvp.Key, PatternFlag = 1 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
if (tokenObj.GroupPatterns != null && tokenObj.GroupPatterns.Count > 0)
{
foreach (KeyValuePair<string, PNResourcePermission> kvp in tokenObj.GroupPatterns)
{
TokenKey key = new TokenKey { ResourceType = "group", ResourceId = kvp.Key, PatternFlag = 1 };
PNTokenKey key = new PNTokenKey { ResourceType = "group", ResourceId = kvp.Key, PatternFlag = 1 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
if (tokenObj.UserPatterns != null && tokenObj.UserPatterns.Count > 0)
{
foreach (KeyValuePair<string, PNResourcePermission> kvp in tokenObj.UserPatterns)
{
TokenKey key = new TokenKey { ResourceType = "user", ResourceId = kvp.Key, PatternFlag = 1 };
PNTokenKey key = new PNTokenKey { ResourceType = "user", ResourceId = kvp.Key, PatternFlag = 1 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
if (tokenObj.SpacePatterns != null && tokenObj.SpacePatterns.Count > 0)
{
foreach (KeyValuePair<string, PNResourcePermission> kvp in tokenObj.SpacePatterns)
{
TokenKey key = new TokenKey { ResourceType = "space", ResourceId = kvp.Key, PatternFlag = 1 };
PNTokenKey key = new PNTokenKey { ResourceType = "space", ResourceId = kvp.Key, PatternFlag = 1 };
dicToken[pubnubInstanceId].AddOrUpdate(key, token, (oldVal, newVal) => token);
}
}
#endregion
#endregion
}
#if DEBUG && NET461
#if DEBUG
System.Diagnostics.Debug.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(dicToken, new TokenManagerConverter()));
#endif
}
Expand All @@ -409,7 +386,7 @@ public string GetToken(string resourceType, string resourceId)
{
string resultToken = "";

TokenKey key = new TokenKey { ResourceType = resourceType, ResourceId = resourceId, PatternFlag = 0 };
PNTokenKey key = new PNTokenKey { ResourceType = resourceType, ResourceId = resourceId, PatternFlag = 0 };
if (!string.IsNullOrEmpty(pubnubInstanceId) && dicToken[pubnubInstanceId].ContainsKey(key))
{
resultToken = dicToken[pubnubInstanceId][key];
Expand All @@ -431,7 +408,7 @@ public string GetToken(string resourceType, string resourceId, bool pattern)
List<string> tokenKeyPatternList = dicToken[pubnubInstanceId].Keys.Where(k => patterFlag == k.PatternFlag && resourceType == k.ResourceType && Regex.IsMatch(resourceId, k.ResourceId)).Select(k => k.ResourceId).ToList();
string targetResourceId = (tokenKeyPatternList != null && tokenKeyPatternList.Count > 0) ? tokenKeyPatternList[0] : "";

TokenKey key = new TokenKey { ResourceType = resourceType, ResourceId = targetResourceId, PatternFlag = patterFlag };
PNTokenKey key = new PNTokenKey { ResourceType = resourceType, ResourceId = targetResourceId, PatternFlag = patterFlag };
if (!string.IsNullOrEmpty(pubnubInstanceId) && dicToken[pubnubInstanceId].ContainsKey(key))
{
resultToken = dicToken[pubnubInstanceId][key];
Expand All @@ -452,13 +429,26 @@ public string GetToken(string resourceType, string resourceId, bool pattern)
return resultToken;
}

public List<string> GetAllTokens()
public Dictionary<PNTokenKey,string> GetAllTokens()
{
Dictionary<PNTokenKey, string> tokenList = null;

if (!string.IsNullOrEmpty(pubnubInstanceId) && dicToken != null && dicToken.ContainsKey(pubnubInstanceId))
{
ConcurrentDictionary<PNTokenKey, string> currentInstanceTokens = dicToken[pubnubInstanceId];
tokenList = new Dictionary<PNTokenKey, string>(currentInstanceTokens);
}

return tokenList;
}

public Dictionary<PNTokenKey, string> GetTokensByResource(string resourceType)
{
List<string> tokenList = null;
Dictionary<PNTokenKey, string> tokenList = null;

if (!string.IsNullOrEmpty(pubnubInstanceId) && dicToken != null && dicToken.ContainsKey(pubnubInstanceId))
{
tokenList = dicToken[pubnubInstanceId].Values.Distinct().ToList();
tokenList = dicToken[pubnubInstanceId].Where(tk=> tk.Key.ResourceType == resourceType).ToDictionary(kvp=> kvp.Key, kvp=> kvp.Value);
}

return tokenList;
Expand Down Expand Up @@ -496,7 +486,7 @@ internal void Destroy()

}

#region IDisposable Support
#region IDisposable Support
private bool disposedValue;

protected virtual void DisposeInternal(bool disposing)
Expand All @@ -517,7 +507,7 @@ void IDisposable.Dispose()
{
DisposeInternal(true);
}
#endregion
#endregion

}
}
3 changes: 3 additions & 0 deletions src/Api/PubnubApi/Interface/IJsonPluggableLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IJsonPluggableLibrary

string SerializeToJsonString(object objectToSerialize);

string SerializeDictionaryOfTokenKey(Dictionary<PNTokenKey, string> objectToSerialize);

List<object> DeserializeToListOfObject(string jsonString);

object DeserializeToObject(string jsonString);
Expand All @@ -25,5 +27,6 @@ public interface IJsonPluggableLibrary
object[] ConvertToObjectArray(object localContainer);

void PopulateObject(string value, object target);

}
}
33 changes: 33 additions & 0 deletions src/Api/PubnubApi/Model/Consumer/PNTokenKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace PubnubApi
{
public class PNTokenKey
{
[JsonProperty("ResourceType ")]
public string ResourceType { get; set; }

[JsonProperty("ResourceId ")]
public string ResourceId { get; set; }

[JsonProperty("PatternFlag ")]
public int PatternFlag { get; set; }

public override bool Equals(object obj)
{
PNTokenKey currentKey = obj as PNTokenKey;
if (currentKey == null)
{
return false;
}
return currentKey.ResourceType == this.ResourceType && currentKey.ResourceId == this.ResourceId && currentKey.PatternFlag == this.PatternFlag;
}

public override int GetHashCode() => ResourceType.GetHashCode() ^ PatternFlag.GetHashCode() ^ ResourceId.GetHashCode();
}
}
10 changes: 10 additions & 0 deletions src/Api/PubnubApi/NewtonsoftJsonDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public string SerializeToJsonString(object objectToSerialize)
return JsonConvert.SerializeObject(objectToSerialize);
}

/// <summary>
/// Use this to serialize Dictionary<PNTokenKey, string>
/// </summary>
/// <param name="objectToSerialize"></param>
/// <returns></returns>
public string SerializeDictionaryOfTokenKey(Dictionary<PNTokenKey, string> objectToSerialize)
{
return JsonConvert.SerializeObject(objectToSerialize, new EndPoint.TokenManager.TokenManagerConverter());
}

public List<object> DeserializeToListOfObject(string jsonString)
{
List<object> result = JsonConvert.DeserializeObject<List<object>>(jsonString);
Expand Down
Loading

0 comments on commit c616ba7

Please sign in to comment.