Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PlaybackGrant #581

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Twilio/JWT/AccessToken/PlaybackGrant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;

namespace Twilio.Jwt.AccessToken
{
/// <summary>
/// Grant to expose Twilio Live
/// </summary>
public class PlaybackGrant : IGrant
{
/// <summary>
/// Grant payload
/// </summary>
public Dictionary<string, object> Grant { get; set; }

/// <summary>
/// Get the playback grant key
/// </summary>
///
/// <returns>the playback grant key</returns>
public string Key
{
get
{
return "player";
}
}

/// <summary>
/// Get the playback grant payload
/// </summary>
///
/// <returns>the video grant payload</returns>
public object Payload
{
get
{
return Grant;
}
}
}
}
36 changes: 36 additions & 0 deletions test/Twilio.Test/Jwt/AccessToken/AccessTokenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,41 @@ public void TestCreateVideoGrant()
Assert.AreEqual("RM123", decodedVg["room"]);
}

[Test]
public void TestCreatePlaybackGrant()
{
var grants = new HashSet<IGrant>
{
{
new PlaybackGrant {
Grant = new Dictionary<string, object> {
{ "requestCredentials", null },
{ "playbackUrl", "https://000.us-east-1.playback.live-video.net/api/video/v1/us-east-000.channel.000?token=xxxxx" },
{ "playerStreamerSid", "VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" },
}
}
}
};
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 decodedVg = ToDict(decodedGrants["player"]);
Assert.AreEqual(null, decodedVg["requestCredentials"]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all the tests across libraries, I haven't seen this value being set. Have you tested the token live with the credentials set?

Copy link
Contributor Author

@miguelgrinberg miguelgrinberg Oct 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field comes like this from the playbackGrant resource. I have not seen it set to anything other than null.

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"]);
}

}
}