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
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class AppSettings
public string? UserBearerTokenSigningKey { get; set; }
public ProviderUser? ProviderUser { get; set; }
public EmployerUser? EmployerUser { get; set; }
public ServiceAccount? ServiceAccount { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace SFA.DAS.Authorization.TokenGenerator.Models;

public class ServiceAccount
{
[JsonPropertyName("sub")]
public string? Sub { get; set; }

[JsonPropertyName("serviceAccount")]
public string? ServiceAccountId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ static List<Claim> SetUserClaims(AppSettings appSettings)
var userType = string.Empty;

// Select user type
while (userType != "P" && userType != "E")
while (userType != "P" && userType != "E" && userType !="S")
{
Console.WriteLine("Enter P for provider user or E for employer user");
Console.WriteLine("Enter P for provider user, E for employer user or S for service account");
userType = Console.ReadLine()?.ToUpper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ internal static List<Claim> GenerateUserClaims(string userType, AppSettings appS
return GetProviderUserClaims(appSettings.ProviderUser);
case "E":
return GetEmployerUserClaims(appSettings.EmployerUser);
}
case "S":
return GetServiceAccountClaims(appSettings.ServiceAccount);
}
throw new ArgumentException("Invalid user type");
}

Expand Down Expand Up @@ -76,7 +78,24 @@ private static List<Claim> GetProviderUserClaims(ProviderUser? providerUser)
return claims;
}

private static Claim CreateExpiryClaim()
private static List<Claim> GetServiceAccountClaims(ServiceAccount? serviceAccount)
{
if (serviceAccount == null)
{
serviceAccount = new ServiceAccount();
}

var claims = new List<Claim>
{
serviceAccount.CreateClaim(x => x.Sub),
serviceAccount.CreateClaim(x => x.ServiceAccountId),
CreateExpiryClaim()
};

return claims;
}

private static Claim CreateExpiryClaim()
{
long unixTime = ((DateTimeOffset)DateTime.UtcNow.AddMinutes(5)).ToUnixTimeSeconds();
return new Claim("exp", unixTime.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"rolenumericid": "12345",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Guybrush Threepwood",
"http://schemas.portal.com/displayname": "Guybrush Threepwood",
"http://schemas.portal.com/ukprn": "0",
"http://schemas.portal.com/ukprn": "10005077",
"exp": null
},
"EmployerUser": {
Expand All @@ -37,5 +37,9 @@
"http://das/employer/identity/claims/display_name": "John Smith",
"http://das/employer/identity/claims/account": "VN48RP",
"exp": null
},
"ServiceAccount": {
"sub": "service-account-id",
"serviceAccount": "Earnings"
}
}