public PNGrantTokenDecoded GetPermissions(string token) { token = token.Replace("-", "+").Replace("_", "/"); int i = token.Length % 4; if (i != 0) { token += new String('=', 4 - i); } #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(token, PNLoggingMethod.LevelInfo); #endif PNGrantTokenDecoded pnGrantTokenDecoded = new PNGrantTokenDecoded(); pnGrantTokenDecoded.Patterns = new GrantResources { Channels = new Dictionary <string, int>(), Groups = new Dictionary <string, int>(), Users = new Dictionary <string, int>(), Spaces = new Dictionary <string, int>() }; pnGrantTokenDecoded.Resources = new GrantResources { Channels = new Dictionary <string, int>(), Groups = new Dictionary <string, int>(), Users = new Dictionary <string, int>(), Spaces = new Dictionary <string, int>() }; pnGrantTokenDecoded.Meta = new Dictionary <string, object>(); byte[] decryptedBytes = Convert.FromBase64CharArray(token.ToCharArray(), 0, token.Length); var cbor = CBORObject.DecodeFromBytes(decryptedBytes); ParseCBOR(cbor, "", ref pnGrantTokenDecoded); return(pnGrantTokenDecoded); }
public void StoreToken(string token) { if (PubNubInstance.PNConfig.StoreTokensOnGrant) { try { PNGrantTokenDecoded pnGrantTokenDecoded = GetPermissions(token); ParseGrantResources(pnGrantTokenDecoded.Resources, token, pnGrantTokenDecoded.Timestamp, pnGrantTokenDecoded.TTL, false); //clear all Users/Spaces pattern maps (by design, store last token only for patterns) Tokens.SpacesPattern = new SafeDictionary <string, UserSpacePermissionsWithToken>(); Tokens.UsersPattern = new SafeDictionary <string, UserSpacePermissionsWithToken>(); ParseGrantResources(pnGrantTokenDecoded.Patterns, token, pnGrantTokenDecoded.Timestamp, pnGrantTokenDecoded.TTL, true); } catch (Exception ex) { //Logging the exception when the debug symbol is set. #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(ex.ToString(), PNLoggingMethod.LevelError); #endif } } }
public void ParseCBOR(CBORObject cbor, string parent, ref PNGrantTokenDecoded pnGrantTokenDecoded) { foreach (KeyValuePair <CBORObject, CBORObject> kvp in cbor.Entries) { if (kvp.Key.Type.ToString().Equals("ByteString")) { string key = System.Text.Encoding.ASCII.GetString(kvp.Key.GetByteString()); ParseCBORValue(key, parent, kvp, ref pnGrantTokenDecoded); } else if (kvp.Key.Type.ToString().Equals("TextString")) { #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(string.Format("TextString Key {0}-{1}-{2}", kvp.Key.ToString(), kvp.Value.ToString(), kvp.Value.Type), PNLoggingMethod.LevelInfo); #endif ParseCBORValue(kvp.Key.ToString(), parent, kvp, ref pnGrantTokenDecoded); } #if (ENABLE_PUBNUB_LOGGING) else { this.PubNubInstance.PNLog.WriteToLog(string.Format("Others Key {0}-{1}-{2}-{3}", kvp.Key, kvp.Key.Type, kvp.Value, kvp.Value.Type), PNLoggingMethod.LevelError); } #endif } }
public void FillGrantToken(string parent, string key, object val, Type type, ref PNGrantTokenDecoded pnGrantTokenDecoded) { #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(string.Format("FillGrantToken: {0}", key), PNLoggingMethod.LevelInfo); #endif key = ReplaceBoundaryQuotes(key); #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(string.Format("FillGrantToken after: {0}", key), PNLoggingMethod.LevelInfo); #endif int i = 0; long l = 0; switch (type.Name) { case "Int32": if (!int.TryParse(val.ToString(), out i)) { //log } break; case "Int64": if (!long.TryParse(val.ToString(), out l)) { //log } break; case "String": // do nothing break; default: #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(string.Format("typeName: {0}", type.Name), PNLoggingMethod.LevelInfo); #endif break; } switch (key) { case "v": pnGrantTokenDecoded.Version = i; break; case "t": pnGrantTokenDecoded.Timestamp = i; break; case "ttl": pnGrantTokenDecoded.TTL = i; break; case "meta": pnGrantTokenDecoded.Meta = val as Dictionary <string, object>; break; case "sig": pnGrantTokenDecoded.Signature = System.Text.Encoding.ASCII.GetBytes(val.ToString()); break; default: switch (parent) { case "res:spc": pnGrantTokenDecoded.Resources.Spaces[key] = i; break; case "res:usr": pnGrantTokenDecoded.Resources.Users[key] = i; break; case "res:chan": pnGrantTokenDecoded.Resources.Channels[key] = i; break; case "res:grp": pnGrantTokenDecoded.Resources.Groups[key] = i; break; case "pat:spc": pnGrantTokenDecoded.Patterns.Spaces[key] = i; break; case "pat:usr": pnGrantTokenDecoded.Patterns.Users[key] = i; break; case "pat:chan": pnGrantTokenDecoded.Patterns.Channels[key] = i; break; case "pat:grp": pnGrantTokenDecoded.Patterns.Groups[key] = i; break; default: #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(string.Format("No match on parent: {0}", parent), PNLoggingMethod.LevelInfo); #endif break; } break; } }
public void ParseCBORValue(string key, string parent, KeyValuePair <CBORObject, CBORObject> kvp, ref PNGrantTokenDecoded pnGrantTokenDecoded) { if (kvp.Value.Type.ToString().Equals("Map")) { #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(string.Format("Map Key {0}", key), PNLoggingMethod.LevelInfo); #endif var p = string.Format("{0}{1}{2}", parent, string.IsNullOrEmpty(parent)?"":":", key); ParseCBOR(kvp.Value, p, ref pnGrantTokenDecoded); } else if (kvp.Value.Type.ToString().Equals("ByteString")) { #if (ENABLE_PUBNUB_LOGGING) string val = System.Text.Encoding.ASCII.GetString(kvp.Value.GetByteString()); this.PubNubInstance.PNLog.WriteToLog(string.Format("ByteString Value {0}-{1}", key, val), PNLoggingMethod.LevelInfo); #endif FillGrantToken(parent, key, kvp.Value, typeof(string), ref pnGrantTokenDecoded); } else if (kvp.Value.Type.ToString().Equals("Integer")) { #if (ENABLE_PUBNUB_LOGGING) this.PubNubInstance.PNLog.WriteToLog(string.Format("Integer Value {0}-{1}", key, kvp.Value), PNLoggingMethod.LevelInfo); #endif FillGrantToken(parent, key, kvp.Value, typeof(int), ref pnGrantTokenDecoded); } #if (ENABLE_PUBNUB_LOGGING) else { this.PubNubInstance.PNLog.WriteToLog(string.Format("Others Key Value {0}-{1}-{2}-{3}", kvp.Key.Type, kvp.Value.Type, key, kvp.Value), PNLoggingMethod.LevelError); } #endif }