Skip to content

Commit

Permalink
GrantToken user changes (#151)
Browse files Browse the repository at this point in the history
* Added Spaces, Users and AuthorizedUser and validations for GrantToken
* Deprecated Channels, Uuids and AuthorizedUuid for GrantToken
  • Loading branch information
budgetpreneur authored Jul 27, 2022
1 parent 17af03d commit c962135
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 85 deletions.
19 changes: 12 additions & 7 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
109 changes: 98 additions & 11 deletions src/Api/PubnubApi/EndPoint/Access/GrantTokenOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ public class GrantTokenOperation : PubnubCoreBase
private PNTokenResources pubnubResources = new PNTokenResources
{
Channels = new Dictionary<string, PNTokenAuthValues>(),
Spaces = new Dictionary<string, PNTokenAuthValues>(),
ChannelGroups = new Dictionary<string, PNTokenAuthValues>(),
Uuids = new Dictionary<string, PNTokenAuthValues>()
Uuids = new Dictionary<string, PNTokenAuthValues>(),
Users = new Dictionary<string, PNTokenAuthValues>()
};
private PNTokenPatterns pubnubPatterns = new PNTokenPatterns
{
Channels = new Dictionary<string, PNTokenAuthValues>(),
Spaces = new Dictionary<string, PNTokenAuthValues>(),
ChannelGroups = new Dictionary<string, PNTokenAuthValues>(),
Uuids = new Dictionary<string, PNTokenAuthValues>()
Uuids = new Dictionary<string, PNTokenAuthValues>(),
Users = new Dictionary<string, PNTokenAuthValues>()
};

private int grantTTL = -1;
private PNCallback<PNAccessManagerTokenResult> savedCallbackGrantToken;
private Dictionary<string, object> queryParam;
private Dictionary<string, object> 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)
{
Expand All @@ -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<string, PNTokenAuthValues>();
}
if (pubnubResources.Spaces == null)
{
pubnubResources.Spaces = new Dictionary<string, PNTokenAuthValues>();
}
if (pubnubResources.ChannelGroups == null)
{
pubnubResources.ChannelGroups = new Dictionary<string, PNTokenAuthValues>();
Expand All @@ -72,19 +106,38 @@ public GrantTokenOperation Resources(PNTokenResources resources)
{
pubnubResources.Uuids = new Dictionary<string, PNTokenAuthValues>();
}
if (pubnubResources.Users == null)
{
pubnubResources.Users = new Dictionary<string, PNTokenAuthValues>();
}
}
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<string, PNTokenAuthValues>();
}
if (pubnubPatterns.Spaces == null)
{
pubnubPatterns.Spaces = new Dictionary<string, PNTokenAuthValues>();
}
if (pubnubPatterns.ChannelGroups == null)
{
pubnubPatterns.ChannelGroups = new Dictionary<string, PNTokenAuthValues>();
Expand All @@ -93,6 +146,10 @@ public GrantTokenOperation Patterns(PNTokenPatterns patterns)
{
pubnubPatterns.Uuids = new Dictionary<string, PNTokenAuthValues>();
}
if (pubnubPatterns.Users == null)
{
pubnubPatterns.Users = new Dictionary<string, PNTokenAuthValues>();
}

}
return this;
Expand Down Expand Up @@ -185,6 +242,19 @@ internal void GrantAccess(PNCallback<PNAccessManagerTokenResult> callback)
Dictionary<string, int> chPatternBitmaskPermCollection = null;
atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubPatterns.Channels, atleastOnePermission, out chPatternBitmaskPermCollection);

Dictionary<string, int> spBitmaskPermCollection = null;
Dictionary<string, int> 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<string, int>();
spPatternBitmaskPermCollection = new Dictionary<string, int>();
}

Dictionary<string, int> cgBitmaskPermCollection = null;
atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubResources.ChannelGroups, atleastOnePermission, out cgBitmaskPermCollection);

Expand All @@ -197,24 +267,38 @@ internal void GrantAccess(PNCallback<PNAccessManagerTokenResult> callback)
Dictionary<string, int> uuidPatternBitmaskPermCollection = null;
atleastOnePermission = FillPermissionMappingWithMaskValues(this.pubnubPatterns.Uuids, atleastOnePermission, out uuidPatternBitmaskPermCollection);

Dictionary<string, int> userBitmaskPermCollection = null;
Dictionary<string, int> 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<string, int>();
userPatternBitmaskPermCollection = new Dictionary<string, int>();

}

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<string, object> resourcesCollection = new Dictionary<string, object>();
resourcesCollection.Add("channels", chBitmaskPermCollection);
resourcesCollection.Add("groups", cgBitmaskPermCollection);
resourcesCollection.Add("uuids", uuidBitmaskPermCollection);
resourcesCollection.Add("users", new Dictionary<string, int>()); //Empty object for users for json structure
resourcesCollection.Add("spaces", new Dictionary<string, int>()); //Empty object for spaces for json structure
resourcesCollection.Add("users", userBitmaskPermCollection);
resourcesCollection.Add("spaces", spBitmaskPermCollection);

Dictionary<string, object> patternsCollection = new Dictionary<string, object>();
patternsCollection.Add("channels", chPatternBitmaskPermCollection);
patternsCollection.Add("groups", cgPatternBitmaskPermCollection);
patternsCollection.Add("uuids", uuidPatternBitmaskPermCollection);
patternsCollection.Add("users", new Dictionary<string, int>()); //Empty object for users for json structure
patternsCollection.Add("spaces", new Dictionary<string, int>()); //Empty object for spaces for json structure
patternsCollection.Add("users", userPatternBitmaskPermCollection);
patternsCollection.Add("spaces", spPatternBitmaskPermCollection);

Dictionary<string, object> optimizedMeta = new Dictionary<string, object>();
if (this.grantMeta != null)
Expand All @@ -230,7 +314,10 @@ internal void GrantAccess(PNCallback<PNAccessManagerTokenResult> callback)
{
permissionCollection.Add("uuid", this.pubnubAuthorizedUuid);
}

else if (!string.IsNullOrEmpty(this.pubnubAuthorizedUserId) && this.pubnubAuthorizedUserId.Trim().Length > 0)
{
permissionCollection.Add("uuid", this.pubnubAuthorizedUserId);
}
Dictionary<string, object> messageEnvelope = new Dictionary<string, object>();
messageEnvelope.Add("ttl", this.grantTTL);
messageEnvelope.Add("permissions", permissionCollection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ public class PNTokenPatterns : PNTokenPermissionMappingBase
}
public class PNTokenPermissionMappingBase
{
[Obsolete("Channels is deprecated, please use Spaces instead.")]
public Dictionary<string, PNTokenAuthValues> Channels { get; set; }

[Obsolete]
public Dictionary<string, PNTokenAuthValues> ChannelGroups { get; set; }

[Obsolete("Uuids is deprecated, please use Users instead.")]
public Dictionary<string, PNTokenAuthValues> Uuids { get; set; }

public Dictionary<string, PNTokenAuthValues> Users { get; set; }
public Dictionary<string, PNTokenAuthValues> Spaces { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions src/Api/PubnubApi/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/Api/PubnubApi/PubnubApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

<PropertyGroup>
<PackageId>Pubnub</PackageId>
<PackageVersion>6.6.0.0</PackageVersion>
<PackageVersion>6.7.0.0</PackageVersion>
<Title>PubNub C# .NET - Web Data Push API</Title>
<Authors>Pandu Masabathula</Authors>
<Owners>PubNub</Owners>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
<PackageReleaseNotes>Introduced UserId to PNConfiguration and deprecated UUID.
Added build target framework support to .Net Framework 4.8 and .Net 6.0.</PackageReleaseNotes>
<PackageReleaseNotes>Added support for Users/Spaces to GrantToken.</PackageReleaseNotes>
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
<!--<Summary>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</Summary>-->
<Description>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</Description>
Expand Down
5 changes: 2 additions & 3 deletions src/Api/PubnubApiPCL/PubnubApiPCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@

<PropertyGroup>
<PackageId>PubnubPCL</PackageId>
<PackageVersion>6.6.0.0</PackageVersion>
<PackageVersion>6.7.0.0</PackageVersion>
<Title>PubNub C# .NET - Web Data Push API</Title>
<Authors>Pandu Masabathula</Authors>
<Owners>PubNub</Owners>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
<PackageReleaseNotes>Introduced UserId to PNConfiguration and deprecated UUID.
Added build target framework support to .Net Framework 4.8 and .Net 6.0.</PackageReleaseNotes>
<PackageReleaseNotes>Added support for Users/Spaces to GrantToken.</PackageReleaseNotes>
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
<!--<Summary>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</Summary>-->
<Description>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</Description>
Expand Down
5 changes: 2 additions & 3 deletions src/Api/PubnubApiUWP/PubnubApiUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@

<PropertyGroup>
<PackageId>PubnubUWP</PackageId>
<PackageVersion>6.6.0.0</PackageVersion>
<PackageVersion>6.7.0.0</PackageVersion>
<Title>PubNub C# .NET - Web Data Push API</Title>
<Authors>Pandu Masabathula</Authors>
<Owners>PubNub</Owners>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
<PackageReleaseNotes>Introduced UserId to PNConfiguration and deprecated UUID.
Added build target framework support to .Net Framework 4.8 and .Net 6.0.</PackageReleaseNotes>
<PackageReleaseNotes>Added support for Users/Spaces to GrantToken.</PackageReleaseNotes>
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
<!--<Summary>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</Summary>-->
<Description>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</Description>
Expand Down
Loading

0 comments on commit c962135

Please sign in to comment.