From c962135c3f60c528aeefa7c19941876a5175d53d Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Wed, 27 Jul 2022 15:38:25 +0530 Subject: [PATCH] GrantToken user changes (#151) * Added Spaces, Users and AuthorizedUser and validations for GrantToken * Deprecated Channels, Uuids and AuthorizedUuid for GrantToken --- .pubnub.yml | 19 ++- CHANGELOG | 4 + .../EndPoint/Access/GrantTokenOperation.cs | 109 +++++++++++++-- .../PNTokenPermissionMappingBase.cs | 6 + src/Api/PubnubApi/Properties/AssemblyInfo.cs | 4 +- src/Api/PubnubApi/PubnubApi.csproj | 5 +- src/Api/PubnubApiPCL/PubnubApiPCL.csproj | 5 +- src/Api/PubnubApiUWP/PubnubApiUWP.csproj | 5 +- .../PubnubApi.ConsoleExample.csproj | 4 +- .../PubnubAsyncAwaitExample.cs | 34 ++--- .../PubnubApi.ConsoleExample/PubnubExample.cs | 34 ++--- .../PubnubApi.ConsoleExample/packages.config | 2 +- .../PubnubApi.Tests/WhenGrantIsRequested.cs | 132 ++++++++++++++++-- 13 files changed, 278 insertions(+), 85 deletions(-) diff --git a/.pubnub.yml b/.pubnub.yml index c98d6e953..0bba21805 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,8 +1,13 @@ name: c-sharp -version: "6.6.0" +version: "6.7.0" schema: 1 scm: github.com/pubnub/c-sharp changelog: + - date: 2022-07-27 + version: v6.7.0 + changes: + - type: improvement + text: "Added support for Users/Spaces to GrantToken." - date: 2022-07-18 version: v6.6.0 changes: @@ -660,7 +665,7 @@ features: - QUERY-PARAM supported-platforms: - - version: Pubnub 'C#' 6.6.0 + version: Pubnub 'C#' 6.7.0 platforms: - Windows 10 and up - Windows Server 2008 and up @@ -670,7 +675,7 @@ supported-platforms: - .Net Framework 4.5 - .Net Framework 4.6.1+ - - version: PubnubPCL 'C#' 6.6.0 + version: PubnubPCL 'C#' 6.7.0 platforms: - Xamarin.Android - Xamarin.iOS @@ -690,7 +695,7 @@ supported-platforms: - .Net Core - .Net 6.0 - - version: PubnubUWP 'C#' 6.6.0 + version: PubnubUWP 'C#' 6.7.0 platforms: - Windows Phone 10 - Universal Windows Apps @@ -714,7 +719,7 @@ sdks: distribution-type: source distribution-repository: GitHub package-name: Pubnub - location: https://github.com/pubnub/c-sharp/releases/tag/v6.6.0.0 + location: https://github.com/pubnub/c-sharp/releases/tag/v6.7.0.0 requires: - name: ".Net" @@ -997,7 +1002,7 @@ sdks: distribution-type: source distribution-repository: GitHub package-name: PubNubPCL - location: https://github.com/pubnub/c-sharp/releases/tag/v6.6.0.0 + location: https://github.com/pubnub/c-sharp/releases/tag/v6.7.0.0 requires: - name: ".Net Core" @@ -1356,7 +1361,7 @@ sdks: distribution-type: source distribution-repository: GitHub package-name: PubnubUWP - location: https://github.com/pubnub/c-sharp/releases/tag/v6.6.0.0 + location: https://github.com/pubnub/c-sharp/releases/tag/v6.7.0.0 requires: - name: "Universal Windows Platform Development" diff --git a/CHANGELOG b/CHANGELOG index c2c40b992..4d6f6fcf5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +v6.7.0 - July 27 2022 +----------------------------- +- Modified: added support for Users/Spaces to GrantToken. + v6.6.0 - July 18 2022 ----------------------------- - Modified: introduced UserId to PNConfiguration and deprecated UUID. diff --git a/src/Api/PubnubApi/EndPoint/Access/GrantTokenOperation.cs b/src/Api/PubnubApi/EndPoint/Access/GrantTokenOperation.cs index 57b4fdd61..c53f7ce36 100644 --- a/src/Api/PubnubApi/EndPoint/Access/GrantTokenOperation.cs +++ b/src/Api/PubnubApi/EndPoint/Access/GrantTokenOperation.cs @@ -21,14 +21,18 @@ public class GrantTokenOperation : PubnubCoreBase private PNTokenResources pubnubResources = new PNTokenResources { Channels = new Dictionary(), + Spaces = new Dictionary(), ChannelGroups = new Dictionary(), - Uuids = new Dictionary() + Uuids = new Dictionary(), + Users = new Dictionary() }; private PNTokenPatterns pubnubPatterns = new PNTokenPatterns { Channels = new Dictionary(), + Spaces = new Dictionary(), ChannelGroups = new Dictionary(), - Uuids = new Dictionary() + Uuids = new Dictionary(), + Users = new Dictionary() }; private int grantTTL = -1; @@ -36,6 +40,7 @@ public class GrantTokenOperation : PubnubCoreBase private Dictionary queryParam; private Dictionary grantMeta; private string pubnubAuthorizedUuid = string.Empty; + private string pubnubAuthorizedUserId = string.Empty; public GrantTokenOperation(PNConfiguration pubnubConfig, IJsonPluggableLibrary jsonPluggableLibrary, IPubnubUnitTest pubnubUnit, IPubnubLog log, EndPoint.TelemetryManager telemetryManager, EndPoint.TokenManager tokenManager, Pubnub instance) : base(pubnubConfig, jsonPluggableLibrary, pubnubUnit, log, telemetryManager, tokenManager, instance) { @@ -49,21 +54,50 @@ public GrantTokenOperation(PNConfiguration pubnubConfig, IJsonPluggableLibrary j InitializeDefaultVariableObjectStates(); } + [Obsolete("AuthorizedUuid is deprecated, please use AuthorizedUserId instead.")] public GrantTokenOperation AuthorizedUuid(string uuid) { - this.pubnubAuthorizedUuid = uuid; + if (!string.IsNullOrEmpty(pubnubAuthorizedUserId)) + { + throw new ArgumentException("Either UUID or UserId can be used. Not both."); + } + pubnubAuthorizedUuid = uuid; + return this; + } + + public GrantTokenOperation AuthorizedUserId(UserId user) + { + if (!string.IsNullOrEmpty(pubnubAuthorizedUuid)) + { + throw new ArgumentException("Either UUID or UserId can be used. Not both."); + } + pubnubAuthorizedUserId = user; return this; } public GrantTokenOperation Resources(PNTokenResources resources) { - if (pubnubResources != null) + if (pubnubResources != null && resources != null) { + if (resources.Channels != null && resources.Channels.Count > 0 && + resources.Spaces != null && resources.Spaces.Count > 0) + { + throw new ArgumentException("Either Channels or Spaces can be used. Not both."); + } + if (resources.Uuids != null && resources.Uuids.Count > 0 && + resources.Users != null && resources.Users.Count > 0) + { + throw new ArgumentException("Either Uuids or Users can be used. Not both."); + } pubnubResources = resources; if (pubnubResources.Channels == null) { pubnubResources.Channels = new Dictionary(); } + if (pubnubResources.Spaces == null) + { + pubnubResources.Spaces = new Dictionary(); + } if (pubnubResources.ChannelGroups == null) { pubnubResources.ChannelGroups = new Dictionary(); @@ -72,19 +106,38 @@ public GrantTokenOperation Resources(PNTokenResources resources) { pubnubResources.Uuids = new Dictionary(); } + if (pubnubResources.Users == null) + { + pubnubResources.Users = new Dictionary(); + } } return this; } public GrantTokenOperation Patterns(PNTokenPatterns patterns) { - if (pubnubPatterns != null) + if (pubnubPatterns != null && patterns != null) { + if (patterns.Channels != null && patterns.Channels.Count > 0 && + patterns.Spaces != null && patterns.Spaces.Count > 0) + { + throw new ArgumentException("Either Channels or Spaces can be used. Not both."); + } + if (patterns.Uuids != null && patterns.Uuids.Count > 0 && + patterns.Users != null && patterns.Users.Count > 0) + { + throw new ArgumentException("Either Uuids or Users can be used. Not both."); + } + pubnubPatterns = patterns; if (pubnubPatterns.Channels == null) { pubnubPatterns.Channels = new Dictionary(); } + if (pubnubPatterns.Spaces == null) + { + pubnubPatterns.Spaces = new Dictionary(); + } if (pubnubPatterns.ChannelGroups == null) { pubnubPatterns.ChannelGroups = new Dictionary(); @@ -93,6 +146,10 @@ public GrantTokenOperation Patterns(PNTokenPatterns patterns) { pubnubPatterns.Uuids = new Dictionary(); } + if (pubnubPatterns.Users == null) + { + pubnubPatterns.Users = new Dictionary(); + } } return this; @@ -185,6 +242,19 @@ internal void GrantAccess(PNCallback callback) Dictionary chPatternBitmaskPermCollection = null; atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubPatterns.Channels, atleastOnePermission, out chPatternBitmaskPermCollection); + Dictionary spBitmaskPermCollection = null; + Dictionary spPatternBitmaskPermCollection = null; + if (pubnubResources.Channels.Count == 0 && pubnubPatterns.Channels.Count == 0) + { + atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubResources.Spaces, atleastOnePermission, out spBitmaskPermCollection); + atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubPatterns.Spaces, atleastOnePermission, out spPatternBitmaskPermCollection); + } + else + { + spBitmaskPermCollection = new Dictionary(); + spPatternBitmaskPermCollection = new Dictionary(); + } + Dictionary cgBitmaskPermCollection = null; atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubResources.ChannelGroups, atleastOnePermission, out cgBitmaskPermCollection); @@ -197,24 +267,38 @@ internal void GrantAccess(PNCallback callback) Dictionary uuidPatternBitmaskPermCollection = null; atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubPatterns.Uuids, atleastOnePermission, out uuidPatternBitmaskPermCollection); + Dictionary userBitmaskPermCollection = null; + Dictionary userPatternBitmaskPermCollection = null; + if (pubnubResources.Uuids.Count == 0 && pubnubPatterns.Uuids.Count == 0) + { + atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubResources.Users, atleastOnePermission, out userBitmaskPermCollection); + atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubPatterns.Users, atleastOnePermission, out userPatternBitmaskPermCollection); + } + else + { + userBitmaskPermCollection = new Dictionary(); + userPatternBitmaskPermCollection = new Dictionary(); + + } + if (!atleastOnePermission) { - LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} At least one permission is needed for at least one or more of uuids, channels or groups",DateTime.Now.ToString(CultureInfo.InvariantCulture)), PNLogVerbosity.BODY); + LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} At least one permission is needed for at least one or more of uuids/users, channels/spaces or groups",DateTime.Now.ToString(CultureInfo.InvariantCulture)), PNLogVerbosity.BODY); } Dictionary resourcesCollection = new Dictionary(); resourcesCollection.Add("channels", chBitmaskPermCollection); resourcesCollection.Add("groups", cgBitmaskPermCollection); resourcesCollection.Add("uuids", uuidBitmaskPermCollection); - resourcesCollection.Add("users", new Dictionary()); //Empty object for users for json structure - resourcesCollection.Add("spaces", new Dictionary()); //Empty object for spaces for json structure + resourcesCollection.Add("users", userBitmaskPermCollection); + resourcesCollection.Add("spaces", spBitmaskPermCollection); Dictionary patternsCollection = new Dictionary(); patternsCollection.Add("channels", chPatternBitmaskPermCollection); patternsCollection.Add("groups", cgPatternBitmaskPermCollection); patternsCollection.Add("uuids", uuidPatternBitmaskPermCollection); - patternsCollection.Add("users", new Dictionary()); //Empty object for users for json structure - patternsCollection.Add("spaces", new Dictionary()); //Empty object for spaces for json structure + patternsCollection.Add("users", userPatternBitmaskPermCollection); + patternsCollection.Add("spaces", spPatternBitmaskPermCollection); Dictionary optimizedMeta = new Dictionary(); if (this.grantMeta != null) @@ -230,7 +314,10 @@ internal void GrantAccess(PNCallback callback) { permissionCollection.Add("uuid", this.pubnubAuthorizedUuid); } - + else if (!string.IsNullOrEmpty(this.pubnubAuthorizedUserId) && this.pubnubAuthorizedUserId.Trim().Length > 0) + { + permissionCollection.Add("uuid", this.pubnubAuthorizedUserId); + } Dictionary messageEnvelope = new Dictionary(); messageEnvelope.Add("ttl", this.grantTTL); messageEnvelope.Add("permissions", permissionCollection); diff --git a/src/Api/PubnubApi/Model/Consumer/AccessManager/PNTokenPermissionMappingBase.cs b/src/Api/PubnubApi/Model/Consumer/AccessManager/PNTokenPermissionMappingBase.cs index a62a626d3..04a762f16 100644 --- a/src/Api/PubnubApi/Model/Consumer/AccessManager/PNTokenPermissionMappingBase.cs +++ b/src/Api/PubnubApi/Model/Consumer/AccessManager/PNTokenPermissionMappingBase.cs @@ -15,9 +15,15 @@ public class PNTokenPatterns : PNTokenPermissionMappingBase } public class PNTokenPermissionMappingBase { + [Obsolete("Channels is deprecated, please use Spaces instead.")] public Dictionary Channels { get; set; } + + [Obsolete] public Dictionary ChannelGroups { get; set; } + + [Obsolete("Uuids is deprecated, please use Users instead.")] public Dictionary Uuids { get; set; } + public Dictionary Users { get; set; } public Dictionary Spaces { get; set; } diff --git a/src/Api/PubnubApi/Properties/AssemblyInfo.cs b/src/Api/PubnubApi/Properties/AssemblyInfo.cs index cd393ca44..6fd67cfe4 100644 --- a/src/Api/PubnubApi/Properties/AssemblyInfo.cs +++ b/src/Api/PubnubApi/Properties/AssemblyInfo.cs @@ -11,8 +11,8 @@ [assembly: AssemblyProduct("Pubnub C# SDK")] [assembly: AssemblyCopyright("Copyright © 2021")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("6.6.0.0")] -[assembly: AssemblyFileVersion("6.6.0.0")] +[assembly: AssemblyVersion("6.7.0.0")] +[assembly: AssemblyFileVersion("6.7.0.0")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. diff --git a/src/Api/PubnubApi/PubnubApi.csproj b/src/Api/PubnubApi/PubnubApi.csproj index fc84fd6f8..cf2ce9670 100644 --- a/src/Api/PubnubApi/PubnubApi.csproj +++ b/src/Api/PubnubApi/PubnubApi.csproj @@ -13,7 +13,7 @@ Pubnub - 6.6.0.0 + 6.7.0.0 PubNub C# .NET - Web Data Push API Pandu Masabathula PubNub @@ -21,8 +21,7 @@ http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png true https://github.com/pubnub/c-sharp/ - Introduced UserId to PNConfiguration and deprecated UUID. -Added build target framework support to .Net Framework 4.8 and .Net 6.0. + Added support for Users/Spaces to GrantToken. Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously diff --git a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj index 3fb3f859c..c17393579 100644 --- a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj +++ b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj @@ -14,7 +14,7 @@ PubnubPCL - 6.6.0.0 + 6.7.0.0 PubNub C# .NET - Web Data Push API Pandu Masabathula PubNub @@ -22,8 +22,7 @@ http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png true https://github.com/pubnub/c-sharp/ - Introduced UserId to PNConfiguration and deprecated UUID. -Added build target framework support to .Net Framework 4.8 and .Net 6.0. + Added support for Users/Spaces to GrantToken. Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously diff --git a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj index 25b977119..3b8460a23 100644 --- a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj +++ b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj @@ -15,7 +15,7 @@ PubnubUWP - 6.6.0.0 + 6.7.0.0 PubNub C# .NET - Web Data Push API Pandu Masabathula PubNub @@ -23,8 +23,7 @@ http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png true https://github.com/pubnub/c-sharp/ - Introduced UserId to PNConfiguration and deprecated UUID. -Added build target framework support to .Net Framework 4.8 and .Net 6.0. + Added support for Users/Spaces to GrantToken. Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously diff --git a/src/Examples/PubnubApi.ConsoleExample/PubnubApi.ConsoleExample.csproj b/src/Examples/PubnubApi.ConsoleExample/PubnubApi.ConsoleExample.csproj index a94501289..b6ff129ec 100644 --- a/src/Examples/PubnubApi.ConsoleExample/PubnubApi.ConsoleExample.csproj +++ b/src/Examples/PubnubApi.ConsoleExample/PubnubApi.ConsoleExample.csproj @@ -48,8 +48,8 @@ ..\..\packages\PeterO.Numbers.1.8.2\lib\net40\Numbers.dll - - ..\..\packages\Pubnub.6.5.0\lib\net48\Pubnub.dll + + ..\..\packages\Pubnub.6.6.0\lib\net48\Pubnub.dll diff --git a/src/Examples/PubnubApi.ConsoleExample/PubnubAsyncAwaitExample.cs b/src/Examples/PubnubApi.ConsoleExample/PubnubAsyncAwaitExample.cs index 8e0def6e4..db807d09f 100644 --- a/src/Examples/PubnubApi.ConsoleExample/PubnubAsyncAwaitExample.cs +++ b/src/Examples/PubnubApi.ConsoleExample/PubnubAsyncAwaitExample.cs @@ -2113,17 +2113,14 @@ static public async Task MainAsync() } break; case "65": - Console.WriteLine("Enter Channel Name for PAM GrantToken."); - string grantChannelName = Console.ReadLine(); + Console.WriteLine("Enter Space Name for PAM GrantToken."); + string grantSpaceName = Console.ReadLine(); - Console.WriteLine("Enter ChannelGroup Name for PAM GrantToken."); - string grantChannelGroupName = Console.ReadLine(); + Console.WriteLine("Enter UserId for PAM GrantToken."); + string grantUserIdEntry = Console.ReadLine(); - Console.WriteLine("Enter UUID for PAM GrantToken."); - string grantUuiIdEntry = Console.ReadLine(); - - Console.WriteLine("Enter Authorized UUID for PAM GrantToken."); - string grantAuthorizedUuiIdEntry = Console.ReadLine(); + Console.WriteLine("Enter Authorized User for PAM GrantToken."); + string grantAuthorizedUserIdEntry = Console.ReadLine(); Console.WriteLine("Read Access? Enter Y for Yes (default), N for No."); string grantReadAccess = Console.ReadLine(); @@ -2172,10 +2169,9 @@ static public async Task MainAsync() Console.ForegroundColor = ConsoleColor.Blue; StringBuilder pamTokenGrantStringBuilder = new StringBuilder(); - pamTokenGrantStringBuilder.AppendLine(string.Format("Channel = {0}", grantChannelName)); - pamTokenGrantStringBuilder.AppendLine(string.Format("ChannelGroup = {0}", grantChannelGroupName)); - pamTokenGrantStringBuilder.AppendLine(string.Format("UUID = {0}", grantUuiIdEntry)); - pamTokenGrantStringBuilder.AppendLine(string.Format("AuthorizedUUID = {0}", grantAuthorizedUuiIdEntry)); + pamTokenGrantStringBuilder.AppendLine(string.Format("Space = {0}", grantSpaceName)); + pamTokenGrantStringBuilder.AppendLine(string.Format("User = {0}", grantUserIdEntry)); + pamTokenGrantStringBuilder.AppendLine(string.Format("AuthorizedserId = {0}", grantAuthorizedUserIdEntry)); pamTokenGrantStringBuilder.AppendLine(string.Format("Read Access = {0}", grantRead.ToString())); pamTokenGrantStringBuilder.AppendLine(string.Format("Write Access = {0}", grantWrite.ToString())); pamTokenGrantStringBuilder.AppendLine(string.Format("Delete Access = {0}", grantDelete.ToString())); @@ -2193,15 +2189,13 @@ static public async Task MainAsync() PNResult grantTokenResponse = await pubnub.GrantToken() .TTL(grantTokenTTL) .Meta(new Dictionary() { { "score", 100 }, { "color", "red" }, { "author", "pandu" } }) - .AuthorizedUuid(grantAuthorizedUuiIdEntry) + .AuthorizedUserId(grantAuthorizedUserIdEntry) .Resources(new PNTokenResources() { - Channels = new Dictionary() { - { grantChannelName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, - ChannelGroups = new Dictionary() { - {grantChannelGroupName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, - Uuids = new Dictionary() { - { grantUuiIdEntry, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, + Spaces = new Dictionary() { + { grantSpaceName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, + Users = new Dictionary() { + { grantUserIdEntry, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, }) .ExecuteAsync(); PNAccessManagerTokenResult grantTokenResult = grantTokenResponse.Result; diff --git a/src/Examples/PubnubApi.ConsoleExample/PubnubExample.cs b/src/Examples/PubnubApi.ConsoleExample/PubnubExample.cs index f86ad2803..92e698021 100644 --- a/src/Examples/PubnubApi.ConsoleExample/PubnubExample.cs +++ b/src/Examples/PubnubApi.ConsoleExample/PubnubExample.cs @@ -2133,17 +2133,14 @@ static public void Main() })); break; case "65": - Console.WriteLine("Enter Channel Name for PAM GrantToken."); - string grantChannelName = Console.ReadLine(); + Console.WriteLine("Enter Space Name for PAM GrantToken."); + string grantSpaceName = Console.ReadLine(); - Console.WriteLine("Enter ChannelGroup Name for PAM GrantToken."); - string grantChannelGroupName = Console.ReadLine(); + Console.WriteLine("Enter UserId for PAM GrantToken."); + string grantUserIdEntry = Console.ReadLine(); - Console.WriteLine("Enter UUID for PAM GrantToken."); - string grantUuiIdEntry = Console.ReadLine(); - - Console.WriteLine("Enter Authorized UUID for PAM GrantToken."); - string grantAuthorizedUuiIdEntry = Console.ReadLine(); + Console.WriteLine("Enter Authorized User for PAM GrantToken."); + string grantAuthorizedUserIdEntry = Console.ReadLine(); Console.WriteLine("Read Access? Enter Y for Yes (default), N for No."); string grantReadAccess = Console.ReadLine(); @@ -2192,10 +2189,9 @@ static public void Main() Console.ForegroundColor = ConsoleColor.Blue; StringBuilder pamTokenGrantStringBuilder = new StringBuilder(); - pamTokenGrantStringBuilder.AppendLine(string.Format("Channel = {0}", grantChannelName)); - pamTokenGrantStringBuilder.AppendLine(string.Format("ChannelGroup = {0}", grantChannelGroupName)); - pamTokenGrantStringBuilder.AppendLine(string.Format("UUID = {0}", grantUuiIdEntry)); - pamTokenGrantStringBuilder.AppendLine(string.Format("AuthorizedUUID = {0}", grantAuthorizedUuiIdEntry)); + pamTokenGrantStringBuilder.AppendLine(string.Format("Space = {0}", grantSpaceName)); + pamTokenGrantStringBuilder.AppendLine(string.Format("User = {0}", grantUserIdEntry)); + pamTokenGrantStringBuilder.AppendLine(string.Format("AuthorizedUserId = {0}", grantAuthorizedUserIdEntry)); pamTokenGrantStringBuilder.AppendLine(string.Format("Read Access = {0}", grantRead.ToString())); pamTokenGrantStringBuilder.AppendLine(string.Format("Write Access = {0}", grantWrite.ToString())); pamTokenGrantStringBuilder.AppendLine(string.Format("Delete Access = {0}", grantDelete.ToString())); @@ -2213,15 +2209,13 @@ static public void Main() pubnub.GrantToken() .TTL(grantTokenTTL) .Meta(new Dictionary() { { "score", 100 }, { "color", "red" }, { "author", "pandu" } }) - .AuthorizedUuid(grantAuthorizedUuiIdEntry) + .AuthorizedUserId(grantAuthorizedUserIdEntry) .Resources(new PNTokenResources() { - Channels = new Dictionary() { - { grantChannelName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, - ChannelGroups = new Dictionary() { - {grantChannelGroupName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, - Uuids = new Dictionary() { - { grantUuiIdEntry, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, + Spaces = new Dictionary() { + { grantSpaceName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, + Users = new Dictionary() { + { grantUserIdEntry, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } }, }) .Execute(new PNAccessManagerTokenResultExt((result, status) => { diff --git a/src/Examples/PubnubApi.ConsoleExample/packages.config b/src/Examples/PubnubApi.ConsoleExample/packages.config index 358e100e8..ce9efe1e5 100644 --- a/src/Examples/PubnubApi.ConsoleExample/packages.config +++ b/src/Examples/PubnubApi.ConsoleExample/packages.config @@ -4,7 +4,7 @@ - + diff --git a/src/UnitTests/PubnubApi.Tests/WhenGrantIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenGrantIsRequested.cs index 859937262..4ed406bed 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenGrantIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenGrantIsRequested.cs @@ -933,12 +933,10 @@ public static void ThenGrantTokenShouldReturnSuccess() pubnub.GrantToken() .Resources(new PNTokenResources() { - Channels=new Dictionary() { - { "ch1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, - ChannelGroups = new Dictionary() { - { "cg1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, - Uuids = new Dictionary() { - { "uuid1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, + Spaces=new Dictionary() { + { "spc1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, + Users = new Dictionary() { + { "usr1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, } ) .TTL(10) @@ -996,12 +994,10 @@ public static async Task ThenWithAsyncGrantTokenShouldReturnSuccess() PNResult result = await pubnub.GrantToken() .Resources(new PNTokenResources() { - Channels = new Dictionary() { - { "ch1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, - ChannelGroups = new Dictionary() { - { "cg1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, - Uuids = new Dictionary() { - { "uuid1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, + Spaces = new Dictionary() { + { "spc1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, + Users = new Dictionary() { + { "usr1", new PNTokenAuthValues() { Read = true, Write = true, Manage= true, Create = true, Delete=true, Get = true, Update = true, Join = true } } }, } ) .TTL(10) @@ -1032,7 +1028,7 @@ public static void ThenRevokeTokenShouldReturnSuccess() try { - PNResult grantResult = pubnub.GrantToken().TTL(5).Resources(new PNTokenResources() { Channels = new Dictionary() { { "ch1", new PNTokenAuthValues() { Read = true } } } }).ExecuteAsync().Result; + PNResult grantResult = pubnub.GrantToken().TTL(5).Resources(new PNTokenResources() { Spaces = new Dictionary() { { "spc1", new PNTokenAuthValues() { Read = true } } } }).ExecuteAsync().Result; if (grantResult.Result != null && !string.IsNullOrEmpty(grantResult.Result.Token)) { revokeManualEvent = new ManualResetEvent(false); @@ -1065,6 +1061,116 @@ public static void ThenRevokeTokenShouldReturnSuccess() } + [Test] + public static void ThenSetAuthorizedUserFailsWhenAuthorizedUuidIsUsed() + { + server.ClearRequests(); + + PNConfiguration config = new PNConfiguration(new UserId("newuserid")) + { + SubscribeKey = "somesubkey", + PublishKey = "somepubkey", + SecretKey = "someseckey", + Secure = false + }; + pubnub = createPubNubInstance(config); + server.RunOnHttps(config.Secure); + + Assert.Throws(() => + { + PNResult grantResult = pubnub.GrantToken() + .AuthorizedUuid("someuuid") + .AuthorizedUserId(new UserId("someuser")) + .ExecuteAsync().Result; + }); + } + + [Test] + public static void ThenSetAuthorizedUuidFailsWhenAuthorizedUserIsUsed() + { + server.ClearRequests(); + + PNConfiguration config = new PNConfiguration(new UserId("newuserid")) + { + SubscribeKey = "somesubkey", + PublishKey = "somepubkey", + SecretKey = "someseckey", + Secure = false + }; + pubnub = createPubNubInstance(config); + server.RunOnHttps(config.Secure); + + Assert.Throws(() => + { + PNResult grantResult = pubnub.GrantToken() + .AuthorizedUserId(new UserId("someuser")) + .AuthorizedUuid("someuuid") + .ExecuteAsync().Result; + }); + } + + [Test] + public static void ThenSetChannelPermsFailsWhenSpacePermsIsUsed() + { + server.ClearRequests(); + + PNConfiguration config = new PNConfiguration(new UserId("newuserid")) + { + SubscribeKey = "somesubkey", + PublishKey = "somepubkey", + SecretKey = "someseckey", + Secure = false + }; + pubnub = createPubNubInstance(config); + server.RunOnHttps(config.Secure); + + Assert.Throws(() => + { + PNResult grantResult = + pubnub.GrantToken() + .TTL(5) + .Resources(new PNTokenResources() { + Spaces = new Dictionary() { + { "sp1", new PNTokenAuthValues() { Read = true } } + }, + Channels = new Dictionary() { + { "ch1", new PNTokenAuthValues() { Read = true } } + } + }).ExecuteAsync().Result; + }); + } + + [Test] + public static void ThenSetSpacePermsFailsWhenChannelPermsIsUsed() + { + server.ClearRequests(); + + PNConfiguration config = new PNConfiguration(new UserId("newuserid")) + { + SubscribeKey = "somesubkey", + PublishKey = "somepubkey", + SecretKey = "someseckey", + Secure = false + }; + pubnub = createPubNubInstance(config); + server.RunOnHttps(config.Secure); + + Assert.Throws(() => + { + PNResult grantResult = + pubnub.GrantToken() + .TTL(5) + .Resources(new PNTokenResources() + { + Channels = new Dictionary() { + { "ch1", new PNTokenAuthValues() { Read = true } } + }, + Spaces = new Dictionary() { + { "sp1", new PNTokenAuthValues() { Read = true } } + } + }).ExecuteAsync().Result; + }); + } public static byte[] HexStringToByteArray(string hex) {