Skip to content

Commit

Permalink
fix: Adding TaskRouterGrant file (#748)
Browse files Browse the repository at this point in the history
* fix: Adding TaskRouterGrant file
  • Loading branch information
tiwarishubham635 committed May 30, 2024
1 parent bc1401a commit 4542584
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ docker-push:
docker push twilio/twilio-csharp:${CURRENT_TAG}

cover:
dotnet sonarscanner begin /k:"$(PROJECT_NAME)" /o:"twilio" /d:sonar.host.url=https://sonarcloud.io /d:sonar.login="${SONAR_TOKEN}" /d:sonar.language="cs" $(SONAR_SOURCES) /d:sonar.cs.opencover.reportsPaths="test/lcov.net451.opencover.xml"
# TODO: /d:sonar.coverage.exclusions is a temporary fix for new code files to be ignored from coverage report
dotnet sonarscanner begin /k:"$(PROJECT_NAME)" /o:"twilio" /d:sonar.host.url=https://sonarcloud.io /d:sonar.token="${SONAR_TOKEN}" /d:sonar.language="cs" $(SONAR_SOURCES) /d:sonar.coverage.exclusions=src/Twilio/JWT/AccessToken/TaskRouterGrant.cs /d:sonar.cs.opencover.reportsPaths="test/lcov.opencover.xml"
# Write to a log file since the logs for build with sonar analyzer are pretty beefy and travis has a limit on the number of log lines
dotnet build Twilio.sln > buildsonar.log
dotnet test test/Twilio.Test/Twilio.Test.csproj --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=../lcov
dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
dotnet sonarscanner end /d:sonar.token="${SONAR_TOKEN}"

cache:
directories:
Expand Down
67 changes: 67 additions & 0 deletions src/Twilio/JWT/AccessToken/TaskRouterGrant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Collections.Generic;

namespace Twilio.Jwt.AccessToken
{
/// <summary>
/// Grant to use for Twilio TaskRouter
/// </summary>
public class TaskRouterGrant : IGrant
{
/// <summary>
/// Workspace SID
/// </summary>
public string WorkspaceSid { get; set; }

/// <summary>
/// Worker SID
/// </summary>
public string WorkerSid { get; set; }

/// <summary>
/// Role
/// </summary>
public string Role { get; set; }

/// <summary>
/// Get the grant name
/// </summary>
///
/// <returns>name of the grant</returns>
public string Key
{
get
{
return "task_router";
}
}

/// <summary>
/// Get the grant payload
/// </summary>
///
/// <returns>payload of the grant</returns>
public object Payload
{
get
{
var payload = new Dictionary<string, string>();

if (WorkspaceSid != null)
{
payload.Add("workspace_sid", WorkspaceSid);
}
if (WorkerSid != null)
{
payload.Add("worker_sid", WorkerSid);
}
if (Role != null)
{
payload.Add("role", Role);
}

return payload;
}
}

}
}
35 changes: 35 additions & 0 deletions test/Twilio.Test/Jwt/AccessToken/AccessTokenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,41 @@ public void TestCreatePlaybackGrant()
Assert.AreEqual("https://000.us-east-1.playback.live-video.net/api/video/v1/us-east-000.channel.000?token=xxxxx", decodedVg["playbackUrl"]);
Assert.AreEqual("VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", decodedVg["playerStreamerSid"]);
}

[Test]
public void TestCreateTaskRouterGrant()
{
var grants = new HashSet<IGrant>
{
{
new TaskRouterGrant
{
WorkspaceSid = "WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
WorkerSid = "WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
Role = "worker"
}
}
};
var token = new TestToken("AC456", "SK123", Secret, grants: grants).ToJwt();
Assert.IsNotNull(token);
Assert.IsNotEmpty(token);

var decoded = new DecodedJwt(token, Secret);
var payload = decoded.Payload;
Assert.IsNotNull(payload);

Assert.AreEqual("SK123", payload["iss"]);
Assert.AreEqual("AC456", payload["sub"]);
Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));

var decodedGrants = ToDict(payload["grants"]);
Assert.AreEqual(1, decodedGrants.Count);

var decodedCg = ToDict(decodedGrants["task_router"]);
Assert.AreEqual("WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", decodedCg["workspace_sid"]);
Assert.AreEqual("WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", decodedCg["worker_sid"]);
Assert.AreEqual("worker", decodedCg["role"]);
}

}
}
6 changes: 6 additions & 0 deletions test/Twilio.Test/Twilio.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="NUnitLite" Version="3.13.2" />

<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 4542584

Please sign in to comment.