Skip to content
Open
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
8 changes: 6 additions & 2 deletions jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ If you can do not care about `tracking`, and your Master Subscribe Token does no
There are some scenarios where the payload will need a `tracking` section (such as if it's already set on the Master Subscribe Token).
Read more [here](./tracking.md)

The `originCluster` can be set in SST if the MST token uses `auto` (or can be the same value as MST).

```psuedojson
{
"streaming": {
"tokenId": number,
"tokenType": "Subscribe",
"streamName": string,
"originCluster": string,
"allowedOrigins": string[],
"allowedIpAddresses": string[],
"tracking": null
Expand Down Expand Up @@ -45,7 +48,8 @@ We support the following: HS256, HS384, HS512.
The JWT signing library may or may not add a `nbf` (NotBefore) field.
This is supported, but not strictly required in our JWTs.

This example below shows an example `SST` with **no tracking**, generated from a Master Subscribe Token that does not have `tracking` enabled.
This example below shows an example `SST` with **no tracking**, generated from a Master Subscribe Token that does not have `tracking` enabled. It also overrides the `originCluster` to the `phx-1` cluster.

For other tracking examples, read [here](./tracking.md)

```json
Expand All @@ -54,6 +58,7 @@ For other tracking examples, read [here](./tracking.md)
"tokenId": 1,
"tokenType": "Subscribe",
"streamName": "teststream",
"originCluster": "phx-1",
"allowedOrigins": [],
"allowedIpAddresses": [],
"tracking": null
Expand All @@ -62,4 +67,3 @@ For other tracking examples, read [here](./tracking.md)
"iat": 1673600857
}
```
```
15 changes: 13 additions & 2 deletions src/dotnet/CSharp.SelfSignJwt/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
sampleTokenWithParentTracking.token,
streamName,
sampleTokenWithParentTracking.tracking);
Console.WriteLine($"SST With Parent Tracking: {selfSignTokenWithParentTracking}\n\n");
Console.WriteLine($"SST with Parent Tracking: {selfSignTokenWithParentTracking}\n\n");



Expand Down Expand Up @@ -58,7 +58,18 @@
streamName,
customViewerData: "uniqueViewer1234");

Console.WriteLine($"SST With customViewerData: {selfSignTokenWithCustomViewerData}\n\n");
Console.WriteLine($"SST with customViewerData: {selfSignTokenWithCustomViewerData}\n\n");



streamName = ChooseStreamName(sampleTokenWithCustomViewerData);
var selfSignTokenWithOriginCluster = tokenGenerator.CreateToken(sampleTokenWithCustomViewerData.tokenId,
sampleTokenWithCustomViewerData.token,
streamName,
originCluster: "phx-1");

Console.WriteLine($"SST with OriginCluster: {selfSignTokenWithOriginCluster}\n\n");



/***
Expand Down
2 changes: 2 additions & 0 deletions src/dotnet/CSharp.SelfSignJwt/SampleSubscribeToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ public class SampleSubscribeToken
[JsonPropertyName("id")]
public uint tokenId { get; init; }
public string token { get; init; }
public string originCluster { get; init; }
public List<TokenStream> streams { get; init; }
public Tracking? tracking { get; init; }

public SampleSubscribeToken()
{
this.streams = new List<TokenStream>();
this.token = string.Empty;
this.originCluster = string.Empty;
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/dotnet/CSharp.SelfSignJwt/TokenGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public string CreateToken(uint tokenId, string tokenString, string streamName,
Tracking? tracking = null,
IEnumerable<string>? allowedOrigins = null, IEnumerable<string>? allowedIpAddresses = null,
int expiresIn = 60,
string? customViewerData = null)
string? customViewerData = null,
string? originCluster = null)
{
var payload = new JwtPayload(tokenId, streamName, allowedOrigins, allowedIpAddresses, tracking, customViewerData);
var payload = new JwtPayload(tokenId, streamName, allowedOrigins, allowedIpAddresses, tracking,
customViewerData, originCluster);
ValidateStreamingPayload(payload.streaming);

var tokenDescriptor = new SecurityTokenDescriptor()
Expand Down Expand Up @@ -66,10 +68,12 @@ class JwtPayload

public JwtPayload(uint tokenId, string streamName,
IEnumerable<string>? allowedOrigins, IEnumerable<string>? allowedIpAddresses, Tracking? tracking,
string? customViewerData)
string? customViewerData,
string? originCluster)
{
this.streaming = new StreamPayload(tokenId, streamName,
allowedOrigins, allowedIpAddresses, tracking, customViewerData);
allowedOrigins, allowedIpAddresses, tracking,
customViewerData, originCluster);
}
}

Expand All @@ -89,16 +93,20 @@ class StreamPayload

public string? customViewerData { get; }

public string? originCluster { get; }

public StreamPayload(uint tokenId, string streamName,
IEnumerable<string>? allowedOrigins, IEnumerable<string>? allowedIpAddresses, Tracking? tracking,
string? customViewerData)
string? customViewerData,
string? originCluster)
{
this.tokenId = tokenId;
this.streamName = streamName;
this.allowedOrigins = allowedOrigins ?? Enumerable.Empty<string>();
this.allowedIpAddresses = allowedIpAddresses ?? Enumerable.Empty<string>();
this.tracking = tracking;
this.customViewerData = customViewerData;
this.originCluster = originCluster;
}
}
}
Expand Down
30 changes: 17 additions & 13 deletions src/nodejs/node-tokengenerator/TokenGenerator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,30 @@ export default class TokenGenerator {
* @param {string} streamName
* @param {string[]=} allowedOrigins
* @param {string[]=} allowedIpAddresses
* @param {Tracking} tracking
* @param {?Tracking} tracking
* @param {?number} [expiresIn = 60]
* @param {?string} customViewerData - Viewer data associated with connections using this token. Max length: 1024
* @param {?string} originCluster
* @returns {string}
*/
createToken(tokenId, token, streamName,
allowedOrigins, allowedIpAddresses ,
tracking,
allowedOrigins, allowedIpAddresses,
tracking = null,
expiresIn = defaultExpiresIn,
customViewerData = null) {
customViewerData = null,
originCluster = null) {
const streaming = {
tokenId: tokenId,
tokenType: 'Subscribe',
streamName: streamName,
originCluster: originCluster ?? null,
tracking: tracking ?? null,
customViewerData: customViewerData ?? null,
allowedOrigins: allowedOrigins ?? [],
allowedIpAddresses: allowedIpAddresses ?? []
};
const payload = {
streaming: {
tokenId: tokenId,
tokenType: 'Subscribe',
streamName: streamName,
tracking: tracking ?? null,
customViewerData: customViewerData ?? null,
allowedOrigins: allowedOrigins ?? [],
allowedIpAddresses: allowedIpAddresses ?? []
}
streaming: Object.fromEntries(Object.entries(streaming).filter(([_, v]) => v !== null))
};
this._validateStreamingPayload(payload.streaming);

Expand Down
4 changes: 2 additions & 2 deletions src/nodejs/node-tokengenerator/Tracking.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class Tracking{
constructor(trackingId){
export default class Tracking {
constructor(trackingId) {
this.trackingId = trackingId;
}
}
31 changes: 24 additions & 7 deletions src/nodejs/node-tokengenerator/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ const sstWithCustomTracking = createSSTWithCustomTrackingInformation();
// use basic SST with customViewerData
const sstWithCustomViewerData = createSSTWithCustomViewerData();

console.log('SST with no TrackingID: '+ sstNoTrackingToken +
'\nSST with parent TrackingID: '+ sstWithParentTrackingToken +
'\nSST with custom TrackingID: '+ sstWithCustomTracking +
'\nSST with customViewerData: ' + sstWithCustomViewerData);
// use basic SST with originCluster
const sstWithOriginCluster = createSSTWithOriginCluster();

console.log('SST with no TrackingID: ' + sstNoTrackingToken +
'\n\n\nSST with parent TrackingID: ' + sstWithParentTrackingToken +
'\n\n\nSST with custom TrackingID: ' + sstWithCustomTracking +
'\n\n\nSST with customViewerData: ' + sstWithCustomViewerData +
'\n\n\nSST with originCluster: ' + sstWithOriginCluster);


function createSSTWithNoTrackingInformation(){
Expand All @@ -34,24 +38,37 @@ function createSSTWithParentTrackingInformation(){
const sampleToken = getSampleToken();
const streamName = getStreamName(sampleToken, regexStreamName);

return generator.createToken(sampleToken.id, sampleToken.token, streamName, null, null, sampleToken.tracking);
return generator.createToken(sampleToken.id, sampleToken.token, streamName,
null, null, sampleToken.tracking);
}

function createSSTWithCustomTrackingInformation(){
const sampleToken = getSampleToken();
const streamName = getStreamName(sampleToken, regexStreamName);

return generator.createToken(sampleToken.id, sampleToken.token, streamName, null, null, new Tracking("customTrackingId"));
return generator.createToken(sampleToken.id, sampleToken.token, streamName,
null, null,
new Tracking("customTrackingId"));
}

function createSSTWithCustomViewerData() {
const sampleToken = getSampleToken();
const streamName = getStreamName(sampleToken, regexStreamName);

return generator.createToken(sampleToken.id, sampleToken.token, streamName, null, null, null, null,
return generator.createToken(sampleToken.id, sampleToken.token, streamName,
null, null, null, null,
'uniqueViewer1234');
}

function createSSTWithOriginCluster() {
const sampleToken = getSampleToken();
const streamName = getStreamName(sampleToken, regexStreamName);

return generator.createToken(sampleToken.id, sampleToken.token, streamName,
null, null, null, null, null,
'phx-1');
}

function getSampleToken(sampleTokenPath = '../../sample-json/sampleSST.json') {
const token = JSON.parse(readFileSync(sampleTokenPath));
if (!token) {
Expand Down
40 changes: 20 additions & 20 deletions src/sample-json/sampleSST.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"id": 527,
"token": "tokenStringtokenStringtokenStringtokenStringtokenStringtokenString",
"label": "sampleSSTLabel",
"addedOn": "2023-03-27T03:52:40Z",
"isActive": true,
"allowedOrigins": [],
"allowedIpAddresses": [],
"allowedCountries": [],
"deniedCountries": [],
"streams": [
{
"streamName": ".*",
"isRegex": true
},
{
"streamName": "lday0h9i",
"isRegex": false
}
],
"originCluster": "do-sfo-legacy"
"id": 527,
"token": "tokenStringtokenStringtokenStringtokenStringtokenStringtokenString",
"label": "sampleSSTLabel",
"addedOn": "2023-03-27T03:52:40Z",
"isActive": true,
"allowedOrigins": [],
"allowedIpAddresses": [],
"allowedCountries": [],
"deniedCountries": [],
"streams": [
{
"streamName": ".*",
"isRegex": true
},
{
"streamName": "lday0h9i",
"isRegex": false
}
],
"originCluster": "auto"
}
38 changes: 19 additions & 19 deletions src/sample-json/sampleSSTWithNoParentTracking.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"tracking": {
"trackingId": "test-test-test"
},
"id": 541,
"token": "tokentokentokentokentokentokentokentokentokentokentokentokentokentokentoken",
"label": "sampleSSTLabel",
"addedOn": "2023-03-27T03:52:40Z",
"isActive": true,
"allowedOrigins": [],
"allowedIpAddresses": [],
"allowedCountries": [],
"deniedCountries": [],
"streams": [
{
"streamName": ".*",
"isRegex": true
}
],
"originCluster": "do-sfo-legacy"
"tracking": {
"trackingId": "test-test-test"
},
"id": 541,
"token": "tokentokentokentokentokentokentokentokentokentokentokentokentokentokentoken",
"label": "sampleSSTLabel",
"addedOn": "2023-03-27T03:52:40Z",
"isActive": true,
"allowedOrigins": [],
"allowedIpAddresses": [],
"allowedCountries": [],
"deniedCountries": [],
"streams": [
{
"streamName": ".*",
"isRegex": true
}
],
"originCluster": "auto"
}
40 changes: 20 additions & 20 deletions src/sample-json/sampleSSTWithParentTracking.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"tracking": {
"trackingId": "test-parent-tracking"
},
"id": 8538,
"label": "mst-pvt-tracking-2",
"token": "testtokentesttokentesttokentesttokentesttokentesttoken",
"addedOn": "2023-03-27T03:52:40Z",
"isActive": true,
"streams": [
{
"streamName": ".*",
"isRegex": true
}
],
"allowedOrigins": [],
"allowedIpAddresses": [],
"allowedCountries": [],
"deniedCountries": [],
"originCluster": "do-sfo-legacy"
}
"tracking": {
"trackingId": "test-parent-tracking"
},
"id": 8538,
"label": "mst-pvt-tracking-2",
"token": "testtokentesttokentesttokentesttokentesttokentesttoken",
"addedOn": "2023-03-27T03:52:40Z",
"isActive": true,
"streams": [
{
"streamName": ".*",
"isRegex": true
}
],
"allowedOrigins": [],
"allowedIpAddresses": [],
"allowedCountries": [],
"deniedCountries": [],
"originCluster": "auto"
}