Skip to content

Commit 8a949fc

Browse files
committed
TD-4086 Reworks JWT token generation to add iss and kid to token headers
1 parent 70fe40c commit 8a949fc

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static class ConfigurationExtensions
5151
private const string UserResearchUrlName = "UserResearchUrl";
5252
private const string TableauSectionKey = "TableauDashboards";
5353
private const string TableauClientId = "ClientId";
54+
private const string TableauClientSecretId = "ClientSecretId";
5455
private const string TableauClientSecret = "ClientSecret";
5556
private const string TableauUsername = "Username";
5657
private const string TableauClientName = "ClientName";
@@ -236,6 +237,10 @@ public static string GetTableauClientSecret(this IConfiguration config)
236237
{
237238
return config[$"{TableauSectionKey}:{TableauClientSecret}"]!;
238239
}
240+
public static string GetTableauClientSecretId(this IConfiguration config)
241+
{
242+
return config[$"{TableauSectionKey}:{TableauClientSecretId}"]!;
243+
}
239244
public static string GetTableauUser(this IConfiguration config)
240245
{
241246
return config[$"{TableauSectionKey}:{TableauUsername}"]!;

DigitalLearningSolutions.Web/Helpers/ExternalApis/TableauConnectionHelper.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class TableauConnectionHelper : ITableauConnectionHelperService
2323
{
2424
private readonly string connectedAppClientName;
2525
private readonly string connectedAppSecretKey;
26+
private readonly string connectedAppSecretId;
2627
private readonly string connectedAppClientId;
2728
private readonly string tableauUrl;
2829
private readonly string dashboardUrl;
@@ -31,6 +32,7 @@ public TableauConnectionHelper(IConfiguration config)
3132
{
3233
connectedAppClientName = config.GetTableauClientName();
3334
connectedAppClientId = config.GetTableauClientId();
35+
connectedAppSecretId = config.GetTableauClientSecretId();
3436
connectedAppSecretKey = config.GetTableauClientSecret();
3537
tableauUrl = config.GetTableauSiteUrl();
3638
dashboardUrl = config.GetTableauDashboardUrl();
@@ -39,27 +41,32 @@ public TableauConnectionHelper(IConfiguration config)
3941
public string GetTableauJwt(string email)
4042
{
4143
var tokenHandler = new JwtSecurityTokenHandler();
42-
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(connectedAppSecretKey));
43-
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
44+
var key = Encoding.ASCII.GetBytes(connectedAppSecretKey);
45+
4446
var claims = new[]
4547
{
4648
new Claim(JwtRegisteredClaimNames.Sub, user),
47-
new Claim(JwtRegisteredClaimNames.Iss, connectedAppClientId),
48-
new Claim("scp", "tableau:views:embed"),
49-
new Claim("users.primaryemail", email),
5049
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
51-
new Claim(JwtRegisteredClaimNames.Exp,
52-
new DateTimeOffset(DateTime.UtcNow.AddMinutes(20)).ToUnixTimeSeconds().ToString())
50+
new Claim("users.primaryemail", email),
51+
new Claim("scp", "tableau:views:embed")
5352
};
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+
);
5465

55-
var token = new JwtSecurityToken(
56-
issuer: connectedAppClientId,
57-
audience: "tableau",
58-
claims: claims,
59-
expires: DateTime.UtcNow.AddMinutes(20),
60-
signingCredentials: credentials);
66+
var token = new JwtSecurityToken(header, payload);
67+
var tokenString = tokenHandler.WriteToken(token);
6168

62-
return new JwtSecurityTokenHandler().WriteToken(token);
69+
return tokenString;
6370
}
6471

6572
public async Task<string> AuthenticateUserAsync(string jwtToken)

DigitalLearningSolutions.Web/appsettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@
8383
"CompetencyDashboardUrl": "https://tabuat.data.england.nhs.uk/#/workbooks/7839/views",
8484
"Username": "SVC_default_TEL",
8585
"Password": "",
86-
"ClientName": "tel_dls",
86+
"ClientName": "tel_dls",
8787
"ClientId": "a7906ce3-e0c9-403e-a169-8eb78d858f8a",
88-
"ClientSecret": ""
88+
"ClientSecretId": "38b99058-a806-4ee2-bf7a-ad1933a81ae5",
89+
"ClientSecret": ""
8990
},
9091
"UserResearchUrl": "https://forms.office.com/e/nKcK8AdHRX"
9192
}

0 commit comments

Comments
 (0)