Skip to content

Commit

Permalink
Add PlaybackGrant (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg authored Oct 15, 2021
1 parent 4c8d2a8 commit 1548860
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
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"]);
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"]);
}

}
}

0 comments on commit 1548860

Please sign in to comment.