Skip to content

Commit 028e860

Browse files
committed
TD-4086 Reworks the JWT token code to generate correct format with correct encoding
1 parent 6bfece6 commit 028e860

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

DigitalLearningSolutions.Web/Helpers/ExternalApis/TableauConnectionHelper.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Net.Http.Headers;
1414
using System.Net.Http;
1515
using System.Threading.Tasks;
16+
using Microsoft.AspNetCore.DataProtection;
1617

1718
public interface ITableauConnectionHelperService
1819
{
@@ -40,32 +41,34 @@ public TableauConnectionHelper(IConfiguration config)
4041
}
4142
public string GetTableauJwt(string email)
4243
{
43-
var tokenHandler = new JwtSecurityTokenHandler();
44-
var key = Encoding.ASCII.GetBytes(connectedAppSecretKey);
44+
var key = Encoding.UTF8.GetBytes(connectedAppSecretKey);
45+
var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256);
4546

46-
var claims = new[]
47-
{
48-
new Claim(JwtRegisteredClaimNames.Sub, user),
49-
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
50-
new Claim("users.primaryemail", email),
51-
new Claim("scp", "tableau:views:embed")
52-
};
53-
var securityKey = new SymmetricSecurityKey(key);
54-
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
55-
var header = new JwtHeader(credentials);
56-
header["kid"] = connectedAppSecretId; // Secret ID
57-
header["iss"] = connectedAppClientId; // Issuer (iss)
58-
var payload = new JwtPayload(
59-
connectedAppClientId, // Issuer (iss)
60-
"tableau", // Audience (aud)
61-
claims,
62-
notBefore: DateTime.UtcNow,
63-
expires: DateTime.UtcNow.AddMinutes(5)
64-
);
47+
var claims = new List<Claim>
48+
{
49+
new Claim(JwtRegisteredClaimNames.Sub, user),
50+
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
51+
new Claim("users.primaryemail", email),
52+
};
53+
54+
var header = new JwtHeader(signingCredentials)
55+
{
56+
{ "kid", connectedAppSecretId },
57+
{ "iss", connectedAppClientId }
58+
};
59+
60+
var payload = new JwtPayload
61+
{
62+
{ "iss", connectedAppClientId },
63+
{ "aud", "tableau" },
64+
{ "exp", new DateTimeOffset(DateTime.UtcNow.AddMinutes(5)).ToUnixTimeSeconds() },
65+
{ "sub", user },
66+
{ "scp", new[] { "tableau:content:read" } }
67+
};
6568

6669
var token = new JwtSecurityToken(header, payload);
70+
var tokenHandler = new JwtSecurityTokenHandler();
6771
var tokenString = tokenHandler.WriteToken(token);
68-
6972
return tokenString;
7073
}
7174

DigitalLearningSolutions.Web/appsettings.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@
8080
},
8181
"TableauDashboards": {
8282
"SiteUrl": "https://tabuat.data.england.nhs.uk",
83-
"CompetencyDashboardUrl": "https://tabuat.data.england.nhs.uk/#/workbooks/7839/views",
84-
"Username": "SVC_default_TEL",
83+
"CompetencyDashboardUrl": "https://tabuat.data.england.nhs.uk/#/site/monitor/views/DLSIdentifiableDataNHSEUAT/Cover",
84+
//"CompetencyDashboardUrl": "https://tabuat.data.england.nhs.uk/#/workbooks/7866/views",
85+
//"CompetencyDashboardUrl": "https://tabuat.data.england.nhs.uk/#/workbooks/7839/views",
86+
//"Username": "SVC_default_TEL",
87+
"Username": "[email protected]",
8588
"Password": "",
8689
"ClientName": "tel_dls",
8790
"ClientId": "a7906ce3-e0c9-403e-a169-8eb78d858f8a",

0 commit comments

Comments
 (0)